﻿/*    8/14/2009
		PikaChoose
	Jquery plugin for photo galleries
    Copyright (C) 2009 Jeremy Fry

*/

 /* Глобальные настрокики для шаблонов */
 $(document).ready(function (){ 
 // Определяем bg у Bread530
var st_bg = $(".style-icon-bread530 .content").css("background-color");
if(st_bg == "undefined") st_bg = $(".style-icon-bread .content").css("background-color");

var fix = $(".one-page-stars_picture");
if(fix.attr("rel") == "visible_false")
	{
		fix.css("display","none");
	}
$(".inner_prev_article").css('background-color',st_bg);
$(".inner_next_article").css('background-color',st_bg);
$(".expert").css('border-color',st_bg);
$(".exp_bg").css('background-color',st_bg);
/* вызов функции опроса по нажатию голосовать */
/*$("#lnkClient").click(function(){
	setTimeout(background, 1000); 
}); */
/******************************************** */

/* Позиция для нижнего пейджинги */
var position_url = document.URL.split("pagenum_")[1];
	if(position_url === undefined || position_url === NaN) { position_url = 0; } else { position_url = position_url* 1;}
var a_link = $(".link_pager a");
var coun_a = a_link.size();
a_link.each(function(index){
		if(index == position_url - 6) $(this).show();
		if(index == position_url - 5) $(this).show();
		if(index == position_url - 4) $(this).show();
		if(index == position_url - 3) $(this).show();
		if(index == position_url - 2) $(this).show();
		if(index == position_url - 1) $(this).show();
		if(index == position_url)  $(this).show();
		if(index == position_url + 1) $(this).show();
		if(index == position_url + 2) $(this).show();
		if(index == position_url + 3) $(this).show();
		if(index == position_url + 4) $(this).show();
});
$("#articlePager").show();
/* конец Позиция для нижнего пейджинги */

$(".vert_prev").css('background-color',st_bg);
$(".vert_next").css('background-color',st_bg);
$(".multi-css .pageNumber").css('color',st_bg);
$(".multi-css .text").css({'width':'500px','border':'none'});
$("#Div1").css('border','none');
$("#pikame #active").css("border-color",st_bg);

 /* Подцвечивание листалки */
$("ul#pikame li").click(function(){
	$("#pikame #active").attr("id","");
	$("ul#pikame li").children('img').removeClass("active");
	$(this).children('img').toggleClass("active");  
});

});

 /*************************************/
/*  $(document).ready(function (){ 
  	var test = $(".exp_foto p").html().replace(/;/g,"");
	$(".exp_foto p").html(test); 
  }); */
			$(document).ready(
				function (){

	$("ul#pikame li img").css("max-width","60px");
	$("ul#pikame li img").css("max-height","60px");
	
	var totalcontent = $("#slider ul#pikame li").size();
	if(totalcontent == 1) { $("#multi-page-instruction-vert #multi-page-article-gallery_photo-slider").css("display","none"); }
	if(totalcontent != 0) { $("#pikame").PikaChoose(); } else { $("#slider").css("display","none"); }
	/* Скрипт для one-page-stars */
	// Устанавливоем положение счетчика coun
var activ = $("#pikame #active");
var num = activ.attr("rel");	

var coun = 0;
// Кол-во картинок в строке
var counimg = 6;

if(num != undefined) { coun = num; counimg = 0; }
//Ширина площади одной страницы
var contentwidth = $("#slider2 ul#pikame li").width() + 12;
//Обще количество картинок
var totalcontent = $("#slider2 ul#pikame li").size(); 
//Общая ширина всего контента (всех страниц)
var allcontentwidth = 72 * totalcontent;
// Убираем левый или правые стрелки если одна картинка.
if(totalcontent >= 2)  { 
$("#slider2").css('display','block');  
$("#bot_left1").css('display','block'); $("#bot_right1").css('display','block');
	if (totalcontent > 6) {
			$("#slid_left").css('display','block'); $("#slid_right").css('display','block');
} else  {
			/*$("#bot_left1").css('display','none'); $("#bot_right1").css('display','none'); */
			$("#slid_left").css('display','none'); $("#slid_right").css('display','none');
		}
}
// Ширина строки с картинками
 $("#slider2 ul#pikame").css({"width":allcontentwidth});
// Убираем левый или правые стрелки если число в строке меньше totalcontent
  
  $('#slider2 #slid_right').live("click",function () {
  if(coun >= totalcontent) coun = totalcontent;
  if(coun <= 0) coun = 0; 
  if( 0 <= (totalcontent - (counimg + coun)) )
    {
		coun++;
		$('#pikame').animate({"left":"-=71px"}, "slow");
		if(coun >= 1) $("#slid_left").css('display','block');
		if((totalcontent - (counimg + coun)) == 0) $("#slid_right").css('display','none');
		$("#count").html(coun);
	}
    });
	  $('#slider2 #slid_left').live("click",function () {
	    if(coun >= totalcontent) coun = totalcontent;
		if(coun <= 0) coun = 0;
	  	if((coun > 0) ) {
		coun--;
		$('#pikame').animate({"left":"+=71px"}, "slow");
		if(coun == 0) $("#slid_left").css('display','none');
		if((totalcontent - (counimg + coun)) >= 0) $("#slid_right").css('display','block');
		$("#count").html(coun);
	  }
    });
	
	
/*****************************/
				}); 


jQuery.iPikaChoose = {
	build : function(user_options)
	{
		var user_options;
		var defaults = {
			show_captions: true,
			slide_enabled: false,
			auto_play: false,
			show_prev_next: true,
			slide_speed: 3000,
			thumb_width: 60,
			thumb_height: 60,
			buttons_text: { play: "Play", stop: "Stop", previous: "Previous", next: "Next" },
			delay_caption: false,
			user_thumbs: false
		};

		return jQuery(this).each(
			function() {
// Устанавливоем положение счетчика coun
var coun = 0;
// Кол-во картинок в строке
var counimg = 6;
//Ширина площади одной картинки
var contentwidth = $("#slider ul#pikame li").width();
//Обще количество картинок
var totalcontent = $("#slider ul#pikame li").size(); 
//Общая ширина всего контента (всех страниц)
var allcontentwidth = 72 * totalcontent;

// Ширина строки с картинками
 $("ul#pikame").css({"width":allcontentwidth});
// Убираем левый или правые стрелки если число в строке меньше totalcontent
$("#slider #slid_left").css('display','none');
if(totalcontent <= counimg) $("#slid_right").css('display','none');
// Пишем сколько фотографий в слайдере
$("#slider #pika_2").html(totalcontent);

  $('#slid_right').live("click",function () {
  if(coun >= totalcontent) coun = totalcontent;
  if(coun <= 0) coun = 0; 
  if( 0 <= (totalcontent - (counimg + coun)) )
    {
		coun++;
		$('#pikame').animate({"left":"-=71px"}, "slow");
		if(coun >= 1) $("#slid_left").css('display','block');
		if((totalcontent - (counimg + coun)) == 0) $("#slid_right").css('display','none');
		$("#count").html(coun);
	}
    });
	  $('#slid_left').live("click",function () {
	    if(coun >= totalcontent) coun = totalcontent;
		if(coun <= 0) coun = 0;
	  	if((coun > 0) ) {
		coun--;
		$('#pikame').animate({"left":"+=71px"}, "slow");
		if(coun == 0) $("#slid_left").css('display','none');
		if((totalcontent - (counimg + coun)) >= 0) $("#slid_right").css('display','block');
		$("#count").html(coun);
	  }
    });
				//bring in options
				var options = jQuery.extend(defaults, user_options);
				// grab our images
				var images = jQuery(this).children('li').children('img');
				//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("#slider").before("<div class='pika_main'></div>");
				// houses eveything about the UL
				var main_div = jQuery("#slider").prev(".pika_main"); 
				//navigation div ALWAYS gets created, its refrenced a lot				
				var navigation_div = jQuery("#slider").prev(".pika_main");
				//fill in sub elements
				var totalcontent = $("#slider ul#pikame li").size();
				 if(totalcontent > counimg) {
					navigation_div.prepend("<a id='bot_left'></a> <a  id='bot_right'></a>");
					$("#slider").append("<div id='slid_left'></div><div id='slid_right'></div>"); 
					} 
				if(totalcontent < 2) {
						$("#slider").css({'height' : '0px', 'overflow' : 'hidden'});
						$("#slid_left").css('display','none');
						$("#slid_right").css('display','none');
					} else {
						$("#slider").css('display','block');
					} 
				var previous_image_anchor = navigation_div.children('a:first');
				var next_image_anchor = navigation_div.children('a:last');

				//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.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 />");
				var main_img = sub_div.children("img");
				
				//create the caption div when appropriate
				if(options.show_captions){
					sub_div.append("<div class='pika_caption'></div>");
					var caption_div = sub_div.children(".pika_caption");
				}
				
				
				//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>");
				var main_link = main_img.parent("a");
				
				function LoadImages()
				{
					jQuery(this).bind("load", function()
					{
						//had to make a seperate function so that the thumbnails wouldn't have problems
						//from beings resized before loaded, thus not h/w
						
						//delete hidden image to clean up things.
						jQuery(this).next('img').remove();
						var w = jQuery(this).width();
						var h = jQuery(this).height();
						if(w===0){w = jQuery(this).attr("width");}
						if(h===0){h = jQuery(this).attr("height");}
						//grab a ratio for image to user defined settings
						var rw = options.thumb_width/w;
						var rh = options.thumb_height/h;
						
						//determine which has the smallest ratio (thus needing
						//to be the side we use to scale so our whole thumb is filled)
						if(rw<rh){
							//we'll use ratio later to scale and not distort
							var ratio = rh;
							var left = ((w*ratio-options.thumb_width)/2)*-1;
							left = Math.round(left);
							//set images left offset to match
						/*	jQuery(this).css({left:left});*/
						}else{
							var ratio = rw;
							//you can uncoment this lines to have the vertical picture centered
							//but usually tall photos have the focal point at the top...
							//var top = ((h*ratio-options.thumb_height)/2)*-1;
							//var top = Math.round(top);
							var top = 0;
						/*	jQuery(this).css({top:top}); */
						}
						//use those ratios to calculate scale
						var width = Math.round(w*ratio);
						var height = Math.round(h*ratio);
						jQuery(this).css("position","relative");
						jQuery(this).width(width).height(height);
						var imgcss={
							width: width,
							height: height
						};
						jQuery(this).css(imgcss);					
						jQuery(this).hover(
							function(){jQuery(this).fadeTo(250,1);},
							function(){if(!jQuery(this).hasClass("pika_selected")){jQuery(this).fadeTo(250,1);}}
						);
						jQuery(this).fadeTo(250,1);	
						
						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).insertBefore(this);
					
					jQuery(this).hide();
	
					//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
				var licss = {
					width: options.thumb_width + 4 +"px",
					height: options.thumb_height + 4 +"px",
					"list-style": "none",
					overflow: "hidden"
				};
				images.each(function(){
					jQuery(this).parent('li').css(licss);
					//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){
					// Устанавливаем какой кадр стал активным и ставим ему класс 
					var activ_img = jQuery(this).parent().attr("rel");
					$("#slider #pika_1").html(activ_img);

					//catch when user clicks on an image Then cancel current slideshow
					if(how!="auto"){
						if(options.slide_enabled){
							stop_anchor.hide();
							play_anchor.show();
							playing=false;
						}
						main_img.stop();
						main_img.dequeue();
						if(options.show_captions)
						{
							caption_div.stop();
							caption_div.dequeue();
						}
					}
					//all our image variables
					if(options.user_thumbs)
					{		
						var image_source = jQuery(this).attr("ref");
					}else
					{
						var image_source = jQuery(this).attr("src");
					}
					var image_link = jQuery(this).attr("ref");
					var image_caption = jQuery(this).next("span").html();
								
					//fade out the old thumb
					images.filter(".pika_selected").fadeTo(100,1); 
					images.filter(".pika_selected").removeClass("pika_selected"); 
					//fade in the new thumb
					jQuery(this).fadeTo(100,1);
					jQuery(this).addClass("pika_selected");
					//fade the old image out and the new one in
					if(options.show_captions)
					{
						if(options.delay_caption)
						{
							caption_div.fadeTo(100,1);
						}
						caption_div.fadeTo(100,1,function(){
							caption_div.html(image_caption);
							caption_div.fadeTo(100,1);
						});
					}
					
					main_img.fadeTo(100,0,function(){
						
						//make the image fade in on load, which should hopefully get rid of any jumping
						main_img.unbind('load');
						main_img.bind('load',function()
 						{
							main_img.fadeTo(800,1,function(){
								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"]);}
									});
								}
							});
						});
						main_img.attr("src",image_source);
						main_link.attr("href", image_link);
						main_link.children().attr("alt", image_caption);
						
					});
			}//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(){
				main_div.hover(
					function(){play_div.fadeIn(600);},
					function(){play_div.fadeOut(600);}
				);
				play_anchor.bind("click", function(){
					main_img.stop();
					main_img.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.PikaChoose = jQuery.iPikaChoose.build;


