jQuery.iPikaChooseTiker = {
	build : function(user_options)
	{
		var user_options;
		var defaults = {
			show_captions: false,
			slide_enabled: true,
			auto_play: true,
			show_prev_next: true,
			speedCaption: "fast",
			thumb_width: 50,
			thumb_height: 50,
			buttons_text: { 
					play: "<img src='/static/img/tiker_play.png'>", 
					stop: "<img src='/static/img/tiker_stop.png'>", 
					previous: "<img src='/static/img/tiker_l.png'>", 
					next: "<img src='/static/img/tiker_r.png'>" 
			},
			delay_caption: true,
			user_thumbs: true,
			
			slide_speed: 3000, //скорость слайд-шоу			
			speedSlide: "fast", //скорость затухания превьюх
			opacitySlide: 0.4,
			speedImg: 1000

		};

		return jQuery(this).each(
			function() {
				//bring in options
				var options = jQuery.extend(defaults, user_options);
				// grab our images
				var images = jQuery(this).children('li').children('img');
				var images_h2 = jQuery(this).children('li').children('h2');
				//hide the images so the user doesn't see crap
				images.fadeOut(1);

				
				//save our list for future ref
				var ulist = jQuery(this);
				images.each(LoadImages);

				//start building structure
				jQuery(this).before("<div class='pika_main'></div>");
				// houses eveything about the UL
				var main_div = jQuery(this).prev(".pika_main");
				
				//add in slideshow elements when appropriate
				if(options.slide_enabled){
					main_div.append("<div class='pika_play'></div>");
					var play_div = jQuery(this).prev(".pika_main").children(".pika_play");
					
					play_div.css('z-index', '6');
					
					play_div.html("<a class='pika_play_button'>" + options.buttons_text.play + "</a><a class='pika_stop_button'>" + options.buttons_text.stop + "</a>");
					//play_div.fadeOut(1);
					var play_anchor = play_div.children('a:first');
					var stop_anchor = play_div.children('a:last');
				}
				//this div is used to make image and caption fade together
				main_div.append("<div class='pika_subdiv'></div>");
				var sub_div = main_div.children(".pika_subdiv");
				
				//the main image we'll be using to load
				sub_div.append("<img/>");
//				sub_div.append("<img class='pika_main_img2' />");
				
				var main_img  = sub_div.children("img");
//				var main_img2 = sub_div.children(".pika_main_img2");

//				main_img.css('z-index', '5');
//				main_img2.css('z-index', '4');
				
				//navigation div ALWAYS gets created, its refrenced a lot				
				main_div.append("<div class='pika_navigation'></div>");
				var navigation_div = jQuery(this).prev(".pika_main").children(".pika_navigation");
				
				navigation_div.css('z-index', '6');
				//fill in sub elements
				navigation_div.prepend("<a id='pika_navigation_a1'>" + options.buttons_text.previous + "</a><a id='pika_navigation_a2'>" + options.buttons_text.next + "</a>");
				var previous_image_anchor = navigation_div.children('a:first');
				var next_image_anchor = navigation_div.children('a:last');
				
				//hide the navigation if the user doesn't want it
				if(!options.show_prev_next){
					navigation_div.css("display","none");
				}
				
				//playing triggers the loop for the slideshow
				var playing = options.auto_play;
				
				main_img.wrap("<a></a>");
//main_img2.wrap("<a></a>");
				var main_link = main_img.parent("a");
//var main_link2 = main_img2.parent("a");
				
				function LoadImages()
				{
					jQuery(this).bind("load", function()
					{

						jQuery(this).hover(
							function(){
								jQuery(this).fadeTo(options.speedSlide,1);
								jQuery(this).parent('li').children('h2').fadeTo(options.speedSlide,1);
							},

							function(){
								if(!jQuery(this).hasClass("pika_selected")){
									jQuery(this).fadeTo(options.speedSlide,options.opacitySlide);
									jQuery(this).parent('li').children('h2').fadeTo(options.speedSlide,options.opacitySlide);
								}
							}
						);
						jQuery(this).fadeTo(options.speedSlide,options.opacitySlide);	
						jQuery(this).parent('li').children('h2').fadeTo(options.speedSlide,options.opacitySlide);
//var image_link = jQuery(this).next("h2").attr("url");
//main_link.attr("href", image_link);						
						if(jQuery(this).hasClass('pika_first')){
							jQuery(this).trigger("click",["auto"]);
						}
						
					});
				
					//clone so the on loads will fire correctly
					var newImage = jQuery(this).clone(true).insertAfter(this);
					
					jQuery(this).remove();
	
					//reset images to the clones
					images = ulist.children('li').children('img');
				}
			function activate()
			{
				//sets the intial phase for everything
				
				//image_click is controls the fading
				images.bind("click",image_click);
				//hiding refrence to slide elements if slide is disabled
				if(options.slide_enabled){
					if(options.auto_play){
						playing = true;
						play_anchor.hide();
						stop_anchor.show();
					}else{
						play_anchor.show();
						stop_anchor.hide();
					}
				}
				
				images.filter(":last").addClass("pika_last");
				images.filter(":first").addClass("pika_first");
				//css for the list

				images.each(function(){

					//fixes a bug where images don't get the correct display after loading
					if(jQuery(this).attr('complete')==true && jQuery(this).css('display')=="none")
					{
						jQuery(this).css({display:'inline'});
					}
				});
				//previous link to go back an image
				previous_image_anchor.bind("click",previous_image);
				//ditto for forward, also the item that gets auto clicked for slideshow
				next_image_anchor.bind("click",next_image);	
			}//end activate function

			function image_click(event, how){
					//catch when user clicks on an image Then cancel current slideshow
				
					var image_link = jQuery(this).next("h2").attr("url");
					var image_caption = jQuery(this).next("span").html();
					
										
					if(how!="auto"){
						document.location = image_link;
					
						if(options.slide_enabled){
							stop_anchor.hide();
							play_anchor.show();
							playing=false;
						}
						
						main_img.stop();
						main_img.dequeue();			

					}
					//all our image variables
					if(options.user_thumbs)
					{		
						var image_source = jQuery(this).attr("ref");
					}else
					{
						var image_source = jQuery(this).attr("src");
					}

					if (main_img.attr("src")){	
					}else{
						main_img.attr("src",image_source);
						main_link.css({"background-image": 'url('+image_source+')'});
//main_img.css({"background-image": 'url('+image_source+')'});
						
					}	

								
					//fade out the old thumb
					images.filter(".pika_selected").fadeTo(options.speedSlide,options.opacitySlide); 
					images.filter(".pika_selected").removeClass("pika_selected"); 
					
					images_h2.filter(".pika_selected").fadeTo(options.speedSlide,options.opacitySlide); 
					images_h2.filter(".pika_selected").removeClass("pika_selected");  
					//fade in the new thumb
					jQuery(this).fadeTo(options.speedSlide,1)
					jQuery(this).addClass("pika_selected");
					
					jQuery(this).parent('li').children('h2').fadeTo(options.speedSlide,1);
					jQuery(this).parent('li').children('h2').addClass("pika_selected");


					main_link.css({"background-image": 'url('+image_source+')'});
										
					main_img.fadeTo(options.speedImg,0,function(){
				
						main_link.attr("href", image_link);
						main_img.attr("src",image_source);		
						//main_img.css({"background-image": 'url('+image_source+')'});				
						main_img.fadeTo(1,1);
						
						if(playing){
							jQuery(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_first")){
					images.filter(":last").trigger("click",how);
				}else{
					images.filter(".pika_selected").parent('li').prev('li').children('img').trigger("click",how);
				}
			}//end previous image function
			
			function play_button(){

				play_anchor.bind("click", function(){
					main_img.stop();
					main_img.dequeue();
					
//					main_img2.stop();
//					main_img2.dequeue();
					
					if(options.show_captions)
					{
						caption_div.stop();
						caption_div.dequeue();
					}
					playing = true;
					next_image_anchor.trigger("click",["auto"]);
					jQuery(this).hide();
					stop_anchor.show();
				});
				stop_anchor.bind("click", function(){
					playing = false;
					jQuery(this).hide();
					play_anchor.show();
				});
			}
			if(options.slide_enabled){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.PikaChooseTiker = jQuery.iPikaChooseTiker.build;