// JavaScript Document
var hidDiv = null;

function getOffset(child, parent) {
	var offset = {
		top: 0,
		left: 0
	},
	top = 0, left = 0;

	while (child && child != parent) {
		top = top + parseInt(child.offsetTop);
		left = left + parseInt(child.offsetLeft);
		child = child.offsetParent;
	}	
	
	if (child != parent) {
		return {
			top: undefined,
			left: undefined
		}
	}

	return {
		top : top,
		left : left
	}
}

function checkImages(slider, newOffsetLeft) {
	var viewport = slider.parents(".slide_container:first"),
		viewport_offsetWidth = viewport.attr("offsetWidth"),
		slider_domElement = slider[0];
	
	slider.find("img").map(function () {
		var offset = getOffset(this, slider_domElement),
			diff = newOffsetLeft + offset.left;
		if (diff + this.offsetWidth > 0 && diff < viewport_offsetWidth) {
			$(this).trigger("lazyload");
		}
	});
}

$(function(){
	$(".slider").each(function(){
		var width = 0;
		$(this).find(".column").each(function(){
			width = width*1 + ($(this).css("width").replace("px",""))*1+28;
		});
		$(this).css("width",width+"px");
	});

	showHideControls();


	$(".slide_control .left").click(function(){
		var newleft;
		var parent = $($(this).parents(".slide_container").get(0));
		var slider = parent.find(".slider:first");
		slider.stop(true,true);
		var left = slider.css("left").replace("px","");
		var width = slider.css("width").replace("px","");
		if(left < 0){
			newleft = left*1+952;
		}
		else{
			newleft = left;
		}
		//salert(newleft);

		var maxLeft = 0;
		if (newleft > maxLeft) {
			newleft = maxLeft;
		}

		showHideControls(newleft+"px", parent);
		checkImages(slider, newleft);
		slider.animate({left:newleft+"px"},300);
	});

	$(".slide_control .right").click(function(){
		var newleft;
		var parent = $($(this).parents(".slide_container").get(0));
		var slider = parent.find(".slider:first");
		slider.stop(true,true);
		var left = slider.css("left").replace("px","");
		var width = slider.css("width").replace("px","");
		//alert(slider.css("width"));
		if(left*1 + (width-952) >= 3){
			newleft = left-952;
		}
		else{
			newleft = left;
		}
		var minLeft = 952 - width;
		if (newleft < minLeft) {
			newleft = minLeft;
		}
		showHideControls(newleft+"px", parent);
		checkImages(slider, newleft);
		slider.animate({left:newleft+"px"},300);
	});
});


function hideContent(){
    
	hidDiv = document.createElement("div");
	hidDiv.id = "hidder";
	hidDiv.className = "contentHider";
	hidDiv.innerHTML = '<div style="text-align:center; color:#ffffff; position:fixed; left:50%; top:50%; margin: -42px 0px 0px -143px;"><img src="/images/sun_club_logo.png" width="285" height="84" border="0" alt=""  /><br /><img src="/images/loader.gif" hspace="10" width="16" height="11" border="0" alt="" />Загрузка...</div>';
	document.body.appendChild(hidDiv);
}


function showContent(){
	$("#hidder").hide();
}

function showHideControls(newleft, cont){
	cont = cont || document.documentElement;
	$(".slide_container").each(function(){
		var par = $(cont);
		var slider = par.find(".slider:first");
		var width = slider.css("width").replace("px","");
		var left = newleft ? newleft.replace("px","") : slider.css("left").replace("px","");
		
		if(width<=955){
			par.find(".slide_control .left").hide();
			par.find(".slide_control .right").hide();
		}
		else{
			par.find(".slide_control .left").show();
			par.find(".slide_control .right").show();
		}
		if(left>=0){
			par.find(".slide_control .left").hide();
		}
		if(left*1+width*1 <= 953){
			par.find(".slide_control .right").hide();
		}
	});
}


function showImages(){
	
}

$(function () {
	$(".slider").find("img").lazyload({
		placeholder: "/upload/1.gif",
		event: "lazyload"
	}).end().map(function() {
		checkImages($(this), 0);
	});
//	console.log(window.location.href);
	if(window.location.href == 'http://solnceclub.com/'){window.setTimeout(function() { $('html, body').animate({ scrollTop: 650+'px' }); }, 3000);}	
});

$(function () {
	$('.pr-voting a').live("click", function(){
		var cl = this.id,
			params = this.id.split("-"),
			th = $(this);
		$('.pr-voting a').remove();
		$('.' + cl).html('<img src="/images/loader-tr.gif">');
		$.ajax({
			url: "/projects/ajax.php",
			type: "POST",
			data: {
				id: params[1],
				section: params[0]
			},
			success: function(answer) {
				if (answer > 0) {
					$('.' + cl).text("**");
				}
			}
		});
	});
});
