var lduration=500;
//LOGIN
login_duration=200;
lanimate=false;
outdis=false;
loginshow=false;
//END LOGIN
function checkUserSess() {
	var sess = $.cookie("SID");
	if (sess && sess.length==52) {
		checkSess();
	}else{
		//Session not found
		notLogin();
	}
}

function checkSess() {
	$.ajax({
		url: "/ajax/login/",
		cache: false,
		dataType: "xml",
		processData:true,
		success: function(data){
			var status =  $(data).find('status').text();
			if (status=="1") {
				var uname = $(data).find('uname').text();
				loginOk(uname);
			}else{
				notLogin();
			}
			
		}
	});
}

function notLogin() {
	if ($('#loginok').is(":visible")) {
		$('#loginok').animate({
			top: -55
		  }, lduration, function() {
			$('#login_a').show();
			$('#login_a').animate({
				top: -55
			  }, lduration, function() {
				loginshow=true;
				$("#lenter").removeClass("buta");
				$("#lenter").addClass("but");
				lanimate=false;
			});
		});
	}else{
		$('#login_a').show();
		$('#login_a').animate({
			top: -55
		  }, lduration, function() {
			loginshow=true;
			$("#lenter").removeClass("buta");
			$("#lenter").addClass("but");
			lanimate=false;
		});
	}
}

function loginOk(uname) {
	$("#luname").html(uname);
	if ($('#login_a').is(":visible")) {
		$('#login_a').animate({
			top: -95
		  }, lduration, function() {
			loginshow=false;
			$("#lenter").removeClass("but");
			$("#lenter").addClass("buta");
			$(this).hide();
			$('#loginok').show();
			$('#loginok').animate({
				top: 0
			  }, lduration, function() {
				loginshow=false;
				$("#lenter").removeClass("but");
				$("#lenter").addClass("buta");
			});
			lanimate=false;
		});
	}else{
		
		$('#loginok').show();
		$('#loginok').animate({
			top: 0
		  }, lduration, function() {
			loginshow=false;
			$("#lenter").removeClass("but");
			$("#lenter").addClass("buta");
			lanimate=false;
		});
	}
}

function initLoginForm() {
	$("#loginform").submit(function() {
		
		var uname=$(this).find("input[name='username']").val();
		var password=$.md5($(this).find("input[name='password']").val());
		
		$.ajax({
			type: 'POST',
			url: "/ajax/login/",
			data: {'username':uname,'password':password},
			dataType: "xml",
			success: function(data) {
				var status =  $(data).find('status').text();
				if (status=="1") {
					var uname = $(data).find('uname').text();
					loginOk(uname);
					$("#login_links").show();
					$("#login_error").hide();
				}else{
					//notLogin();
					$("#login_links").hide();
					$("#login_error").show();
				}
			}
		});
		return false;
	});
}

function logOut() {
	$.ajax({
		type: 'POST',
		url: "/ajax/login/",
		data: {'delogin':1},
		dataType: "xml",
		success: function(data) {
			notLogin();
		}
	});
	return false;
}


function articleFontSizeToggle() {
	if ($('.article').hasClass('fs12')) {
		$('.article').removeClass('fs12')
	}else{
		$('.article').addClass('fs12')
	}
}


$(document).ready(function() {
	checkUserSess();
	initLoginForm();
	
	
	//LOGIN BLOCK

	$("#lenter").click(
		function() {
			if (!lanimate) {
				lanimate=true;
				if ($('#login_a').position().top==0) {
					$('#login_a').animate({
						top: -55
					  }, login_duration, function() {
						$(this).css("z-index",1);
						$("#lenter").removeClass("buta");
						$("#lenter").addClass("but");
						loginshow=false;
						lanimate=false;
					});
				}else{
					$("#login_a").css("z-index",3);
					$('#login_a').animate({
						top: 0
					  }, login_duration, function() {
						loginshow=true;
						$("#lenter").removeClass("but");
						$("#lenter").addClass("buta");
						lanimate=false;
					});
				}
			}
		}
	);
	
	
	$("#login_a").mouseover(function() {
		if (loginshow) {
			$(this).clearQueue();
		}
	});
	$("#login_a").mouseleave(function() {
		$(this).delay(1500);
		if (!lanimate && !outdis) {
			 $(this).delay(1500).animate({
				top: -55
			  }, login_duration, function() {
				$("#lenter").removeClass("buta");
				$("#lenter").addClass("but");
				lanimate=false;
			});
		}	
	});
	
	$("#login_a input").keyup(function() {
		var uname = $("#login_a input[name='username']").val();
		var psw = $("#login_a input[name='password']").val();
		//console.log("uname: "+uname+" psw: "+psw);
		if ((uname=="") && (psw=="")) {
			outdis=false;
		}else{
			outdis=true;
		}
	});
	
	$("#login_a input").click(function() {
		$(this).val("");
	})
	$("#login_a input").focusin(function() {
		outdis=true;
	});
	$("#login_a input").focusout(function() {
		var uname = $("#login_a input[name='username']").val();
		var psw = $("#login_a input[name='password']").val();
		//console.log("uname: "+uname+" psw: "+psw);
		if ((uname=="") && (psw=="")) {
			outdis=false;
		}else{
			outdis=true;
		}
	});
	
	//END LOGIN BLOCK
	
})

/* FUNCTION */
function getcont(url,oId,hId,this_li) {
	showLoader("#"+oId,true);
	if ($('#'+oId).size()>0) {
		$('#'+oId).load(url,function() {
			if (typeof(init_gal)!="undefined" && init_gal==1) {
                initGal();
				InitializeGallery();
				$("#picture").show();
            }
			showLoader("#"+oId,false);
		});
	}else{
		$('#div_content').load(url,function() {showLoader("#"+oId,false);});
	}
	if (this_li) {
		$(this_li).parents("ul").find("li.active").removeClass("active");
		$(this_li).parents("ul").find("a.active").removeClass("active");
		$(this_li).parent().find("a.active").removeClass("active");
		$(this_li).addClass("active");
	}
	
	return false;
}

function showLoader(osel,enable) {
	/*if (enable) {
		$(osel).css("position","relative");
		if (!$(osel).children("div.ajaxloader").size()>0) {
			$(osel).children("div.ajaxloader").show();
		}else{
			$(osel).append('<div class="ajaxloader"><img src="/data/img/ajax-loader.gif"/></div>');
		}
	}else{
		$(osel).children("div.ajaxloader").hide();
	}
	*/
	
}
/*END FUNCTION */

/* ARCHIVE MATCHES */
var liGoWork=false;
function liGo(direction,contsel,dynsel,a) {
	var count_li = $(dynsel).size()-3;
	var li_width = $(dynsel).width();
	if (!liGoWork) {
		if (direction>0) {
			//RIGHT
			var curleft = $(contsel).position().left;
			if (Math.abs(curleft)<(count_li*li_width)) {
				var nextpos_num = Math.floor(Math.abs(curleft)/li_width)+1;
				liGoWork=true;
				$(contsel).animate({
					left: li_width*nextpos_num*(-1)
				  }, 300, function() {
					$(a).parent().find(".cont_menu_left_na").removeClass("cont_menu_left_na");
					if (nextpos_num>=count_li) {
						$(a).addClass("cont_menu_right_na");
					}else{
						$(a).removeClass("cont_menu_right_na");
					}
					liGoWork=false;
				});
			}
		}else{
			//LEFT
			var curleft = $(contsel).position().left;
			if (Math.abs(curleft)>0) {
				var nextpos_num = Math.floor(Math.abs(curleft)/li_width)-1;
				liGoWork=true;
				$(contsel).animate({
					left: li_width*nextpos_num*(-1)
				  }, 300, function() {
					$(a).parent().find(".cont_menu_right_na").removeClass("cont_menu_right_na");
					if (nextpos_num<=0) {
						$(a).addClass("cont_menu_left_na");
					}else{
						$(a).removeClass("cont_menu_left_na");
					}
					liGoWork=false;
				});
			}
		}
	}
}

function mplayfile(furl,imgurl,autoplay,comment,arcname,arcurl,arcsize) {
	 flashembed("player", 
		{
			src:'/data/flash/flowplayer/FlowPlayerDark.swf',
			width: 335, 
			height: 251
		},
		{config: {   
			autoPlay: autoplay,
			autoBuffering: false,
			initialScale: 'scale',
			videoFile: furl,
			showVolumeSlider: true,
			splashImageFile: imgurl,
			controlsOverVideo: 'ease',
			controlBarBackgroundColor: -1,
			controlBarGloss: 'low',
			showWatermark:'always',
			loop:false
		}} 
	);
	document.getElementById("h2").innerHTML=comment;
	if (arcname!='0') {
		document.getElementById("arcdiv").setAttribute("style", "margin:6px 0 0 10px");
		document.getElementById("archref").setAttribute("href", arcurl);
		document.getElementById("archref").setAttribute("title", "Download "+arcname+" "+arcsize+" Mb");
		document.getElementById("arcsize").innerHTML=arcsize;
	}
	else {
		document.getElementById("arcdiv").setAttribute("style", "display:none");
	}
	//document.getElementById("arcsize").innerHTML=arcsize;
	//if (arcsize>0){arcsize="arcsize"}
	}


/*END ARCHIVE MATCHES */

/* QUESTION */
function post_quest(form,osel) {
	$.ajax({
		type: 'POST',
		url: $(form).attr("act"),
		data: $(form).serialize(),
		dataType: "html",
		success: function(data) {
			$(osel).html(data);
		}
	});
	return false;
}
/*END QUESTION */

/*MAIN INDEX NEWS */
function selnewstab(selid,a) {
	$(a).parent().find("a.taba").addClass("tab").removeClass("taba");
	$(a).addClass("taba").removeClass("tab");
	$("#"+selid).parent().children("div").hide();
	$("#"+selid).show();
}
/*END MAIN INDEX NEWS */

/* FEEDBACK */
function postFeedback(form,osel,fbsel) {
	$.ajax({
		type: 'POST',
		url: "/ajax/feedback/",
		data: $(form).serialize(),
		dataType: "html",
		success: function(data) {
			$(fbsel).show()
			$(osel).hide();
		}
	});
	return false;
}
function clInp(inp) {
	if ($(inp).attr("clear")!=1) {
		$(inp).val('');
		$(inp).attr("clear",1);
	}
}
/* END FEEDBACK */

/*CHECK USERNAME*/
function checkUname() {
	$.ajax({
		type: 'POST',
		url: "/ajax/checklogin/",
		data: {username: $("#username").val()},
		dataType: "xml",
		success: function(data) {
			var status =  $(data).find('out_exist_user').text();
			if (status=="false") {
				$("#nonexist_uname").show();
				$("#exist_uname").hide();
			}else{
				$("#nonexist_uname").hide();
				$("#exist_uname").show();
			}
		}
	});
}
/*END CHECK USERNAME*/


/* MAIN PAGE PLAYER ROTATION */
var imgar = new Array();
	imgar[0] = "player75_1961.jpg";
	imgar[1] = "player75_1961_1.jpg";
	imgar[2] = "player75_1962.jpg";
	imgar[3] = "player75_1980.jpg";
	imgar[4] = "player75_1984.jpg";
	imgar[5] = "player75_1995.jpg";
	imgar[6] = "player75_2008.jpg";
	imgar[7] = "player75_2009.jpg";
	imgar[8] = "player75_2009.jpg";

jQuery.extend({
	random: function(X) {
	    return Math.floor(X * (Math.random() % 1));
	},
	randomBetween: function(MinV, MaxV) {
	  return MinV + jQuery.random(MaxV - MinV + 1);
	}
});
	
function changePlayerRandom() {
	var num = $.random(8);
	$("#playerdiv").css("background-image","url(http://shakhtar.com/data/img/pl75/"+imgar[num]+")");
}	
/*END MAIN PAGE PLAYER ROTATION */

