// JavaScript Document
var objLiHovered = null;
var mLogoAnim = 1;
var mAnimationDone = false;

function checkHover() {
  if(objLiHovered/* && !lSubIn*/) {
    objLiHovered.find('ul').fadeOut('fast');
    objLiHovered = null;
  }
}

$(document).ready(
		function(){
			
			$('#header_pics ul').fadeIn(500);
		  
			$('div#header_pics ul').innerfade({
				speed: 'slow',
				timeout: 5000,
				type: 'sequence',
				containerheight: '202px'
			});
			
			$('#gallery_pics').innerfade({
				speed: 'fast',
				timeout: 5000,
				type: 'sequence',
				containerheight: '140px'
			});
			
			$('#webcams').innerfade({
				speed: 2000,
				timeout: 5000,
				type: 'sequence',
				containerheight: '140px'
			});
			
			$('#ondisplay ul').innerfade({
				speed: 'fast',
				timeout: 8000,
				type: 'sequence',
				containerheight: '170px'
			});
			
			//$("#logo").show(0).delay(50).hide(0).delay(50).show(0).delay(50).hide(0).delay(100).show(0).delay(100).hide(0).delay(1500).fadeIn(1000);
			$("#logo").hide().delay(60);
			window.setTimeout(showAnimation, 8000);
			
			// ------------------------------------------------------
			// logo
			$("#top_logo").hover( function() {
			  //$('#logo').clearQueue();
			  if (mLogoAnim == 0) {
			    $('#logo').clearQueue();
  			  $('#logo').fadeOut(200);
  			}
			}, function() {
			  $('#logo').fadeIn(200);
			  $('#logo').animate({opacity: '100'}, 0);
			});
			
			// boards
			$("#boards_home").hover( function() {
			  $('#logo').clearQueue();
			  $('#logo').fadeOut(200);
			}, function() {
			  $('#logo').fadeIn(200);
			  $('#logo').animate({opacity: '100'}, 0);
			});
			
			// ------------------------------------------------------
			// hledani
			$("#searchRight").focus( function() {
			  if ($(this).attr("value") == "Hledat..")
			    $(this).attr("value", "");
			  $('#search').removeClass("input_out").addClass("input_in");
			  }
			);
			
			$("#searchRight").blur( function() {
			  if($(this).attr("value") == "")
			    $(this).attr("value", "Hledat..");
			  $('#search').removeClass("input_in").addClass("input_out");
			});
			
			// ------------------------------------------------------
			// login
			$("#login_name").focus( function() {
			  if ($(this).attr("value") == "uživatelské jméno")
			    $(this).attr("value", "");
			  $(this).removeClass("grey");
			  }
			);
			
			$("#login_name").blur( function() {
			  if($(this).attr("value") == "") {
			    $(this).attr("value", "uživatelské jméno");
		  	  $(this).addClass("grey");
		  	}
			});
			
			// ------------------------------------------------------
			// HESLO
			$("#login_pswd").focus( function() {
			  if ($(this).attr("value") == "hesloooo")
			    $(this).attr("value", "");
			  $(this).removeClass("grey");
			  }
			);
			
			$("#login_pswd").blur( function() {
			  if($(this).attr("value") == "") {
			    $(this).attr("value", "hesloooo");
		  	  $(this).addClass("grey");
		  	}
			});
			
			// submenu
			$("#main_menu li").hover( function() {
			  $(this).find('ul.submenu').fadeIn("fast");
			}, function() {
			  $(this).find('ul.submenu').fadeOut("fast");
			});
			
			// fb
			$("#fb").hover( function() {
			  $('#fb').animate({right:'+=82'},500);
			}, function() {
			  $('#fb').animate({right:'-=82'},500);
			});
			
			// kschool
			$("#ks_top_butt").hover( function() { $('#ks_top_butt').animate({right:'+=88'},500);
			}, function() { $('#ks_top_butt').animate({right:'-=88'},500);	});
			// kstore
			$("#store_top_butt").hover( function() { $('#store_top_butt').animate({right:'+=88'},500);
			}, function() { $('#store_top_butt').animate({right:'-=88'},500);	});
			// ank
			$("#ank_top_butt").hover( function() { $('#ank_top_butt').animate({right:'+=74'},500);
			}, function() { $('#ank_top_butt').animate({right:'-=74'},500);	});
			
			$('#ks_panel ul').show();
      $('#ks_panel ul').innerfade({
			  animationtype: 'fade',
				speed: 1200,
				timeout: 8000,
				type: 'sequence'
			});
		}
	);
	
	$(window).load(
		function() {showAnimation();}
	);

/**
 *  spusti nacitaci animaci
 */
function showAnimation(){
  if (mAnimationDone)
    return;
  
  mAnimationDone = true;
  
  $("#logo").delay(1000).fadeIn(400).animate({opacity: '100'}, 0).fadeOut(600).delay(200).fadeIn(1000).fadeOut(600).fadeIn(1000);
	$('#logo').queue(function() {
		mLogoAnim = 0;
	});
	
	$("#boards_over").delay(1000).animate({top:'+=128'},1000);
	$("#sports").delay(700).show().animate({top:'-=100'},700);
	$("#ondisplay").delay(500).animate({top:'+=375'},700);
	$("#bottom").addClass("bottom_bg");
	
	showPartners();
}

/**
 *  maximalni delka textarea
 */
function ismaxlength(obj){
  var mlength = obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : 500;
  if (obj.getAttribute && obj.value.length>mlength)
    obj.value=obj.value.substring(0,mlength)
}

/**
 * malej antispam
 */
function reactOnBlurField(obj) {
  var lField = document.getElementById('email1');
  lField.value = "hehe" + obj.value + "haha";
}

/**
 * Vyvola odeslani formulare s akci onchange
 */
function submitOnChange(aFormID, aParam) {
  aParam = aParam || "a";
  var lActionStr = $("#"+aFormID).attr("action");
  var lIndexOf = lActionStr.indexOf("#");
  
  if (lIndexOf > 0)
    lActionStr = lActionStr.substr(0, lIndexOf) + "&onchange=" + aParam + lActionStr.substr(lIndexOf, lActionStr.length);
  else
    lActionStr = lActionStr + "&onchange=" + aParam;
  
  $("#"+aFormID).attr("action", lActionStr	);
  //this.form.action = this.form.action+'&amp;onchange=a';
  document.getElementById(aFormID).submit();
}

/**
 * Vyvola stisknuti tlacitka
 */
function clickButton(aButtName) {
  $('#' + aButtName).click();
}

/**
 * validace cisla
 */
function validateNumber(aField, aDecimal) {
  var lValue = aField.value;
  var lValidChars = '0123456789';
  var lDotUsed = !aDecimal;
  var lOneChar;
  
  for (var i = 0; i< lValue.length; i++) {
    lOneChar = lValue.charAt(i);
    
    if (i > 0 && (i < lValue.length - 1) && !lDotUsed && (lOneChar == '.' || lOneChar == ',')) {
      lDotUsed = true;
      continue;
    }
    
    if (lValidChars.indexOf(lOneChar) == -1) {
      //$("#"+aField.id).focus();
      return false;
    }
  }
  
  return true;
}

/**
 * Skryti zobrazeni poozky
 */
function alterStructure(aEnable, aDivID) {
  if (!aEnable) {
    $("#" + aDivID).hide();
  }
  else {
    $("#" + aDivID).show();
  }
}

/**
 * Vyvola stisknuti tlacitka
 */
function showWebcam(aID) {
  $('#webcams a').hide();
  $('#webcam_texts a').removeClass('in');
  
  $('#cam_' + aID).show();
  $('#wc_' + aID).addClass('in');
  
  return false;
}

/**
 * Zobrazi partners
 */
function showPartners() {
  var xmlhttp;

  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();

	xmlhttp.onreadystatechange=function()	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			$('#forpartners').html(xmlhttp.responseText);
		}
	}
	xmlhttp.open("GET","http://mushow.cz/showPartners.php");
	xmlhttp.send();
}

/**
 * Hlasovani
 */
function submitPoll(aCode) {
  var xmlhttp;

  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();

	xmlhttp.onreadystatechange=function()	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			$('#poll_div').html(xmlhttp.responseText);
			//$('#poll_div input').attr("disabled", true);
		}
	}
	
	xmlhttp.open("POST","http://mushow.cz/processPoll.php");
  xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  
  var item = $("input[name='poll_item']:checked").val();
  
	xmlhttp.send("item=" + item + "&code=" + aCode);
}
