var duration = 5000;
var autoplay = true;
var width = 480;
var sliderbox;
var thumbnails = [];
var current = 0;
var number;
function initSliderTabs()
{
	if(document.getElementById("gallery"))
	{
		sliderbox = $('#gallery');
		number = $('#gallery .video-holder').length;

		/* thumbnails */
		thumbnails = $('#thumbs li a');
		$(thumbnails[0]).addClass("active");
		$(thumbnails).each(function(el)
		{
			$(thumbnails)[el].href = 'javascript:;';
			$(thumbnails[el]).click(function()
			{
				if(current!=el)
				{
					$(sliderbox).animate({left: -el*width + "px"}, { duration: 400, queue:false });
					current=el;
					$(thumbnails).removeClass("active");
					$(this).addClass("active");
				}
			});
		});

		/* autoplay */
		setTimeout('rotate()', duration);
	}
}
function rotate()
{
	if(autoplay)
	{
		if(++current>=number)
			current=0;
		
		$(thumbnails).removeClass("active");
		$(thumbnails[current]).addClass("active");
		$(sliderbox).animate({left: -current*width + "px"}, { duration: 400, queue:false });
		setTimeout('rotate()', duration);
	}
}

if (window.addEventListener)
	window.addEventListener("load", initSliderTabs, false);
else if (window.attachEvent)
	window.attachEvent("onload", initSliderTabs);



$(document).ready(function(){
	initTabs('.tabset');
   	initAccordion('#accordion');
	
	// gestione filtri ajax sui pulsanti accordion
	$(".mostview").click(function(){

		var url = $(this).attr("href") + "&filter=" +  $(this).attr("rel")
		//alert( url );
		$(this).parent().find(".slide").load( url );
		

		var bg = $(this).css("background-image")
		// in questo controllo è possibile aggiungere la gestione css per il cambio di immagini
		if ( $(this).attr("rel") == "mostview" ) {
				$(this).attr("rel","recent")
				$(this).css("background-image", bg.replace("mostview","recent"))

			} else {
				$(this).attr("rel","mostview")
				$(this).css("background-image", bg.replace("recent","mostview"))
				}
		
		return false;
		}) 
	
		// gestione overlay video del giorno
		$("span.img-holder").hover( 
			function() { // mouse in
	
				$(this).append( "<img style='position: absolute; top: 0px; left: 1px;' class='thumb_play' src='/wp-content/themes/careertv/images/rollover_thumb_play_trans.png'>" );
			}, 

			function () { // mouse out

				$(this).find(".thumb_play").remove();
			
			});
		// fine gestione overlay video del giorno
		
		/* 
		09.04.09 | maurizio 
		funcione ajax per gestire il rimuovi dalla lista
		vieve fatta la chiamata alla pagina indicata nel link
		e se ritorna con successo fa un fedout dell'oggetto LI
		*/
		$("a.btn_myremove").click( function() { 
						var url = $(this).attr("rel")					
						var obj = this
						
						$.get( url , function(data){
						   if (data == "removed") {
							   $(obj).parents("li").fadeOut();
							   }
						 });						
						

						return false;
						})
		
	
	/*
    if(typeof('tabs') == 'function') $('#tabs-area').tabs({ 
														  fxAutoHeight: true,
														  onClick: function() {alert( 'ciao' ); }
														  });
	*/

});


/* gestione accordion*/
function initAccordion(_cont){
	var _speed = 500;
	$(_cont).each(function(){
		var _a = -1;
		var _list = $(this).children();
		_list.each(function(_i){
			this._h = $(this).children('.acco-content');
			this._box = $(this).find('.acco-content > .slide:first');
			this._btn = $(this).find('.acco-content > h3:first');
			if($(this).hasClass('active')){
				_a = _i;
				this._box.show();
			}
			else{
				this._box.hide();
			}
			this._btn.click(function(){
				if(_a != -1){
					_list.eq(_a).removeClass('active');
					_list.get(_a)._h.stop().animate({height:_list.get(_a)._btn.outerHeight(true)}, _speed, function(){
						$(this).children('.slide:first').hide();
						$(this).height('auto');
					});
				}
				if(_a != _i){
					_list.eq(_i).addClass('active');
					_list.get(_i)._h.height(_list.get(_i)._h.height());
					_list.get(_i)._box.show();
					_list.get(_i)._h.stop().animate({height: _list.get(_i)._box.outerHeight(true) + _list.get(_i)._btn.outerHeight(true)}, _speed, function(){
						$(this).height('auto');
					});
					_a = _i;
				}
				else{
					_a = -1;
				}
				return false;
			});
		});
	});
}

function initTabs(h_list) {
	$(h_list).each(function(_ind, _el) {
		var btn_h = $(_el);
		var _btn = $(_el).find('a');
		var _a = 0;
		_btn.each(function(_ind, _el) {
			var _box = $('#'+_el.href.substr(_el.href.indexOf("#") + 1));
			if($(_el).parent().hasClass('tabs-selected')) {
				_box.show();
				_a = _ind;
			}
			else {
				_box.hide();
			}
			_el.onclick = function() {
				if(!$(this).parent().hasClass('tabs-selected')){
					$('#'+ _btn.get(_a).href.substr(_btn.get(_a).href.indexOf("#") + 1)).hide();
					_btn.eq(_a).parent().removeClass('tabs-selected');
					$('#'+ this.href.substr(this.href.indexOf("#") + 1)).show();
					$(this).parent().addClass('tabs-selected');
					tabsContent(this)
					_a = _ind;
				}
				return false;
			}
		});
	});
}


function tabsContent( obj ) {

	switch( obj.id ) {
	
		case "lbwhere":
			
			var map = null;
			var geocoder = null;
			var address = $("#geoaddr").html();
			
			if ( address == "" ) return false;

			//alert( address );
			
			if (GBrowserIsCompatible()) {
				map = new GMap2(document.getElementById("map_canvas"));
		        map.addControl(new GSmallMapControl());

				geocoder = new GClientGeocoder();
				
				//map.setCenter(new GLatLng(37.4419, -122.1419), 13);

				  if (geocoder ) {
					geocoder.getLatLng(
					  address,
					  function(point) {
						if (!point) {
						  alert(address + " non trovato");
						} else {
						  map.setCenter(point, 15);
						  var marker = new GMarker(point);
						  map.addOverlay(marker);
						  marker.openInfoWindowHtml(address);
						}
					  }
					);
				  }



			  }
		
		
		break; //si ferma qui
		
		
		default:
		//istruzioni
		}
	
	
	}