/*    
		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/>.
*/

jQuery.iPikaChoose = {
	build : function(user_options)
	{
		var user_options;
		var defaults = {
			show_captions: true,
			slide_enabled: true,
			auto_play: false,
			show_prev_next: true,
			slide_speed: 5000,
			thumb_width: 50,
			thumb_height: 45
		};

		return $(this).each(
			function() {
				var options = $.extend(defaults, user_options);
				$(this).hide();
				var $images = $(this).children('li').children('img');
				var $ulist = $(this);
				$images.fadeTo(1,0.01);
				//this function builds the structure of pikachoose and assigns
				//a variable for each of the elements to be accessed
				$(this).before("<div class='pika_main'></div>");
				var $main_div = $(this).prev(".pika_main");
				
				if(options.slide_enabled){
					$main_div.append("<div class='pika_play'></div>");
					var $play_div = $(this).prev(".pika_main").children(".pika_play");
					$play_div.html("<a class='pika_play_button'><img src='' alt='Play'></a>\
										<a class='pika_stop_button'><img src='' alt='Stop'></a>");
					$play_div.fadeOut(1);
					var $play_anchor = $play_div.children('a:first');
					var $stop_anchor = $play_div.children('a:last');
				}
				$main_div.append("<div class='pika_subdiv'></div>");
				var $sub_div = $main_div.children(".pika_subdiv");
				$sub_div.append("<img />");
				var $main_img = $sub_div.children("img");
				
				if(options.show_captions){
					$sub_div.append("<div class='pika_caption'></div>");
					var $caption_div = $sub_div.children(".pika_caption");
				}
				
				
					$(this).after("<div class='pika_navigation'></div>");
					var $navigation_div = $(this).next(".pika_navigation");
					//fill in sub elements
					$navigation_div.prepend("<a>Previous</a> :: <a>Next</a>");
					var $previous_image_anchor = $navigation_div.children('a:first');
					var $next_image_anchor = $navigation_div.children('a:last');
				if(!options.show_prev_next){
					$navigation_div.css("display","none");
				}
				var $playing = options.auto_play;

				
			function activate(){
				$images.bind("click",image_click);
				if(options.auto_play){
					$playing = true;
					$play_anchor.hide();
					$stop_anchor.show();
				}else{
					$play_anchor.show();
					$stop_anchor.hide();
				}
				
				$previous_image_anchor.bind("click",previous_image);
				$next_image_anchor.bind("click",next_image);	
				
				$(window).load(function(){
					prep_thumbs();
					$images.filter(":not(:first)").fadeTo(250,0.4);
					$images.filter(":first").fadeTo(250,1,function(){
						$(this).trigger("click",["auto"]);
						$ulist.fadeIn(1000);
					});
				});
			}//end activate function
			
			function prep_thumbs(){
					$images.each(function(){
						var $w = $(this).width();
						var $h = $(this).height();
						if($w==0){$w = $(this).attr("width");}
						if($h==0){$h = $(this).attr("height");}
						var $rw = options.thumb_width/$w;
						var $rh = options.thumb_height/$h;
						
						if($rw<$rh){
							var $ratio = $rh;
							var $left = (($w*$ratio-options.thumb_width)/2)*-1;
							var $left = Math.round($left);
							$(this).css({left:$left});
						}else{
							var $ratio = $rw;
							var $top = (($h*$ratio-options.thumb_height)/2)*-1;
							var $top = Math.round($top);
							$(this).css({left:$top});
						}

						var $width = Math.round($w*$ratio);
						var $height = Math.round($h*$ratio);
						$(this).css("position","relative");
						$(this).width($width).height($height);
						var imgcss={
							width: $width,
							height: $height
						};
						
						$(this).css(imgcss);
						var licss = {
							width: options.thumb_width+"px",
							height: options.thumb_height+"px",
							"list-style": "none",
							overflow: "hidden"
						};
						$(this).parent('li').css(licss);
						$(this).hover(
							function(){$(this).fadeTo(250,1);},
							function(){if(!$(this).hasClass("pika_selected")){$(this).fadeTo(250,0.4);}}
						);
					});
					$images.filter(":last").addClass("pika_last");
					$images.filter(":first").addClass("pika_first");
			}//end fix thumbs functions
			function image_click(event, how){
					//catch when user clicks on an image Then cancel current slideshow
					if(how!="auto"){
						$stop_anchor.hide();
						$play_anchor.show();
						$playing=false;
						$sub_div.stop();
						$sub_div.dequeue();
					}			
					var $image_source = $(this).attr("src");
					var $image_caption = $(this).attr("title");
								
					//fade out the old thumb
					$images.filter(".pika_selected").fadeTo(250,0.4); 
					$images.filter(".pika_selected").removeClass("pika_selected"); 
					//fade in the new thumb
					$(this).fadeTo(250,1);
					$(this).addClass("pika_selected");
					//fade the old one out and the new one in.
					
					$sub_div.fadeTo(500,0.05,function(){
						$main_img.attr("src",$image_source);
						if(options.show_captions){$caption_div.html($image_caption);}
					});
					$sub_div.fadeTo(800,1,function(){
						if($playing){
							$(this).animate({top:0},options.slide_speed, function(){
								//redudency needed here to catch the user clicking on an image during a change.
								if($playing){$next_image_anchor.trigger("click",["auto"]);}
							});
						}
					});
			}//end image_click function
			
			function next_image(event, how){
				if($images.filter(".pika_selected").hasClass("pika_last")){
					$images.filter(":first").trigger("click",how);
				}else{
					$images.filter(".pika_selected").parent('li').next('li').children('img').trigger("click",how);
				}
			}//end next image function
			
			function previous_image(event, how){
				if($images.filter(".pika_selected").hasClass("pika_last")){
					$images.filter(":first").trigger("click",how);
				}else{
					$images.filter(".pika_selected").parent('li').next('li').children('img').trigger("click",how);
				}
			}//end previous image function
			
			function play_button(){
				$main_div.hover(
					function(){$play_div.fadeIn(400);},
					function(){$play_div.fadeOut(400);}
				);
				$play_anchor.bind("click", function(){
					$playing = true;
					$next_image_anchor.trigger("click",["auto"]);
					$(this).hide();
					$stop_anchor.show();
				});
				$stop_anchor.bind("click", function(){
					$playing = false;
					$(this).hide();
					$play_anchor.show();
				});
			}
			play_button();
			activate();

		});//end return this.each
	}//end build function
	
	//activate applies the appropriate actions to all the different parts of the structure.
	//and loads the sets the first image
};//end jquery.ipikachoose		
jQuery.fn.PikaChoose = jQuery.iPikaChoose.build;
