/*    
		PikaChoose
	Jquery plugin for photo galleries
    Copyright (C) 2008 Jeremy Fry

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

//setup
if(typeof($pika_captions)== 'undefined'){$pika_captions = true;} //wether to display captions
if(typeof($pika_slide)== 'undefined'){$pika_slide = false;}//if you use multiple sets of pikachoose in a page please
//set pika_slide to false. Slideshows with multiple items create problems. :(
if(typeof($pika_play)== 'undefined'){$pika_play = false;}//set to true for slideshow to play initially
if(typeof($pika_nav_buttons)== 'undefined'){$pika_nav_buttons = false;}//show prev/next buttons
if(typeof($pika_speed)== 'undefined'){$pika_speed = 5000;} //delay between slideshow in ms
if(typeof($pika_thumb_w)== 'undefined'){$pika_thumb_w = 80;}//in pixels
if(typeof($pika_thumb_h)== 'undefined'){$pika_thumb_h = 80;} //in inches... j/k pixels.

$(window).bind('load', function() {
	$(".main_pika").each(function(){
		$(this).children(".pika_caption").before("<img />");
	});
	//this loads the first image when we load the page
	$(".pikachoose").each(function(){
		$(this).children('li:first').children('img').trigger("click",["auto"]);
	});

	$(".pikachoose").children('li').children('img').fadeIn("slow");
		//grab our main items. Images and LIs
	var $lis = $(".pikachoose").children('li');
	var $imgs = $lis.children('img')
	
		$imgs.each(function(){
		var $w = $(this).width();
		var $h = $(this).height();
		var $rw = $pika_thumb_w/$w;
		var $rh = $pika_thumb_h/$h;
		if($rw<$rh){
			var $ratio = $rh;
			var $left = (($w*$ratio-$pika_thumb_w)/2)*-1;
			$left = Math.round($left);
			$(this).css({left:$left});
		}else{
			var $ratio = $rw;
			var $top = (($h*$ratio-$pika_thumb_h)/2)*-1;
			$top = Math.round($top);
			$(this).css({left:$top});
		}
		
		var $width = Math.round($w*$ratio);
		var $height = Math.round($h*$ratio);
		$(this).width($width).height($height);
	});
	
});

$(document).ready(function()
{
	var $play_anchor = $(".pika_play").children("a:first-child");
	var $stop_anchor = $(".pika_play").children("a:last-child");
	//grab our main items. Images and LIs
	var $lis = $(".pikachoose").children('li');
	var $imgs = $lis.children('img')

	$lis.each(function(){
		$(this).css({width:$pika_thumb_w, height:$pika_thumb_h});
	});
		
	//anchorsfor back forward and slideshow
	var $next_anchor = $(".pika_nav").children("a:last");	

	//setting to 50% to differentiate(sp?) between
	//selected on and non selected	
	$lis.fadeTo(1,.5);
	
	//the big chunk. Does everything
	function PikaDoIt(event,how)
	{		
			//catch when user clicks on an image . This way we don't start a new slide show cycle
			if(how!="auto"){
				$stop_anchor.hide();
				$play_anchor.show();
				$pika_play=false
			};
			//$("#span").html(how);
			//grab the element for the main image
			var $main_pika = $(this).parent('li').parent('ul').parent('div').prev('div');
			var $pika_caption = $(this).parent('li').parent('ul').parent('div').prev('div').children('div:last');
			var $image_source = $(this).attr("src");
			var $image_caption = $(this).attr("title");

			$main_pika.stop();
			$main_pika.dequeue();
			//fade out the old thumb
			$(this).parent('li').siblings('li.pika_selected').fadeTo(250,.5); 
			$(this).parent('li').siblings('li.pika_selected').removeClass("pika_selected"); 
			//fade in the new thumb
			$(this).parent('li').fadeTo(250,1);
			$(this).parent('li').addClass("pika_selected");

			//fade the old one out and the new one in.
			
			$main_pika.fadeTo(500,.05,function(){
				$main_pika.children('img').attr("src",$image_source);
				if($pika_captions){$pika_caption.html($image_caption)};
			});
			$main_pika.fadeTo(800,1);
			if($pika_play){
				$(this).stop();
				$(this).dequeue();
				$(this).animate({top:0},$pika_speed, function(){
					//redudency needed here to catch the user clicking on an image during a change.
					if($pika_play){$next_anchor.trigger("click",["auto"]);}
				});
			}
			
	}
	$imgs.click(PikaDoIt);
	//just for flashy prettiness
	$lis.hover(
		function(){$(this).fadeTo(250,1);},
		function(){if(!$(this).hasClass("pika_selected")){$(this).fadeTo(250,.5)};}
	);
	
	$(".pika_nav").each(function (){
		$(this).children("a:first").click(	function()
		{
			$selected = $(this).parent().prev("ul.pikachoose").children("li.pika_selected");
			$first = $selected.parent().children("li:first-child");
			$first.addClass("firstPika");
			if($selected.hasClass("firstPika")){
				$selected.parent().children("li:last-child").children('img').trigger('click');
			}else{
				$selected.prev("li").children('img').trigger('click');
			}
		});
	});
	
	
	$(".pika_nav").each(function (){
		$(this).children("a:last").click(	function (event,how)
		{
			$selected = $(this).parent().prev("ul.pikachoose").children("li.pika_selected");
			$last = $selected.parent().children("li:last-child");
			$last.addClass("lastPika");
			if($selected.hasClass("lastPika")){
				$selected.parent().children("li:first-child").children('img').trigger('click',[how]);
			}else{
				$selected.next("li").children('img').trigger('click',[how]);
			}
		});
	});
	if($pika_slide){
		
		$play_anchor.click(function(){
			$pika_play = true;
			$next_anchor.trigger("click",["auto"]);
			$(this).hide();
			$stop_anchor.show();
			return false;
		});
		
		$stop_anchor.click(function(){
			$(this).hide();
			$play_anchor.show();
			$pika_play = false;
			return false;
		});
		
		if(!$pika_play){	//depending on default for slide show we show apporpriate option
			$stop_anchor.hide();
		}else{
			$play_anchor.hide();
		};
	
		$(".main_pika").hover(
		function(){
			$(this).children('div.pika_play').fadeIn(200);
		},
		function(){
			$(this).children('div.pika_play').fadeOut(200);		
		});
		
		$(".pika_play").fadeOut(1);
	}else{
		$(".pika_play").css({display:"none"});
	}
	
	if(!$pika_nav_buttons){
		$(".pika_nav").css({display:"none"});		
	}
});

