var gfxIndicator = '<img id="progress_img" src="img/indicator.gif" width="16" height="16" alt="" /> ';

// title -> top content functions

 $(document).ready(function(){
   $(".top-video").click(function(e)
	{
	  var disclaimer = readCookie('disclaimer');
	  if (disclaimer == 1)
	  {
		$(".top-content-header li").removeClass("active");
		$(this).addClass("active");
		$("#game_top").hide();
		$("#background_top").hide();
		$("#midi_top").hide();
		$("#video_top").show();
	  }
	  else
	  {
		if (confirm("Obsah, ktorý chcete zobraziť, je erotického charakteru a potvrdením voľby OK súhlasíte so všetkými nasledujúcimi podmienkami a zároveň potvrdzujete, že:\n\n" +
					"* ste starší ako 18 rokov\n" +
					"* ak sa nachádzate mimo územia Slovenskej republiky alebo ste štátnym občanom inej krajiny, spĺňate podmienky plnoletosti a iné vyžadované podmienky pre prezeranie pornografických alebo erotických materiálov\n" +
					"* na nasledujúce stránky vstupujete dobrovoľne a ste si vedomí skutočnosti, že materiál na nasledujúcich stránkach je erotického alebo pornografického charakteru\n" +
					"* sexuálne orientovaný materiál a materiál s pornografickým obsahom Vás neuráža, nepohoršuje a neumožníte prístup k týmto stránkam osobám mladším ako 18 rokov\n" +
					"* materiál využijete pre osobnú potrebu\n" +
					"* neposkytnete, nebudete rozširovať, vystavovať, zverejňovať, neprenecháte ani inak nesprístupníte ani nebudete ponúkať materiály osobám, ktoré nespĺňajú ktorúkoľvek z vyššie uvedených podmienok\n\n"))
		{
		  $(".top-content-header li").removeClass("active");
		  $(this).addClass("active");
		  $("#game_top").hide();
		  $("#background_top").hide();
		  $("#midi_top").hide();

		  $("#video_top").show();
		  setDisclaimer(1);
		}
	  }
   });

   $(".top-hry").click(function(e){
	$(".top-content-header li").removeClass("active");
	$(this).addClass("active");
	$("#video_top").hide();
	$("#background_top").hide();
	$("#midi_top").hide();
	$("#game_top").show();
   });

   $(".top-pozadia").click(function(e){
	$(".top-content-header li").removeClass("active");
	$(this).addClass("active");
	$("#video_top").hide();
	$("#game_top").hide();
	$("#midi_top").hide();

	$("#background_top").show();
   });

   $(".top-midi").click(function(e){

	  $(".top-content-header li").removeClass("active");
	  $(this).addClass("active");
	  $("#video_top").hide();
	  $("#background_top").hide();
	  $("#game_top").hide();

	  $("#midi_top").show();

   });

// phones brand selections
   $("#panel a").click(function(e){
	//$("telefon") = $(this).attr('id');
	// get phone name
	var telefon = $(this).attr('id');

	// fill select with content
	$("#mobile_type").html("" + eval(telefon));

	// create cookie
	createCookie('c2_makerid', telefon, 1);
   });

   // vyber button
   $("#phone_pick").click(function(){
	if ($("#mobile_type option:selected").attr("value") == 0)
	{
	  eraseCookie("c2_pid");
	}
	else
	{
	  createCookie('c2_pid',$("#mobile_type option:selected").attr("value"),1);
	}

	window.location.replace(removeParam(window.location, 'page'));

	return false;

   });

   $(".panel-button").click(function(){
	  var c2_pid = readCookie('c2_pid');
	  var maker = readCookie('c2_makerid');
	  var telefon = maker;
	  $.tel_makerid =readCookie('c2_makerid');
	  $.tel_pid = readCookie('c2_pid');
	 // alert(parseInt($.tel_pid));
	  if ($.tel_pid == 0 || $.tel_pid == null)
	  {
		eraseCookie("c2_pid");
		$("#mobile_type").html("<option>Zvoľ typ telefónu</option>");
	  }
	  else
	  {
		$("#mobile_type").html("" + eval(telefon));
	    $("#mobile_type option[value='" + $.tel_pid +"']").attr("selected", "selected");
	  }
   });

   $("#supported_phones").click(function(){
		$('#panel').show('slow');
		$("#phone_info").show();
	  });

  $("#supported_phones_in_detail").click(function(){
	$('#panel').show('slow');
	$("#phone_info").show();
  });

   // feature image
   $(".featured-paging li").click(function(e){
	$(".featured-paging li").removeClass("active");
	$(this).addClass("active");

	// hide all images
	$.id = $(this).text();
	//alert($(id));
	$(".featured").hide();
//	$("#feature-" + $.id).fadeOut("slow");
	$("#feature-" + $.id).fadeIn("slow");

	//alert($(this).text());
	//$("#game_top").hide();
	//$("#background_top").hide();
	//$("#video_top").show();
   });

   	// feature images slideshow

   	//feature_slideshow();
	setTimeout("feature_slideshow()",10000);
   	// -------------------------------
  // send text by pressing key enter
  $("#chat_text").keypress(function(e) {
	if(e.which == 13)
	{
	  send_message();
	}
  });


  // validation form data for terms agree

  $("#terms_form").submit(function() {

	if ($("#suhlas1").val() == "on" && $("#suhlas2").val() == "on")
	{
		return true;
	}
	else
	{
		alert("Musíte súhlasiť s podmienkami Coolmobil.sk a Zoznam.sk")
		return false;
	}
  	return false;

   });


	$("#udaje_tooltip").tooltip("#demotip");


});

// slideshow
function feature_slideshow()
{
	// number of elements for slideshow
	var element_counts = $(".featured-paging li").length;

	// get active element
	$.id = $(".featured-paging .active").text();
	if ($.id >= 1 && $.id <= element_counts && $.id <= 5)
	{
		if ($.id == element_counts)
		{
			$.id = 1;
		}
		else
		{
			$.id++;
		}
	}
	else
	{
		$.id = 1;
	}
	// li uses index -> id - 1 is the index value
	$.id_li = $.id - 1;
	$(".featured-paging li").removeClass("active");
	$(".featured-paging li:eq(" + $.id_li + ")").addClass("active");

	// hide all images
	$(".featured").hide();

	$("#feature-" + $.id).fadeIn("slow");
	setTimeout("feature_slideshow()",10000);
}


/* COOKIES */
function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


function removeParam(url,param) {
	myLocation = new String(url);
	re = /page=\d+&?/gi;
	newLocation = myLocation.replace(re, '');
	return newLocation;
}

/* AJAX Request */

var http = false;
var pwstatus = false;

// http object initialize
if(navigator.appName == "Microsoft Internet Explorer")
{
  http = new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
  http = new XMLHttpRequest();
}

// ajax http object

function GetXmlHttpObject()
{
  if (window.XMLHttpRequest)
  {
	// code for IE7+, Firefox, Chrome, Opera, Safari
	return new XMLHttpRequest();
  }
  if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

// state change function
function stateChanged()
{
  if (http.readyState==4)
  {
	try
	{
	  //var license = http.responseXML.getElementsByTagName('wmrm')[0];
	  //document.netobj.StoreLicense(license.firstChild.xml);
	  //window.location="?download=" + document.predeliver.track_id.value;
	}
	catch(err)
	{
	}
  }
}

// send message to chat windows
function send_message()
{
	if (http==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	$("#progress").html(gfxIndicator);
	$("#progress_img").show();
	var params = "action=chat_message" + "&text=" + escape($("#chat_text").val()) + "&ajax=1";

	http.abort();

	var url="index?action=chat_message&ajax=1";
	http.open("POST", url, true);

	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");

	http.onreadystatechange=function()
	{
	if(http.readyState==4)
	  {
		$("#chat").html(http.responseText);
		$("#progress_img").hide();
		$("#chat_text").val('');
	  }
	}
	http.send(params);
}

function refresh_chat()
{
  if (http==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	$("#progress").html(gfxIndicator);
	$("#progress_img").show();

	http.abort();

	var params = "action=chat_message" + "&ajax=1";
	var url="index?action=chat_message&ajax=1";
	http.open("POST", url, true);

	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");

	http.onreadystatechange=function()
	{
	if(http.readyState==4)
	  {
		$("#chat").html(http.responseText);
		$("#progress_img").hide();
	  }
	}
	http.send(params);

	setTimeout("refresh_chat()", 10000);
}
// refresh of chat window
// setTimeout("refresh_chat()", 5000);


/* DISCLAIMER */
function setDisclaimer(bool) {
    if (bool) {
        createCookie('disclaimer',1,1);
        return true;
    } else {
        return false;
    }
}

function showRestricted() {
    setDisclaimer(1);
    displayElement(getElement('disclaimer'),0);
    displayElement(getElement('restrictedContent'),1);
}

/* JAVA TRAILER */
function showTrailer(mid) {
    document.getElementById('game_trailer').innerHTML = '<img src="down/ja/' + mid + '/' + mid + '_trailer.gif">';
}

function displayFintu() {
	getElement('post-form').innerHTML += '<inp'+'ut typ'+'e="hi'+'dden" nam'+'e="fi'+'nta" va'+'lue="zapn'+'uta">';
}

function getElement(el) {
	if (document.getElementById) {
		el = document.getElementById(el);
	} else if (document.all) {
		el = document.all.item(el);
	} else {
		el = null;
	}
	return el;
}
