var end = true, work = false;
var kol = 0;

$(document).ready(function (){
	
	bind_m();
	
	sifr();
	
	_height();
	
	popupa();
	
	$("p.shadow").css("opacity", "0.3");
});

function bind_m(){
	$("div.tab").bind("mouseover", function(){
		$("div.tab").stop().not($(this)).animate({width: 140}, 150);
		$(this).animate({width: 365}, 150);
	});
	$("div.tab_block").bind("mouseleave", function(){
		$("div.tab").animate({width: 185}, 150);
	});
}

function sifr(){
	if(typeof sIFR == "function"){

		sIFR.replaceElement(named({
			sSelector:"div.tab_menu p.ru",
			sFlashSrc:"img/swift.swf",
			sColor:"#FFFFFF",
			sLinkColor:"#FFFFFF",
			sBgColor:"#FFFFFF",
			sHoverColor:"#FFFFFF",
			nPaddingTop:0,
			nPaddingBottom:0,
			sWmode:"transparent",
			sFlashVars:"textalign=left&offsetTop=0"
		}));		

		sIFR.replaceElement(named({
			sSelector:"div.tab_menu p.en",
			sFlashSrc:"img/swift.swf",
			sColor:"#CC0000",
			sLinkColor:"#CC0000",
			sBgColor:"#CC0000",
			sHoverColor:"#CC0000",
			nPaddingTop:0,
			nPaddingBottom:0,
			sWmode:"transparent",
			sFlashVars:"textalign=left&offsetTop=0"
		}));		


		sIFR.replaceElement(named({
			sSelector:"h1",
			sFlashSrc:"img/swift.swf",
			sColor:"#FFFFFF",
			sLinkColor:"#FFFFFF",
			sBgColor:"#FFFFFF",
			sHoverColor:"#FFFFFF",
			nPaddingTop:0,
			nPaddingBottom:0,
			sWmode:"transparent",
			sFlashVars:"textalign=right&offsetTop=0"
		}));		

		sIFR.replaceElement(named({
			sSelector:"h2",
			sFlashSrc:"img/swift.swf",
			sColor:"#999999",
			sLinkColor:"#999999",
			sBgColor:"#999999",
			sHoverColor:"#999999",
			nPaddingTop:0,
			nPaddingBottom:0,
			sWmode:"transparent",
			sFlashVars:"textalign=right&offsetTop=0"
		}));
	}
}

function _height(){
		if($.browser.opera) windowHeight = window.innerHeight;
		else windowHeight = $(window).height();
		
		var h = (windowHeight - $("div.main").height())/2 -50;
		if (h < 30) h = 30		
		
		if ($("div.main").height() < windowHeight) $("div.main").css("top", h).css("padding-bottom", "0px");
		else $("div.main").css("padding-bottom", "30px");
	
}

$(window).resize(function(){ _height() });

/*function preview(el, end){

	var html2;
	
	if (end){
	
		html += '<a href="' + el.parent().attr("href") + '"title=num:"' + el.data("num") + '">' + el.parent().html() + '</a>';
		width += (el.width() + 10);
		
		n_width[n++] = width;		
		
		html += '</div>';
		html2 = html.split('<div class="pr');
		html = '';
		for (var i = 1; i < html2.length; i++) html += '<div style="width:' + n_width[i-1] + 'px;" class="pr' + html2[i];
		
		$("div.preview").html(html);
		$("div.preview").css("width", $("div.preview div.pr").not(".nodis").width() + 49);

		$("div.preview").prepend('<div class="zapl-left"><img src="img/zapl.gif"/></div>');
		
		if ($("div.preview div.pr").not(".nodis").next().length) $("div.preview")
			.append('<a href="javascript:void(0)" onclick="click_right();" class="right-prev"><img src="img/right-small.gif" /></a>');
		
		$("div.preview").css("visibility", "visible");
		
		$("div.preview div.pr a").bind("click", function(){
			if (!work) {
				$("div.navig").css("background-position", "center center");																											
				_replace($(this));
			}
			return false;
		});
		
	}
	else {
		if ((width + el.width()) <= 764) {
			html += '<a href="' + el.parent().attr("href") + '">' + el.parent().html() + '</a>';
			width += (el.width() + 10);
		}
		else {
			n_width[n] = width;
			n++;
			width = (el.width() + 10);
			html += '</div><div class="pr nodis"><a href="' + el.parent().attr("href") + '">' + el.parent().html() + '</a>';
		}
	}
}*/

function popupa(){
	
	var el, img, a, new_img, prev, pr_width = 0, num = 0;
	width = 0, html = '<div class="pr">', n = 0, n_width = [];
		
	var prev = $("div.popup div.preview a"), i = 0;
	
	if (prev.length > 15) {
		prev.each(function(){
			if (i == 14){
				prev.slice(0, 15).wrapAll("<div class='pr'></div>");
				prev = $("div.popup div.preview a").not("div.popup div.preview div.pr a");
				i = -1;
			}
			else if (i == prev.length - 1) prev.wrapAll("<div class='pr'></div>");
			i++;
		});
	}
	else prev.wrapAll("<div class='pr'></div>");
				
	$("div.pr").not(":first").addClass("nodis");
	
	$("div.preview").prepend('<div class="zapl-left"><img src="img/zapl.gif"/></div>');
	
	if ($("div.preview div.pr").not(".nodis").next().length) 
		$("div.preview").append('<a href="javascript:void(0)" onclick="click_right();" class="right-prev"><img src="img/right-small.gif" /></a>');
		
	$("div.preview div.pr a").bind("click", function(){
		if (!work) {
			$("div.navig").css("background-position", "444px center");																											
			_replace($(this));
		}
		return false;
	});
		

				
				/*prev.each(function(e) {
					if (this.complete) {
						num++;
						if (num == prev.length) preview($(this), true);
						else preview($(this), false); 						
					} else {
						$(this).bind("load", function(e) {
							var el=$(e.target);
							num++;
							if (num == prev.length) preview(el, true);
							else preview(el, false); 
							$(el).unbind("load");
						});
					}
				});*/

				
	$("div.popup div.image img").each(function(e) {
		if (this.complete) {
			_show(true);
		} else {
			$(this).bind("load", function(e) {
				var el=$(e.target);
				_show(true);
				$(el).unbind("load");
			});
		}
	});
	
	kol = 0;
	
	$("div.preview a img").each(function(e) {
		if (this.complete) {
			sm()
		} else {
			$(this).bind("load", function(e) {
				sm();
			});
		};
	});

}

function sm(){
	kol++;
	if (kol == $("div.preview a img").length) $("div.preview").css("visibility", "visible");
}

var active;

function _show(stat){
		
		var b, tp, lft;
		
		$("div.navig").css("background-position", "-100px -100px");
		img = $("div.popup div.image img");
		tp = (540 - img.height())/2;
		lft = (810 - img.width())/2 + 65;		
		img.css({top: tp, left: lft}).css("visibility", "visible");
		prevnext(img, $("div.preview a").eq(0));
}

function prevnext(img, active) {
	
		var tp = (540 - img.height())/2;
		var lft = (810 - img.width())/2;
		var bp_left = (28 + parseInt(lft)) + 'px center';
		var bp_right = (img.width()/2 + 20) + 'px center';
		
		$("div.right_foto, div.left_foto").css({height: img.height(), top: tp, visibility: "visible"});
		$("div.navig").css("height", img.height());

		$("div.left_foto")
			.bind("mouseover", function(){$("div.left_foto").css("background-position", bp_left);})
			.bind("mouseout", function(){$("div.left_foto").css("background-position", "left -100px");});
		$("div.right_foto")
			.bind("mouseover", function(){$("div.right_foto").css("background-position", bp_right);})
			.bind("mouseout", function(){$("div.right_foto").css("background-position", "right -100px");});
		
		if (active.next().length) $("div.right_foto").unbind("click").bind("click", function(){
			$("div.navig").css("background-position", "center center");																											
			_replace(active.next());
			return false;			
		});
		else if (active.parent().next().not(".zapl-right").length) $("div.right_foto").unbind("click").bind("click", function(){
			$("div.navig").css("background-position", "center center");
			_replace(active.parent().next().find("a:first"));
			click_right();
			return false;
	 	});
		else $("div.right_foto").css("visibility", "hidden");
		
		if (active.prev().length) $("div.left_foto").unbind("click").bind("click", function(){
			$("div.navig").css("background-position", "center center");																														
			_replace(active.prev());
			return false;
		});
		else if (active.parent().prev().not(".zapl-left").length) $("div.left_foto").unbind("click").bind("click", function(){
			$("div.navig").css("background-position", "center center");																														
			_replace(active.parent().prev().find("a:last"));
			click_left();
			return false;
		});
		else $("div.left_foto").css("visibility", "hidden");
		
		window.setTimeout( function(){$("div.right_foto, div.left_foto").fadeIn();}, 500);
}

//var width = 0, html = '<div class="pr">', wmax = 689, n = 0, n_width = new Array;

function _replace(a){
			work = true;
	 		$("div.right_foto, div.left_foto").fadeOut();
			img = $("div.popup div.image img");
			img.css("opacity", 1).fadeTo("normal", 1, function(){
				img.after('<img src=' + a.attr("href").split("tb")[0] + ' />');
				new_img = $("div.popup div.image img").eq(1).hide();
				new_img.each(function(e) {
					if (this.complete) {
						new_img.css("top", (540 - new_img.height())/2);
						new_img.css("left", (810 - new_img.width())/2 + 65);
						new_img.css("visibility", "visible");
						$("div.navig").css("background-position", "-100px -100px");	
						prevnext(new_img, a);
						window.setTimeout( function(){new_img.fadeIn(function(){img.remove();work = false;});}, 100);
					} else {
						$(this).bind("load", function(e) {
							var el = $(e.target);
							new_img.css("top", (540 - new_img.height())/2);
							new_img.css("left", (810 - new_img.width())/2 + 65);
							new_img.css("visibility", "visible");	
							$("div.navig").css("background-position", "-100px -100px");								
							prevnext(new_img, a);							
							window.setTimeout( function(){new_img.fadeIn(function(){img.remove();work = false;});}, 100);
							$(el).unbind("load");
						});
					}
				});
			});
}

function click_left(){
												 
	var old_pr = $("div.preview div.pr").not(".nodis");
	var new_pr = $("div.preview div.pr").not(".nodis").prev();
		
	old_pr.fadeOut().attr("class", "pr nodis");
	new_pr.css("display", "inline").attr("class", "pr");
			
	if (new_pr.prev().not(".left-prev").length) $("div.zapl-left").after('<a href="javascript:void(0)" onclick="click_left();" class="left-prev"><img src="img/left-small.gif" /></a>').remove();
	else $("a.left-prev").after('<div class="zapl-left"><img src="img/zapl.gif"/></div>').remove();
			
	if (new_pr.next().not(".right-prev").length) $("div.zapl-right").after('<a href="javascript:void(0)" onclick="click_right();" class="right-prev"><img src="img/right-small.gif" /></a>').remove();
	else $("a.right-prev").after('<div class="zapl-right"><img src="img/zapl.gif"/></div>').remove();			
	
	return false;
}
		
function click_right(){	

	var old_pr = $("div.preview div.pr").not(".nodis");
	var new_pr = $("div.preview div.pr").not(".nodis").next();

	old_pr.fadeOut().attr("class", "pr nodis");
	new_pr.css("display", "inline").attr("class", "pr");
					
	if (new_pr.prev().not(".left-prev").length) $("div.zapl-left").after('<a href="javascript:void(0)" onclick="click_left();" class="left-prev"><img src="img/left-small.gif" /></a>').remove();
	else $("a.left-prev").after('<div class="zapl-left"><img src="img/zapl.gif"/></div>').remove();

	if (new_pr.next().not(".right-prev").length) $("div.zapl-right").after('<a href="javascript:void(0)" onclick="click_right();" class="right-prev"><img src="img/right-small.gif" /></a>').remove();
	else $("a.right-prev").after('<div class="zapl-right"><img src="img/zapl.gif"/></div>').remove();			
	
	return false;
}

function get_left(el){ 
	
	var offsetLeft = 0;

	el.each(function(){
		element = this;
		do {
			offsetLeft  += element.offsetLeft;
		} while (element = element.offsetParent);
	});
	
	return offsetLeft;
}

function open_popup(data, opt){
	if (opt == 'foto') {
		$("div.main").append('<div class="popup"><a class="close-img" href="/"><img alt="" src="img/close-img.gif"/></a><div class="image" style="background: url(img/load.gif) no-repeat center center;"><img src="" /></div><div class="popup-copy">©  2008-2010 Wowshot Photography. All rights reserved.</div></div>');
		$("div.image img").attr("src", data).hide().css("visibility", "visible").each(function(e) {
			if (this.complete) {
				img = $(this);
				img.css({'top':((540 - img.height())/2), 'left':((810 - img.width())/2)}).fadeIn();
			} else {
				$(this).bind("load", function(e) {
					var img=$(e.target);
					img.css({'top':((540 - img.height())/2), 'left':((810 - img.width())/2)}).fadeIn();
					img.unbind("load");
				});
			}
		});
	}
	
	$("a.close-img").click(function(){
		$("div.popup").fadeOut(function(){$("div.popup").remove();});
		return false;
	});
}
