/* Author: 
	Filippo Della Casa
*/

var isLayout = false;
var layout = 'debug/Interactive-Map.jpg';

// images background
var backgrounds = new Array();
// shortcut document
var $doc;
// shortcut backgroun image
var $img;

$(document).ready(function(e) {

	//alert(channel_entry)	

	$doc = $(document);
	$img = $('#img-bg-ground');
	initMenu();
	initExplore();
	initImgsBind();
	// passed from channel has a string XML
	loadXMLString(backgrounds_image_xml)
	if ( $('#tagline') ){
		$('#tagline').hide();
	}
	
});

/*  BACKGROUNDS **************************************************************   */
loadXMLString=function(xml_string){
	var xmlDoc = $.parseXML( xml_string );
   	parseXML( xmlDoc ); 
}
parseXML = function(xml)
{
	$(xml).find("img").each(
		function()
		{
			backgrounds.push( $(this).attr("src") );
		});
	initBackGrounds();
}
initBackGrounds=function(){
	// eventos resize window!!!!!!!!
	$(window).resize(function(e){
		onResizeWindow();
	});
	loadRandBackGroundImage();
	onResizeWindow();	
}


loadRandBackGroundImage=function(){
	var url = backgrounds[Math.floor(Math.random()*backgrounds.length)];
	if ( backgrounds.length == 1 ) url = backgrounds[0];
	if ( isLayout ) url = layout;
	loadBackGroundImage( url );
}
loadBackGroundImage=function(url){	
	
	// hide and bind to the load event
	$img.hide();
	$img.bind("load",function(){ 
		resizeBackGround();
		$(this).fadeIn(300,function(){
			
				if ( $('#tagline') ){
					$('#tagline').css('visibility','visible');
					$('#tagline').fadeIn(600);
				}
				
				if ( $("#btn-brown") ){
					$("#btn-brown").css('opacity',.8);
					$("#btn-brown").hover( function(e){$(this).css('opacity',1)}, function(e){$(this).css('opacity',.8)} );
				}

			}); 
	});	
	 //set the src attribute now, after insertion to the DOM
	$img.attr('src', url);	
}
resizeBackGround=function(){
	var imgw = $img.width();
	var imgh = $img.height();
	var ratio = Math.max( w/imgw, h/imgh );
	var ww = imgw*ratio;
	var hh = imgh*ratio;
	if ( isLayout )
		$img.css( { position:'absolute', left:w/2 - imgw/2+2, top:-3, width:imgw, height:imgh } );
	else
		$img.css( { position:'absolute', left:w/2 - ww/2, top:margenTop + h/2 - hh/2, width:ww, height:hh } );
}


/*  RESIZE **************************************************************   */
var margenTop = 0;
var w;
var h;

onResizeWindow=function(){
	
	w = $doc.width();
	h = $doc.height()-margenTop;
	
	resizeBackGround();
	
	//elements home
	if ( $('#tagline') ){
		// center tagline
		$('#tagline').css('top', h/2 );
	}
	setPosWindowMap();
}

/* MENU ********************************************************************  */
var not_open_submenu_if_selected = false;

initMenu=function(){
	
	
	
	
    $("ul.dropdown li").hover(function(){
		
		//debug($(this).first())
		//if ( $(this).first().is("span") ) return;
		
		var $li =  $(this);
		
		if ( not_open_submenu_if_selected && $li.hasClass('selected') ) return;
		
		var $subul = $('ul:first',this);
		
		
		
		// remove flecha blanca menu principal if selected
		if( $li.hasClass( 'selected' ) && $subul.hasClass('sub-menu') ){
			$li.css( 'background-image', 'none' );
			//$li.addClass( 'no-background' );
		}
				
		$li.addClass("hover");
        
		var tox = $li.offset().left + $li.width()/2 - $subul.width()/2 - 10;
		var toy = $('header').offset().top + $('header').height();
				
		$subul.css('left', tox );
		$subul.css('top', toy );
		
		$subul.hide();
		$subul.stop(true,true).slideDown(100).fadeIn( 400 );
		$subul.css('visibility', 'visible');
		
    }, function(){
		var $li =  $(this);
		var $subul = $('ul:first',this);
		
		// add flecha blanca menu principal if selected
		if( $li.hasClass( 'selected' )  && $subul.hasClass('sub-menu')  ){
			$li.css( 'background-image', 'url(' + URL_BASE + 'img/arrow_menu.png)' );
			//$li.addClass( 'yes-background' );
		}
		
        $li.removeClass("hover");
		
		$subul.stop(true,true).slideUp(100).fadeOut( 300, function(){ $(this).css('visibility', 'hidden'); } );      
    });
    
    //$("ul.dropdown li ul li:has(ul)").find("a:first").append(" &raquo; ");
}




/* EXPLORE NATURE SECTION *****************************************/

initExplore=function(){
	initGalleryNature();
	initAcordeonesNature();
	initTabsNature();
	initInteractiveMap();
}
initImgsBind=function()
{
	/*
	var imageclasstobind = ['.gallery-thumb'];
	
	for ( var i=0; i<imageclasstobind.length; i++ )
	{
		var cls = imageclasstobind[i];
		$(cls).addClass('loading');
		$(cls).bind( 'load', function(event){
			$(this).removeClass('loading');
		});
	}
	*/
}
initGalleryNature=function(){
	
    $(".gallery-thumb").hover(function(){
		var $divimg =  $(this);
		if ( $divimg.hasClass('gallery-thumb-selected') ) return;
        $divimg.addClass("gallery-thumb-hover");		
    }, function(){
		var $divimg =  $(this);
        $divimg.removeClass("gallery-thumb-hover");     
    });
	// fancybox
	$("a#enlarge-fancybox").fancybox({
				'overlayShow'	: true,
				'padding' : 0,
				'titlePosition' 		: 'over'
			});
	
	$(".enlarge-link-fancybox").fancybox({
				'overlayShow'	: true,
				'padding' : 0,
				'titlePosition' 		: 'over'
			});
	// load Image function
	var $imggallery = $('.img-gallery-img');
	$imggallery.bind( 'load', function() {  
		onLoadImageOfTheGallery() 
	});
	
	// Menu Seasons!!!!!
	$('ul.ul-season li').click( function(e){
		// find old selected
		$liselected = $('ul.ul-season').find('li.selected');
		if ( $liselected ){
			$liselected.removeClass('selected')
		}
		$liselected = $(this);
		$liselected.addClass('selected');
		
		var tox = 0;
		switch( $liselected.html() ){
			case 'Summer':
				tox = 0;
			break
			case 'Spring':
				tox = -899;
			break
			case 'Fall':
				tox = -(2*899);
			break
			case 'Winter':
				tox = -(3*899);
			break
		}
		$('#gallery-slider').animate(
			{
				left: tox
		  	}, 300, 'swing', function() {
			  // complete handler
		});
	});
	
	
	// Select current season depending on month date
	var current_season = '';
	if ( current_month ){
		// month go from 0 --> 11
		var month_n = current_month-1;
		var day = current_day;
		
		//alert( day + ',' + month_n )
		
		var today = new Date();
		today.setMonth( month_n );
		today.setDate( day );
		
		var springDate = new Date();
		springDate.setMonth(3-1);
		springDate.setDate(20);

		var summerDate = new Date();
		summerDate.setMonth(6-1);
		summerDate.setDate(21);

		var fallDate = new Date();
		fallDate.setMonth(9-1);
		fallDate.setDate(23);

		var winterDate = new Date();
		winterDate.setMonth(12-1);
		winterDate.setDate(22);
		
		
		if ( today >= springDate && today < summerDate )
		{
			current_season = 'Spring';
		}
		else if ( today >= summerDate && today < fallDate )
		{
			current_season = 'Summer';
		}
		else if ( today >= fallDate && today < winterDate )
		{
			current_season = 'Fall';
		}
		else
		{
			current_season = 'Winter';
		}
		$('ul.ul-season li').each( function(index,element){
			if( $(element).html() == current_season )
			{
				$(element).click();
			}
		} );
	}

	
	
}
openImageGallery=function(el,src,srclarge,caption){
	$divsel = $(el);
	// remove prec selected
	$gallerythumb = $divsel.parents('#gallery-thumbs');
	$gallerythumb.find('div.gallery-thumb-selected').removeClass('gallery-thumb-selected');
	
	// sel the new one
	$divsel.addClass('gallery-thumb-selected');
	
	$galleryseason = $gallerythumb.parents('#gallery-season');
	
	loadImageOfTheGallery( $galleryseason, src );

	// change caption
	$galleryseason.find('.gallery-caption').html(caption);
	// change src large of enlarge to be fired from colorbox
	$galleryseason.find('a#enlarge-fancybox').attr( 'title', caption );
	$galleryseason.find('a#enlarge-fancybox').attr( 'href', srclarge );	
}

loadImageOfTheGallery=function( $galleryseason, src ){
	//$.fancybox.showActivity();
	
	
	
	// load image
	var $imggallery = $galleryseason.find('.img-gallery-img');
	$imggallery.fadeTo( 100, .4 );
	$imggallery.attr('src', '' );
	$imggallery.attr('src', src + '?rand=' + Math.random() );
	
	
	//var centerximage = $galleryseason.css('left');
	//var centeryimage = $galleryseason.css('top');
	
	//	$('#fancybox-loading').css('left', 0 );
	//$('#fancybox-loading').css('top', 0 );
	
}
onLoadImageOfTheGallery=function(){
	//$.fancybox.hideActivity();
	var $imggallery = $('.img-gallery-img');
	$imggallery.fadeTo( 300, 1 );
}
initAcordeonesNature=function(){
	// NATUREA AREAS --> LOTUS PONDS	
	
	
	$(".nat-text-more").hide();
	$("a.nat-more").click( function( e ){
			e.preventDefault();
			var $text_more = $(this).parents('.nat-text-acordeon-list').find('.nat-text-more');
			$text_more.slideToggle(200);
			var actualtxt = $(this).html();
			var txt;
			switch (actualtxt)
			{
				case 'more':
					txt = "less";
					break;
				case 'less':
					txt = "more";
					break;
				case 'see all':
					txt = "see less";
					break;
				case 'see less':
					txt = "see all";
					break;
					
			}
			$(this).html( txt );
		},function(){
			
		} );
	
		
	// PLANTS & ANIMALS
	$(".nat-text-more-pa").hide();
	$("a.nat-more-pa").click( function( e ){
			e.preventDefault();
			var $text_more = $(this).parents('.nat-text-acordeon-list-pa').find('.nat-text-more-pa');
			$text_more.slideToggle(200);
			$(this).html( $(this).html() == 'see all' ? 'see less' : 'see all' );
		},function(){
			
		} );
		
		
	//alert($(".nat-text-more-pa"))
}

initTabsNature=function(){
	$('#tab-pa-plants').click( function(e){
		if ( $(this).hasClass('tab-pa-sel') ) return;	
			
		$('#tab-pa-animals').removeClass( 'tab-pa-sel' );		
		$(this).addClass('tab-pa-sel');

		switchPlantsAndAnimals('plants');
	});
	$('#tab-pa-animals').click( function(e){
		if ( $(this).hasClass('tab-pa-sel') ) return;	
			
		$('#tab-pa-plants').removeClass( 'tab-pa-sel' );		
		$(this).addClass('tab-pa-sel');
		
		switchPlantsAndAnimals('animals');
	});
	
	$('#animals-list-content').css('visibility', 'hidden' );	
}

switchPlantsAndAnimals=function(qual){
	
	var toy = qual == 'plants' ? 0 : -382;
	var tox = qual == 'plants' ? 0 : -718;
	
	
	
	$('#plants-list-content').css('visibility', qual == 'plants' ? 'visible' : 'hidden' );
	$('#animals-list-content').css('visibility', qual == 'animals' ? 'visible' : 'hidden' );
	
	
	$('#gallery-switch').css('top', toy );
	$('#list-container-slider').css('left', tox );
	

	
	
	/*
	$('#gallery-switch').animate(
		{
			top: toy
		}, 400, 'swing', function() {
		  // complete handler
	});
	$('#list-container-slider').animate(
		{
			left: tox
		}, 400, 'swing', function() {
		  // complete handler
	});	
	*/
}

// menu sidebar
var point_selected = 0;

// coordinates window sidebar
var win_coor = [[0,0],
[554,354],
[692,364],
[626,515],
[460,519],
[333,372],
[120,367],
[86,272],

[410,450],
[661,330],
[269,369],

[649,364],
[579,513],
[332,416],
[132,448],
[40,334],
[155,332],
[526,346],
[595,350]

]; 

var id_interval;
initInteractiveMap=function(){
	// click on sidebar
	$("ul.second-title li").click( function(event){
		var num = event.currentTarget.id.split('_')[1];
		onSelectPointMap(num);		
	});
	$("ul.second-title li").hover( function(event){	
		var num = parseInt( event.currentTarget.id.split('_')[1], 10 );
		if ( num <= 7 ){
			var $point = $('#point_'+num);			
			$point.addClass('point-hover');
		}	
	}, function(event){
		var num = parseInt( event.currentTarget.id.split('_')[1], 10 );
		if ( num <= 7 ){
			var $point = $('#point_'+num);
			$point.removeClass('point-hover');
		}	
	} );
	
	// acordeon
	$(".win-text-more").hide();
	$("a.a-text-more").click( function( e ){
			e.preventDefault();
			var $text_more = $(this).parents('.win-content').find('.win-text-more');
			id_interval = setInterval( "setPosWindowMap()", 30 );
			$text_more.slideToggle(400,function(){clearInterval(id_interval);setPosWindowMap();});
			$(this).html( $(this).html() == 'READ MORE »' ? 'READ LESS »' : 'READ MORE »' );
		},function(){
			
		} );
	// close window
	$("a.close-win-btn").click( function(e){ 
		e.preventDefault();
		closeWindowMap();
	} )
	//debug coor
	/*
	$('#i-map-wrapper').click( function(event){
		//alert( (event.pageX - $(this).offset().left ) + "," + event.pageY );
	} );
	*/
	
}
getliPointMap=function(n){
	return $("ul.second-title li#mappt_" + n);
}
// map points
onMapClick=function(n){
	onSelectPointMap(n);
}

var id_check;
onMapOver=function(n){
	clearTimeout( id_check );
	var $li = getliPointMap(n);
	$li.addClass( 'hover_map' );
	onSelectPointMap(n);
}
onMapOut=function(n){
	var $li = getliPointMap(n);
	$li.removeClass( 'hover_map' );
	id_check = setTimeout( closeWindowMap, 400 );
}
onSelectPointMap=function(num){
	if ( num == point_selected ) return;
	var $wind;
	// deselect
	if ( point_selected != 0 )
	{
		$wind = $('#window-pt_'+point_selected);
		closeWindowMap( $wind );		
	}
	point_selected = num;
	// open window info	
	$wind = $('#window-pt_'+point_selected);
	$(".win-text-more").hide();
	openWindowMap( $wind );
}
setPosWindowMap=function( $wind ){
	if ( !$wind )
	{
		$wind = $('#window-pt_'+point_selected);	
	}
	if ( $wind && $('#i-map-wrapper') )
	{
		var tox = win_coor[point_selected][0] + ($('#i-map-wrapper').offset()?$('#i-map-wrapper').offset().left:0) - $wind.width()/2;
		var toy = win_coor[point_selected][1]  - $wind.height() - 10;		
		$wind.css('left',  tox );
		$wind.css('top', Math.max( 63 , toy ) );
		$wind.hover( function(e){
			
			clearTimeout( id_check );
			
		}, function(e){
			
			closeWindowMap()
			
		})	
	}
}
onmapPhoto=function(num){
	onSelectPointMap(num);
}
openWindowMap=function( $wind ){
	$wind.hide();
	$wind.css('visibility', 'visible' );
	setPosWindowMap( $wind );
	$wind.fadeIn(300, function(){
		
		$('#i-map-wrapper').bind('click', function(event){
			//closeWindowMap()
		});
		
	});	
}
closeWindowMap=function( $wind ){
	if ( !$wind )
	{
		$wind = $('#window-pt_'+point_selected);	
	}
	$wind.fadeOut(200);
	$wind.hover( undefined, undefined );
	point_selected = 0;
	$('#i-map-wrapper').unbind( 'click' );
}
debug = function(o){
	var str = '';
	for ( var i in o ){
		//str += i + ' = ' + o[i] + '\n';
		str += i + '\n';
	}
	alert(str)
}







