/*
	Created By: Richard Delph
	For: Bug Safari
	Date: 2009-07-10
*/

/*$(
	function ($) {
		$body = $('html,body').attr('scrollTop',0);
		$(document).ready(on_document_ready);
	}
);

function on_document_ready(e) {
	
	
	// setup habitats panel scroller
	initHabitatsScroller();
	
	// setup bug gallery scroller
	initBugGalleryScroller();

	$('#flash-habitat-wrapper').flash({
		swf: '/flash/GardenPond.swf',
		height: 304,
		width: 655,
		params: {
			play: true,
			loop: true,
			quality: 'high',
			scale: 'showall',
			wmode: 'transparent',
			name: 'flash-habitat',
			id: 'flash-habitat',
			allowfullscreen: false,
			allowscriptaccess: 'sameDomain'			
		}
	})


}
*/

function loadHomeContent() {
	
  	$('#bug-anim-wrapper').flash({
    		swf: '/flash/buganim.swf',
    		height: 418,
    		width: 622,
    		params: {
    			play: true,
    			loop: true,
    			quality: 'high',
    			scale: 'showall',
    			wmode: 'transparent',
    			name: 'buganim',
    			id: 'buganim',
    			allowfullscreen: false,
    			allowscriptaccess: 'sameDomain'			
    		}
    	});
	
}

function initHabitatsScroller() {
	// $hbs = habitat panel scroller
	$hps = $('#habitat-panel-scroller');
	
	// set width of hbs, based on number of children with class .habitat-panel
	$hps.totalWidth = 0;
	$hps.totalPanels = 0;
	$hps.noPanelsDisplayed = 3;
	
	$hps.find('.habitat-panel').each(function() {
		$hps.totalPanels += 1;
		$hps.totalWidth +=	($(this).width() + 
							parseInt($(this).css('margin-left')) + 
							parseInt($(this).css('margin-right')) +
							parseInt($(this).css('padding-left')) +
							parseInt($(this).css('padding-right')));
	});
	$hps.width($hps.totalWidth);
	
	// determine width of habitat panel (hp)
	$hps.panelWidth = $hps.totalWidth / $hps.totalPanels;
	
	// set left & right arrow variables
	$hpsla = $('#habitats-left-arrow');
	$hpsra = $('#habitats-right-arrow');
	
	$hpsla.click(function(e) { e.preventDefault(); })
	$hpsra.click(function(e) { e.preventDefault(); })
	
	$hpsla.css('visibility','hidden');
	
	bindPanelArrowClickEvents($hps,$hpsla,$hpsra);
	
}

function initBugGalleryScroller() {
	// $hbs = habitat panel scroller
	$bgs = $('#bug-gallery-scroller');
	
	// set width of hbs, based on number of children with class .habitat-panel
	$bgs.totalWidth = 0;
	$bgs.totalPanels = 0;
	$bgs.noPanelsDisplayed = 1;
	
	$bgs.find('.bug-gallery-group').each(function() {
		$bgs.totalPanels += 1;
		$bgs.totalWidth +=	($(this).width() + 
							parseInt($(this).css('margin-left')) + 
							parseInt($(this).css('margin-right')) +
							parseInt($(this).css('padding-left')) +
							parseInt($(this).css('padding-right')));
	});
	$bgs.width($bgs.totalWidth);
	
	// determine width of habitat panel (hp)
	$bgs.panelWidth = $bgs.totalWidth / $bgs.totalPanels;
	
	// set left & right arrow variables
	$bgsla = $('#bug-gallery-left-arrow');
	$bgsra = $('#bug-gallery-right-arrow');
	
	$bgsla.click(function(e) { e.preventDefault(); })
	$bgsra.click(function(e) { e.preventDefault(); })
	
	$bgsla.css('visibility','hidden');
	
	bindPanelArrowClickEvents($bgs,$bgsla,$bgsra);
}






function bindPanelArrowClickEvents(ip,leftArrow,rightArrow) {
	leftArrow.bind('click',{dir:'left',imagePanel:ip,leftArrow:leftArrow,rightArrow:rightArrow},panelArrowClickEvent);
	rightArrow.bind('click',{dir:'right',imagePanel:ip,leftArrow:leftArrow,rightArrow:rightArrow},panelArrowClickEvent);
}
function unbindPanelArrowClickEvents(leftArrow,rightArrow) {
	rightArrow.unbind('click',panelArrowClickEvent);
	leftArrow.unbind('click',panelArrowClickEvent);
}

function panelArrowClickEvent(e) {
	
	var dir = e.data.dir;
	var imagePanel = e.data.imagePanel;
	var leftArrow = e.data.leftArrow;
	var rightArrow = e.data.rightArrow;

	var curleft = parseInt(imagePanel.css('left'));
	
	switch(dir) {
		case 'left':

			//work out new left, add photo width to cur length
			var newleft = curleft + imagePanel.panelWidth;

			// if new left = 0 hide left arrow
			if ( newleft == 0 ) {
				leftArrow.css('visibility','hidden');
			} 

			// unbind right click event
			unbindPanelArrowClickEvents(leftArrow,rightArrow);
			rightArrow.css('visibility','visible');

			imagePanel.animate({left: newleft+'px'},1300,'easeOutElastic',function() {
				// bind both arrow click events
				bindPanelArrowClickEvents(imagePanel,leftArrow,rightArrow);
			});

			// don't forget to break or it'll fall straight into the right animation
			break;

		case 'right':

			// work out new left, minus photo width from cur left
			var newleft = curleft - imagePanel.panelWidth;

			// if abs newleft + photo width = total photo width then hide right arrow
			if ( Math.abs(newleft) + (imagePanel.panelWidth*imagePanel.noPanelsDisplayed) >= imagePanel.totalWidth ) {
				rightArrow.css('visibility','hidden');
			} 
			
			// unbind arrow event
			unbindPanelArrowClickEvents(leftArrow,rightArrow);
			leftArrow.css('visibility','visible');

			

			imagePanel.animate({left: newleft+'px'},1300,'easeOutElastic',function() {
				// bind both arrow click events
				bindPanelArrowClickEvents(imagePanel,leftArrow,rightArrow);
			});

			break;
	}
	
}


// function to load in habitat bug data from xml files
// called from the hit button in the flash explorer

function loadBugData(f) {
	$.ajax({
		url: '/xml/'+f,
		global: true,
		type: "GET",
		dataType: "xml",
		success: function(xml) {
			
			var dx = 400;
			
			var title = $(xml).find('title').text();
			var content = $(xml).find('content').text();
			var image = '<img src="/images/habitat-shots/' + $(xml).find('image').text() + '" alt="'+title+'" title="'+title+'" />';
			
			$('#habitat-content-wrapper').fadeOut(dx,function() {
				// load new content
				$('#habitat-content-wrapper h3').css('font-size','15px').html(title);
				$('#habitat-content').css('font-size','10px').html(content);
				
				$('#habitat-content-wrapper').fadeIn(dx);
				
			});
			
			$('#habitat-image-wrapper').fadeOut(dx,function() {
				// load new image in...
				$('#habitat-image-wrapper').html($(image))
				
				$('#habitat-image-wrapper').fadeIn(dx);
			});
			
		}
	});
}