$(document).ready(function(){
	$("*[id^='box-']").live ("click", function(e){
		// type and var //
		$boxArr = $(this).attr("id").split("-");
		$boxId = Number($boxArr[1]);
		$type = $boxArr[2];
		$var = $boxArr[3];
		// type cases //
		if ($type == "tender") {
			$("#c-name, #c-photo, #c-image").show();
			$("#c-text, #c-text-h, #smiles-link, #c-send").hide();
		}
		if ($type == "photo") {
			$("#c-text, #smiles-link, #c-send").show();
			$("#c-name, #c-name-h, #c-photo, #c-image, #c-image-h").hide();			
		}
		if ($type == "book") {
			$("#c-text, #smiles-link, #c-send").show();
			$("#c-name, #c-name-h, #c-photo, #c-image, #c-image-h").hide();			
		}
		if ($type == "cloud") {
			if ($var == "add") $("#c-name").show();
			else $("#c-name, #c-name-h").hide();
			$("#c-photo, #c-image, #c-image-h").hide();
			$("#c-text, #smiles-link, #c-send").show();
		}
		if ($type == "web") {
			$("#c-text, #smiles-link, #c-send").show();
			$("#c-name, #c-name-h, #c-photo, #c-image, #c-image-h").hide();
		}
		// var cases ///////////////////////////////////////////
		if ($var == "add") $("#c-send").val("Добавить");
		else if ($var == "comment") $("#c-send").val("Комментировать");
		$boxid = $(this).attr("id");
		$("#"+$boxid).cboxPos();
		$("#c-box").show();
		return false;
	});
	$.fn.cboxPos = function(){
		$(this).Coords().Sizes();
		$boxX = $X;
		$boxY = $Y;
		$boxW = $W;
		$("#c-box").Coords().Sizes();
		$cboxH = $H;
		$cboxW = $W;
		if ($boxY<$cboxH) {
			$Y = $boxY+30;
			$("div.tale-br, div.tale-bl, div.tale-tl").hide();
			$("div.tale-tr").show();
		} else {
			$Y = $boxY-($cboxH+15)
			$("div.tale-br").show();
			$("div.tale-tr, div.tale-tl, div.tale-bl").hide();
		}
		if ($boxX<$cboxW) {
			$X = $boxX;
			$("div.tale-tr, div.tale-tl, div.tale-br").hide();
			$("div.tale-bl").show();
		} else {
			$X = ($boxX+$boxW)-$cboxW;
			$("div.tale-br").show();
			$("div.tale-tr, div.tale-tl, div.tale-bl").hide();
		}
		$("#c-box").css({"left":+$X+"px", "top":+$Y+"px"});
		return this;
	}
	
	$("#c-name, #c-text, #c-image").focus(function(){
		$(this).errClear();
	});
	$("#c-send").live ("click", function(){
		$error = 0;
		$("#c-name").errCheck();
		if (!$error) $("#c-text").errCheck();
		if (!$error) {
			$cname = $("#c-name").val();
			$ctext = $("#c-text").val();
			$postdata = "ajax=1&id="+$boxId+"&type="+$type+"&var="+$var+"&name="+$cname+"&text="+$ctext;
			$url = document.location.href;
			$(this).hide();
			$.post($url, $postdata, function(data){
				if ($var == "add") $("#content").html(data);
				else if ($var=="comment") $("#"+$type+"-"+$var+"-"+$boxId).html(data);
				$("#c-box").hide();
				$("#c-name, #c-text").val("")
				$(this).show();
			});
		}	
	});
	$("#c-photoform").submit(function(){
		$error = 0;
		$("#c-name").errCheck();
		if (!$error) $("#c-image").errCheck(); else $send = false;
		if (!$error) $send = true; else $send = false;
		return $send;
	})
		
	$("*[id^='vote-']").click(function(){
		$voteArr = $(this).attr("id").split("-");
		$voteId = $voteArr[1];
		$postdata = "ajax=1&vote="+$voteId;
		$url = document.location.href;
		$.post($url, $postdata, function(data){
			$("#block-vote").html(data);
		});
		return false;
	});
	$("*[id^='tender-']").click(function(){
		$(this).attr("disable", "disable");
		$tenderArr = $(this).attr("id").split("-");
		$tenderId = $tenderArr[1];
		$one = $("*[id^='home']").val();
		$two = $("*[id^='guest']").val();		
		$postdata = "ajax=1&tender="+$tenderId+"&one="+$one+"&two="+$two;
		$url = document.location.href;
		$.post($url, $postdata, function(data){
			$("#block-tender").html(data);
		});
		return false;
	});
	$("*[id^='exit-']").click(function(){
		$exitArr = $(this).attr("id").split("-");
		$exitK = $exitArr[1];
		$exitUser = $exitArr[2];
		$postdata = "ajax=1&logout=1&k="+$exitK+"&user="+$exitUser;
		$url = document.location.href;
		$.post($url, $postdata, function(data){
			$("#user-box").html(data);
		});
		return false;
	});
	// balls rate //
	$("img[id^='ball-']").live ("click", function(){
		$IdballArr = $(this).attr("id").split("-");
		$ballNum = Number($IdballArr[1]);
		$ballImgId = Number($IdballArr[2]);
		$("#balls-"+$ballImgId).slideUp();
		post_data = "ajax=1&ball="+$ballNum+"&photo="+$ballImgId;
		$url = document.location.href;
		$.post($url, post_data, function(data){
			$("#balls-"+$ballImgId).html(data).slideDown();
		});		
	});
	
	$("div.vote-ball").live ("mouseover", function(){
		$bT = 1;
		$(this).checkBall();
		if ($bD == "ball") {
			$(this).clearBall().markBall();
			var j=1;
			$(this).siblings("div.vote-ball").each(function(i){
				if (j>=$bN && j<=5) $bT = 2;
				else $bT = 1;
				$(this).clearBall().markBall();
				j+=1;
			});
		}		
	});
	$("div[id^='balls-']").live ("mouseout", function(){
		$(this).find("div.vote-ball").each(function(i){
			$(this).checkBall();
			$bT = $bA;
			$(this).clearBall().markBall();
		});	
	});
	// log and pas //
	$("input.submit-btn").click(function(){
		$url = "http://fan.chernomorets.odessa.ua/forum/index.php?act=Login&CODE=01&CookieDate=1";
		$logval = $("#log-input").val();
		$pasval = $("#pas-input").val();
		if ($logval && $pasval) {
			post_data = "UserName="+$logval+"&PassWord="+$pasval+"&x=0&y=0";
			$.post($url,post_data, function(data){location.reload();});					
		} else if (!$logval) $("#log-input").errCheck();
		else if (!$pasval) $("#pas-input").errCheck();
		return false;
	});
	$("#log-input, #pas-input").focus(function(){
		$(this).errClear();
	});
	$("*.like-link").hover(function(){$(this).addClass("link-over");}, function(){$(this).removeClass("link-over");});
	// number of posts on page //
	$("table.tune input:button").live("click", function(){
		if($("#saynum").val()) {
			$url = document.location.href;
			$count = $("#saynum").val();
			post_data = "ajax=1&count="+$count;
			$.post($url,post_data, function(data){$("#content").hide().html(data).show();});			
		}
	});
	// click check //
	$(document).click (function(e) {
		var $clicked=$(e.target);
		if ($clicked.is("#saynum-show")) {
			$("#saynum-show").hide();
			$("#saynum-edit").show();
			$("#saynum").focus();
		} else if ($("#saynum-edit").is(":visible")) {
			if (!($clicked.is("#saynum") || $clicked.is("#saynum-button"))) {
				$("#saynum-edit").hide();
				$("#saynum-show").show();
			}	
		}
		if ($("#c-box").is(":visible")) {
			if (!($clicked.is("*[id^='box-']") || $clicked.is("*[id^='c-'], div[class^='smiles-']") || $clicked.is("div[class^='smiles-'] > span,table,td > img") || $clicked.is("#c-box > div, img > div, img"))) {
				$("#c-box").hide();
				$("input[id^='c-']").each(function(i) {$(this).errClear();});
			}
		}
		if ($("#enter-box").is(":visible")) {
			if (!($clicked.is("#enter-box > table > td, input") || $clicked.is("#enter-link"))) {
				$("#enter-box, #log-reg-box").toggleClass("invisible");
				$("#log-input, #pas-input").errClear();
			}
		}
		if ($("#ad-ad").is(":visible")) {
			if (!($clicked.is("#ad-ad > div > div, table.ad-tbl > div, td, th, img") || $clicked.is("span.ad-link"))) $("#ad-ad").hide();
		}
		if($("#smiles-box").is(":visible")) {
			if(!($clicked.is("#smiles-link, #smiles-box, #smiles, img[id^='emo-']"))) $("#smiles-box").hide();
			
		}
    });
	// enter press check //
	$(document).bind("keypress keyup", function(e){
		var code = (e.keyCode ? e.keyCode : e.which);
		if (code == 13) {
			if ($("#enter-box").is(":visible")) {
				$url = "http://fan.chernomorets.odessa.ua/forum/index.php?act=Login&CODE=01&CookieDate=1";
				$logval = $("#log-input").val();
				$pasval = $("#pas-input").val();
				if ($logval && $pasval) {
					post_data = "UserName="+$logval+"&PassWord="+$pasval+"&x=0&y=0";
					$.post($url,post_data, function(data){location.reload();});					
				} else if (!$logval) $("#log-input").errCheck(); else if (!$pasval) $("#pas-input").errCheck();
				return false;
			}			
		}		
	});
	$("#enter-link").live("click", function(){
		$("#enter-box, #log-reg-box").toggleClass("invisible");
	});
	
	$("div.home-score > input:text, div.guest-score > input:text").focus(function(){
		if ($(this).val() == "*") $(this).val("");
	});
	$("div.home-score > input:text, div.guest-score > input:text").blur(function(){
		if ($(this).val() == "") $(this).val("*");		
	});
	$("span.ad-link").click(function(){
		$(this).Coords();
		$("#ad-ad").Sizes();
		$("#ad-ad").css({"top":($Y-$H-8)/16+"em","left":($X-50)/16+"em"});
		$("#ad-ad").show();
	});
	$("#ad-ad > div.corns > div.tl-close > img").click(function(){
		$("#ad-ad").hide();
	});
	$("#c-box > div.close-tr > img").click (function(){
		$("#c-box").hide();
	});
	$("#smiles-link").live("click", function(){
		$("#smiles-box").toggle();
	});
	$("img[id^='emo-']").live("click", function(){
		$emo = $(this).attr("alt") + " ";
		$txtval = $("#c-text").val()
		$txtval += $emo;
		$("#c-text").val($txtval).focus();
		$("#smiles-box").hide();
	});
	// functions //
	base();
	$.fn.Coords = function(){
		$coords = $(this).offset();
		$X = $coords.left;
		$Y = $coords.top;		
		return this;
	}
	$.fn.Sizes = function(){
		$W = $(this).width();
		$H = $(this).height();
		return this;
	}
	$.fn.checkBall = function() {
		$IdArr = $(this).find("img").attr("id").split("-");
		$bD = $IdArr[0];		//ball Done
		$bN = Number($IdArr[1]);//ball Num
		$bP = Number($IdArr[2]);//ball Photo
		$bA = Number($IdArr[3]);//ball Act
		return this;
	}
	$.fn.clearBall = function(){
		for(j=1;j<=3;j++) $(this).removeClass("balltype-"+j);
		return this;
	}
	$.fn.markBall = function(){		
		$(this).addClass("balltype-"+$bT);
		return this;
	}
	$.fn.errCheck = function(){
		if ($(this).is(":visible")) {
			$value = $(this).val();
			if ($value) {
				$(this).removeClass("error-field");
				$error = 0;
			} else {
				$(this).addClass("error-field");
				$error = 1;
			}
		}
		return this;
	}
	$.fn.errClear = function(){
		$(this).removeClass("error-field");
		return this;
	}
	$.fn.CBoxSizes = function() {
		$boxOffset = $(this).offset();
		$boxX = $boxOffset.left;
		$boxY = $boxOffset.top;
		$boxW = $(this).width();
		$cboxH = $("#c-box").height();
		$cboxW = $("#c-box").width();
		return this;
	}	
});
$(document).resize(function(){
	base();
});
// base //
var $bodyId = "body";
var $baseId = "base";

function getWindowHeight()
{
	 var windowHeight = 0;
	 if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	 }
	 else {
		if (document.documentElement && document.documentElement.clientHeight) {
		   windowHeight = document.documentElement.clientHeight;
		}
		else {
		   if (document.body && document.body.clientHeight) {
			  windowHeight = document.body.clientHeight;
		   }
		}
	 }
	 return windowHeight;
  }

function base(){
	document.getElementById($baseId).style.marginTop = 0 + "px";
	$winHeight = getWindowHeight();
	$bodyHeight = document.getElementById($bodyId).offsetHeight;
	$baseHeight = document.getElementById($baseId).offsetHeight;
	$difHeight = $winHeight - ($bodyHeight + $baseHeight);	
	if ($difHeight > 0) {
		document.getElementById($baseId).style.marginTop = $difHeight + "px";
	}
}