$(document).ready(function(){
	initTargetBlanks();
	initServices();
	initTabs();
	initSearch();
	initFotoGallery();
	initProzaSort();
	initRequirementsProzaPopUp();
	initLibraryList();
	initLibraryTabs();
	initLibraryParts();
	initMenu();
	initDataPicker();
	initPersonalFoto();
	initSocialPanel();
	initCalenderTarget();

	$("#bf").click(function(){
		$(".beautyfest").slideToggle();
		return false;
	});

})

initTargetBlanks = function(){
	$(".target_blank").click(function(){
		window.open($(this).attr("href"));
		return false;
	})
}

initCalenderTarget = function(){
	$(".calendar a").each(function(){
		var target = $(this).attr("target");
		if (target == ''){
			$(this).attr("target", "_blank");
		}
	});
}

initServices = function(){
	$("#services dd").eq(0).find(".content").show();
	$("#services h3").click(function(){
		if (!($(this).parent("dd").hasClass("active"))){
			$("#services dd").removeClass("active");
			$(this).parent("dd").addClass("active");
			$("#services .content").slideUp("slow");
			$(this).parent("dd").find(".content").slideDown("slow");
		}
	});
	/*
	$("#social_panel").mouseenter(function(){
		$(this).animate({right: 0}, {duration: 600, queue: false});
	}).mouseleave(function(){
		$(this).animate({right: -228+"px"}, {duration: 600, queue: false});
	})
	*/
}

initDataPicker = function(){
	$( "#datepicker" ).datepicker();
}

initSocialPanel = function(){
	soundManager.url = "/skins/default/swf/";
	$("#social_panel .submenu a").eq(1).css('display','none');
	$("#social_panel .submenu a").eq(2).css('display','none');
	$("#social_panel .submenu a").eq(0).click( function(){
		if ($("#social_panel").hasClass("social_panel_hover_off")){
			$("#social_panel").removeClass("social_panel_hover_off");
			$("#social_panel .submenu a").eq(1).css('display','block');
			$("#social_panel .submenu a").eq(2).css('display','block');
		} else {
			$("#social_panel").addClass("social_panel_hover_off");
			$("#social_panel .submenu a").eq(1).css('display','none');
			$("#social_panel .submenu a").eq(2).css('display','none');
		}
	});
	$("#social_panel .submenu a").eq(2).click( function(){	
		if ($("#social_panel").hasClass("social_panel_hover_play")){
			$("#social_panel").removeClass("social_panel_hover_play");
			$("#social_panel").addClass("social_panel_hover_pause");
            //uppodSend('player','play');
			soundManager.play('mySound',{
				onfinish:function(){
					$("#social_panel").removeClass("social_panel_hover_pause");
					$("#social_panel").addClass("social_panel_hover_play");
				}
			});
		} else {
			$("#social_panel").removeClass("social_panel_hover_pause");
			$("#social_panel").addClass("social_panel_hover_play");
            //uppodSend('player','pause'); 
			soundManager.pause('mySound');
		}
	});
};

initMenu = function(){
	$("#main_menu ul li").mouseenter( function(){
		if (!$(this).find("div").hasClass("hover")){
			$("#main_menu ul li div").removeClass("hover");
			$(this).find("div").addClass("hover");
		}
	});
	$("#main_menu ul li").mouseleave( function(){
		if ($(this).find("div").hasClass("hover")){
			$(this).find("div").removeClass("hover");
			$("#main_menu ul .active div").addClass("hover");
		}
	});
}

initPersonalFoto = function(){
	$("#right_column #personal_foto .img").mouseenter( function(){
		if (!$(this).hasClass("selected")){
		//	$("#right_column #personal_foto .img").removeClass("selected");
			$(this).addClass("selected");
		}
	});
	$("#right_column #personal_foto .img").mouseleave( function(){
		if ($(this).hasClass("selected")){
			$(this).removeClass("selected");
		}
	});	
	
	var first_personal_foto_item = 0;
	var all_personal_foto_item = 	$("#right_column #personal_foto .img").length;
	
	for(i=3;i<all_personal_foto_item;i++){
		$("#right_column #personal_foto .img").eq(i).css('display','none');
	}
	
	
	$("#right_column #personal_foto .bottom").click(function(){
		if(first_personal_foto_item+2 < all_personal_foto_item-1){
			$("#right_column #personal_foto .img").eq(first_personal_foto_item).css('display','none');
			first_personal_foto_item ++;
			$("#right_column #personal_foto .img").eq(first_personal_foto_item+2).css('display','block');
		}
	});
	
	$("#right_column #personal_foto .top").click(function(){
		if(first_personal_foto_item > 0){
			$("#right_column #personal_foto .img").eq(first_personal_foto_item+2).css('display','none');
			first_personal_foto_item --;
			$("#right_column #personal_foto .img").eq(first_personal_foto_item).css('display','block');
		}
	});
}

initFotoGallery = function(){
	
	var current_foto_item = 0;
	var all_foto_item = $("#content .fotoline .scroll .fotoarray a").length;
	var loaded = 0;
	/*
	for(i=0;i<all_foto_item;i++){
		var img = $("#content .fotoline .scroll .fotoarray a").eq(i).find("img");
		
		img.load(function(){			
			var width = $(this).width();
			var height = $(this).height();
			console.log(width+" "+height);
			if(width/height > 1) {
				$(this).width(80);
				$(this).css('margin-top',(80-$(this).height())/2);
			}
			else {
				$(this).height(80);	
				$(this).css('margin-left',(80-$(this).width())/2);
			}
			loaded ++;
			if(loaded == all_foto_item) $("#content .fotoline .scroll .fotoarray a").eq(current_foto_item).click(); //автоклик на первый элемент
		});
	}
	*/
	/*
	$("#content .fotoline .scroll .fotoarray a").each(function(){
		alert($(this).children("div").children("img").attr("src_max"));
	});
	*/
	
	for(i=0;i<all_foto_item;i++){
		var img = $("#content .fotoline .scroll .fotoarray a").eq(i).find("img");
		
			var width = img.width();
			var height = img.height();
			//console.log(width+" "+height);
			if(width/height > 1) {
				img.width(80);
				img.css('margin-top',(80-img.height())/2);
			}
			else {
				img.height(80);	
				img.css('margin-left',(80-img.width())/2);
			}
			loaded ++;					
	}
	
	//arr_len = imagearr.length;
	
	$("#content .fotoline .scroll .fotoarray a").click(function(){
	
	i = 0;
	var imagearr = [];
	$("#content .fotoline .scroll .fotoarray a").each(function(){
		imagearr[i] = $(this).children("div").children("img").attr("src_max");
		i++;
	});
	
		if (!$(this).hasClass("active")){
			$("#content .fotoline .scroll .fotoarray a").removeClass("active");
			$(this).addClass("active");
			var img = $(this).find("img");
			var width = img.width();
			var height = img.height();
			
			//if(width/height > 371/282) width = 0;
			if(height > width){
				width = 0;
			}
			
			var src = $(this).find("img").attr("src_max");
			var ht = '';
			
			for (var i = 0; i < imagearr.length; i++){
				//var disp = 'none;'
				if(width == 0) {
					var size = 'height=\"282\"';
				}
				else{
					var size = 'width=\"371\"';
				}
				
				if (imagearr[i] == $(this).find("img").attr("src_max")){
					$("#content .fotoline .before_photo").html(ht);
					ht = '';
					//disp = 'block';
					$("#content .fotoline .view .border div").html("<a  class=\"fancybox\" rel='allfoto' href=\""+ imagearr[i] +"\"><img src="+ $(this).find("img").attr("src_mid") +" " + size + "></a>");
				}
				else{
				ht = ht + "<a  class=\"fancybox\" rel='allfoto' href='"+ imagearr[i] +"'><img src='/skins/default/images/000.gif'></a>";
				}
			}
			
			$("#content .fotoline .after_photo").html(ht);
			
			/*
			if(width == 0) {
				//$("#content .fotoline .view .border div").html("<img src="+ $(this).find("img").attr("src") +" width=\"410\">");
				$("#content .fotoline .view .border div").html("<a class=\"fancybox\" rel='allfoto' href=\""+ $(this).find("img").attr("src_max") +"\"><img src="+ $(this).find("img").attr("src_mid") +" height=\"282\"></a>");            
			} else { 
				$("#content .fotoline .view .border div").html("<a class=\"fancybox\" rel='allfoto' href=\""+ $(this).find("img").attr("src_max") +"\"><img src="+ $(this).find("img").attr("src_mid") +" width=\"371\"></a>");
			};
			*/
			
			   $("#content .fotoline .view .border div a.fancybox").fancybox();
/*
				$("#content .fotoline .view .border div a.fancybox").fancybox({
					itemLoadCallback: getGroupItems
				});	*/		   
			
			$("#content .fotoline .view .title").html($(this).find("p").eq(0).html()+"<span>"+$(this).find("p").eq(1).html()+"</span>");
			$("#content .fotoline .view .text").html($(this).find("p").eq(2).html());
			 
			current_foto_item = $("#content .fotoline .scroll .fotoarray a").index($(this));
			
			for(i=0;i<all_foto_item-1;i++){
				if(((current_foto_item-current_foto_item%4)>i)||((current_foto_item-current_foto_item%4 + 3)<i)) {
					$("#content .fotoline .scroll .fotoarray a").eq(i).css('display','none');
				} else {
					$("#content .fotoline .scroll .fotoarray a").eq(i).css('display','block');
				}
			}
		
			var w_border = 0;
			w_border = $("#content #wrap_border .border img").width();
			if(w_border > 371) w_border = 371;
			$("#content #wrap_border .border div").css("width",w_border+"px");
			
			var h_border = 0;
			h_border = $("#content #wrap_border .border img").height();
			if(h_border < 282){
				var margin = (282 - h_border) / 2;
				$("#content #wrap_border .border img").css("padding-top", margin + "px");
			}
			/*$("#content #wrap_border .border div").css("height",h_border+"px");*/
			
			/*w_border = w_border + 22;
			$("#content #wrap_border").css("width",w_border+"px");*/

		
		} 
	});	
	
	if(loaded == all_foto_item){		
		$("#content .fotoline .scroll .fotoarray a").eq(current_foto_item).click();
	}
	
	$("#content .fotoline .view .next").click(function(){
		if(current_foto_item<all_foto_item-1){
			current_foto_item++; 
		} else {
			current_foto_item=0;
		}
		$("#content .fotoline .scroll .fotoarray a").eq(current_foto_item).click();
	});
	
	$("#content .fotoline .view .prev").click(function(){
		if(current_foto_item>0){
			current_foto_item--; 
		} else {
			current_foto_item=all_foto_item-1;
		}
		$("#content .fotoline .scroll .fotoarray a").eq(current_foto_item).click();	
	});	
	
	$("#content .fotoline .scroll .next").click(function(){
		if(current_foto_item<all_foto_item-1){
			current_foto_item+=4; 
			if(current_foto_item>all_foto_item-1) current_foto_item=all_foto_item-1;
		} else {
			//при большом листании авто перехода не будет
		}
		$("#content .fotoline .scroll .fotoarray a").eq(current_foto_item).click();
	});
	
	$("#content .fotoline .scroll .prev").click(function(){
		if(current_foto_item>0){
			current_foto_item-=4;
			if(current_foto_item<0) current_foto_item=0;
		} else {
			//при большом листании авто перехода не будет
		}
		$("#content .fotoline .scroll .fotoarray a").eq(current_foto_item).click();	
	});		
	
	
	
}
/*
function getGroupItems(opts) {
					jQuery.each(imageList, function(i, val) {
						opts.itemArray.push(val);
					});
				}
*/
initTabs = function(){
	$("#content .tabs .tabs_ul li").click(function(){
		if (!$(this).hasClass("active")){
			$("#content .tabs .tabs_ul li").removeClass("active");
			$(this).addClass("active");
			var index = $("#content .tabs .tabs_ul li").index($(this));
			$("#content .tabs .tab").removeClass("active");
			$("#content .tabs .tab").eq(index).addClass("active");
		} 
	})
}

initLibraryParts = function(){
	$("#content .library_select_part a").click(function(){
        if (!$(this).hasClass("active")){
			$("#content .library_select_part a").removeClass("active");
			$(this).addClass("active");
			var index = $("#content .library_select_part a").index($(this));
			$("#content .tabs .tab").removeClass("active");
			$("#content .tabs .tab").eq(index).addClass("active");
		} 
 
	})
}

initLibraryTabs = function(){
	$("#content .library_avtor a").click(function(){
		if (!$(this).hasClass("active")){
			$("#content .library_avtor a").removeClass("active");
			$(this).addClass("active");
		} 
	})
	$("#content .library_name a").click(function(){
		if (!$(this).hasClass("active")){
			$("#content .library_name a").removeClass("active");
			$(this).addClass("active");
		} 
	})
}

initLibraryList = function(){
	$("#content .library_active_list li").click(function(){
		if (!$(this).hasClass("active")){
			$("#content .library_active_list li").removeClass("active");
			$(this).addClass("active");
		} else {
			$(this).removeClass("active");
		}
	})
}

initRequirementsProzaPopUp = function(){
	$("#content .requirements4proza .popup").click(function(){
		if (!$(this).hasClass("active")){ $(this).addClass("active"); } 
		else { $(this).removeClass("active"); }
	})
}

initProzaSort = function(){
	$("#content .proza_sort_tab a").click(function(){
		if (!$(this).hasClass("up")){
			$("#content .proza_sort_tab a").removeClass("up");
			$("#content .proza_sort_tab a").removeClass("down");
			$(this).addClass("up");
			var index = $("#content .proza_sort_tab a").index($(this));
			$("#content .proza_content").removeClass("proza_active");
			$("#content .proza_content").eq(index*2).addClass("proza_active");
		} else if (!$(this).hasClass("down")){
			$("#content .proza_sort_tab a").removeClass("up");
			$("#content .proza_sort_tab a").removeClass("down");
			$(this).addClass("down");
			var index = $("#content .proza_sort_tab a").index($(this));
			$("#content .proza_content").removeClass("proza_active");
			$("#content .proza_content").eq(index*2+1).addClass("proza_active");
		}
	})
}

initSearch = function(){
    $(".right_column_find_form input.submit").click(function(){
        if ( $(".right_column_find_form input.text").val() == "Поиск")
            return false;
        return true;
    });
 	$("input.text").each(function(){
		if ($(this).attr("value") != $(this).attr("title")) {
			$(this).addClass("focus");
		}
	});
	$("input.text").focus(function(){
		if ($(this).attr("value") == $(this).attr("title")) {
			$(this).attr("value", "");
			$(this).addClass("focus");
		}
	});
	$("input.text").blur(function(){
		if ($(this).attr("value") == "") {
			$(this).attr("value", $(this).attr("title"));
			$(this).removeClass("focus");
		}
	});
 	$("textarea").each(function(){
		if ($(this).attr("value") != $(this).attr("title")) {		 
			$(this).addClass("focus");
		}
	}); 
	$("textarea").focus(function(){
		if ($(this).attr("value") == $(this).attr("title")) {
			$(this).attr("value", "");
			$(this).addClass("focus");
		}
	});
	$("textarea").blur(function(){
		if ($(this).attr("value") == "") {
			$(this).attr("value", $(this).attr("title"));
			$(this).removeClass("focus");
		}
	});
};
function getBrowserInfo() { 
  var t,v = undefined; 
 
  if (window.chrome) t = 'Chrome'; 
  else if (window.opera) t = 'Opera'; 
  else if (document.all) { 
  t = 'IE'; 
  var nv = navigator.appVersion; 
  var s = nv.indexOf('MSIE')+5; 
  v = nv.substring(s,s+1); 
  } 
  else if (navigator.appName) t = 'Netscape'; 
 
  return {type:t,version:v}; 
 } 
 
 function bookmark(a){ 
  var url = window.document.location; 
  var title = window.document.title; 
  var b = getBrowserInfo(); 
 
  if (b.type == 'IE' && 8 >= b.version && b.version >= 4) window.external.AddFavorite(url,title); 
  else if (b.type == 'Opera') { 
  a.href = url; 
  a.rel = "sidebar"; 
  a.title = url+','+title;
  return true; 
  } 
  else if (b.type == "Netscape") window.sidebar.addPanel(title,url,""); 
  else alert("Нажмите CTRL-D, чтобы добавить страницу в закладки."); 
  return false; 
 };
 
 function setHome(ob) { 
ob.style.behavior='url(#default#homepage)'; 
ob.setHomePage(document.location); 
} 


/*$("a[rel=book]").fancybox({
	'transitionIn'		: 'none',
	'transitionOut'		: 'none',
	'titlePosition' 	: 'over',
	'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
	return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
	}
});
*/
function getPlayer(movieName)
     {
         if (navigator.appName.indexOf("Microsoft") != -1) 
         {
             return window[movieName];
             } 
         else 
         {
             return document[movieName];
             }
         }
 
     function playAudio()
     {
         getPlayer('player1').playMusic();
     }
     
     function stopAudio()
         {
                 getPlayer('player1').stopMusic();
         }


