﻿// IDOS JScript File

var mhpurl = "http://www.atlas.sk/";
function setMHP(othis)
{
	var tdate=new Date();
	var ddate=new Date();
	var hpms = navigator.appVersion.indexOf("MSIE");	
	var hpie5 = (hpms>0) && (parseInt(navigator.appVersion.substring(hpms+5, hpms+7)) >= 5);
	if(hpie5)
	{
		othis.style.behavior='url(#default#homepage)';
		if (!othis.isHomePage(mhpurl))
		{
			othis.setHomePage(mhpurl);
		}
	}
}


function show_content(part)
{
	if(part == 'mapy_route')
	{
		var preview = document.getElementById('mapy_object').style.display = "none";
		var preview = document.getElementById('mapy_route').style.display = "block";		
		var preview = document.getElementById('search-object').checked = "";
		var preview = document.getElementById('search-route2').checked = "checked";
	}
	else if(part == 'mapy_object')
	{
		var preview = document.getElementById('mapy_object').style.display = "block";
		var preview = document.getElementById('mapy_route').style.display = "none";		
		var preview = document.getElementById('search-object').checked = "checked";
		var preview = document.getElementById('search-route2').checked = "";
	}
}


/* return true if MSIE 7.0*/
function IsMSIE7(){
    return ( IsMSIE() && navigator.appVersion.indexOf("MSIE 7.0;") >=0 );
}

//return true if MSIE 6 
function IsMSIE6older(){  
    return ( IsMSIE() && (navigator.appVersion.indexOf("MSIE 6.0;") >= 0 || navigator.appVersion.indexOf("MSIE 5.5;") >= 0 ));
}

//return true if is IE
function IsMSIE(){
    return  navigator.appName === "Microsoft Internet Explorer";
}

/* true pokud se jedna o doubleclick a jde o NS (FF) nebo Operu */
function IsNSOperaDblClick(event) {
    return (event && event.type == 'dblclick' && (NS || OPERA));
}

/* podle operatoru zvysi nebo snizi datum */
function GetCalDate(event, sOperation) { 
    /* pri doubleclick se ve FF a v Opere vyvola 2x onclick a 1x ondblclick a dochazi pak k preskakovani datumu */
    if (IsNSOperaDblClick(event)) return false;
    if (sOperation == "-"){
	 JourneyDateBack();
    }
    else{
	 JourneyDateFore();
    }
    return false;
}

/* podle operatoru zvysi nebo snizi cas o hodinu */
function GetTime(event, sOperation) {
    /* pri doubleclick se ve FF a v Opere vyvola 2x onclick a 1x ondblclick a dochazi pak k preskakovani casu */
    if (IsNSOperaDblClick(event)) return false;
    var sTime = "";
    oEl = document.getElementById("Time");
    if(oEl != null){
	 sTime = TimeParse(oEl.value);
	 sDefTime = ChangeTime(sTime,sOperation);
	 oEl.value = sDefTime;
    }
    return false;
}

// rozparsruje datum, pokud je ok, tak jej vrati jinak vezme datum ulozene v Time, coz je datum posledniho ulozeneho pozadavku
function TimeParse(sTime){
    var iLen = sTime.length;
    var bNum = true;
    var asTime = [];
    asTime = sTime.split(":");
    if (iLen >=3 && iLen <= 5 && asTime.length === 2){
	 // pokud sedi delka a je rozdlene na dve casti
	 for(i = 0; i < asTime.length; i++){
	     for(j = 0; j < asTime[i].length; j++){
		  if (!(asTime[i].charCodeAt(j) >=48 && asTime[i].charCodeAt(j) <= 57 )) {
		      // nejedna se o cislo
		      bNum = false;
		  }
	     }
	 }
    }
    else{
	 bNum = false;
    }
    if(bNum){
	  if (eval (asTime[0]) < 0 || eval(asTime[0]) > 24){
	     bNum = false;
	  }
    }
    if (bNum){
	 // zadany cas je ok
	 return sTime;
    }
    else{
	 // zadany cas neni ok, vrati se posledni OK cas
	 return sDefTime;
    }
}

// nastavi hodnotu casu podle sOperation
// bere zretel zda je nastaven odjezd nebo prijezd
function ChangeTime(sTime,sOperation){
    var iFrom;
    var iTo;
    var asTime = [];
    asTime = sTime.split(":");
    var iHours = eval(asTime[0]);
    oEl1 = document.getElementById("IsDepTime0");
    if (oEl != null && oEl1.checked === true){
	 // je vybran prijezd
	 iFrom = 1;
	 iTo = 24;
	 if ( iHours === 0) {
	     iHours = iTo;
	 }
    }
    else{
	 // iFrom a iTo je nastaveno pro odjezd
	 iFrom = 0;
	 iTo = 23;
	 if ( iHours === 24) {
	     iHours = iFrom;
	 }
    }
    var iNum;
    if (sOperation === "+"){
	 iHours += 1;
    }
    else{
	 if (asTime[1] == "00" || asTime[1] == "0") iHours -= 1;
    }
    if (iHours < iFrom){
	 iHours = iTo;
    }
    if (iHours > iTo){
	 iHours = iFrom;
    }
    asTime[0] = iHours;
    asTime[1] = "00";
    return asTime[0] + ":" + asTime[1];
}


/*
function save(c_name,value)
{
var exdate=new Date();
expiredays = 90;
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
*/

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);
}

var cached_lookups = {};
function $fast(query) {
	//alert(query);
	if(cached_lookups[query] == null) {
		cached_lookups[query] = {'result':$(query), 'saves':0};
	} else
		cached_lookups[query].saves++;
	
	return cached_lookups[query].result;
}

function alert_cached_saves() {
	$.each(cached_lookups, function(query, cached) { alert('' + query + ': ' + cached.saves);});
}

function horoskop_show(sign) {
	$fast("#moje-sluzby-horoskop .signs").hide();
	$fast("#sign-" + sign).show();
	$fast("#sign-" + sign + " .save").show();
	return false;
}

function horoskop_all() {
	$fast("#moje-sluzby-horoskop .sign").hide();
	$fast("#moje-sluzby-horoskop .signs").show();
	//$fast("#sign-" + sign + " .save").show();
	return false;
}

function horoskop_save(sign) {
	createCookie('MS_HSKP', sign, 90);
	$fast("#sign-" + sign + " .save").hide();
	return false;
}

function tvprogram_show(selection) {
	$fast("#moje-sluzby-program .selection").hide();
	$fast("#" + selection).show();
	$fast("#tvp_reset").show();
	return false;
}

function tvprogram_tab(selection, program) {
	if($fast("#tvtab-" + program).hasClass("ui-tabs-selected"))
		return false;
	
	$fast("#" + selection + " .tab").hide();
	$fast("#" + selection + " li").removeClass("ui-tabs-selected");
	$fast("#tv-" + program).show();
	$fast("#tvtab-" + program).addClass("ui-tabs-selected");
	$fast("#" + selection + " .save").show();
	$fast("#" + selection + " .save").unbind();
	$fast("#" + selection + " .save").click(function(){return tvprogram_save(selection, program)});
	//$fast("#" + selection + " .save").attr('onclick', "return tvprogram_save('"+selection+"', '"+program+"');");
	return false;
}

function tvprogram_all() {
	$fast("#moje-sluzby-program .programs").hide();
	$fast("#moje-sluzby-program .selection").show();
	return false;
}

function tvprogram_save(selection, program) {
	createCookie("MS_TVP", selection+"/"+program, 90);
	$fast("#" + selection + " .save").hide();
	return false;
}

function tvprogram_reset() {
	eraseCookie("MS_TVP");
	$fast("#tvp_reset").hide();
	return false;
}

function logincheck() {
	var name = $fast('#email-name').attr('value');
	var domain = $fast('#email-domain').attr('value');
	var action = 'http://registracia.atlas.sk/verify.aspx';
	if(domain == '@pobox.sk')
		action = 'http://registracia.pobox.sk/verify.aspx';
	$fast('#login-form').attr('action', action);
	$fast('#login-name').attr('value', name + domain);
	return true;
}

function catalog_open() {
	// Hide all services and display companies
	$fast('#all-services').hide();
	$fast('#services').fadeTo(300, 1.0);
	$fast('#vsetky').show("slide", { direction: "left" }, 600);
	$fast('#companies-display-all').fadeOut(300);
	return false;
}

function catalog_close() {
	$fast('#vsetky').hide("slide", { direction: "left" }, 600);
	$fast('#companies-display-all').fadeIn(300);
	return false;
}

function services_open() {
	$fast('#vsetky').hide();
	$fast('#companies-display-all').fadeIn(100);
	$fast('#all-services').show("slide", { direction: "left" }, 600);
	$fast('#services').fadeTo(300, 0.1);
	return false;
}
function services_close() {
	$fast('#all-services').hide("slide", { direction: "left" }, 600);
	$fast('#services').fadeTo(300, 1.0);
	return false;
}

function register_letter_load () {
	$('#companies .all .nav a').each(function (idx) {
	     var letter = $(this).attr('href').replace('#', '').replace('firmy-', '');
	     if (letter !== 'top') {
		  $fast("#letter-" + letter).click(function() {return lazy_load_letter(letter)});
	     }
	});
}

var loaded_letters = {};
function lazy_load_letter(letter) {
	if(loaded_letters[letter] == null) {
		$fast("#firmy-" + letter).load("/firmy/" + letter + ".html", function(){letter_loaded(letter)});
	}
	return false;
}

function letter_loaded(letter) {
	loaded_letters[letter] = true;
	letter_init_carousel(letter);
}

function letter_init_carousel(letter) {
	insertCarousel(letter);
}

function insertCarousel(letter) {
	var carousel = $fast("#firmy-" + letter + ' .carousel'); //$("#companies .all .carousel:visible");
	if (!carousel.hasClass('inserted')) {
		carousel.jcarousel({
			scroll: 1,
			initCallback: letter_carousel_initCallback,
			itemVisibleInCallback: letter_carousel_itemVisibleInCallback,
			buttonNextHTML: null,
			buttonPrevHTML: null
		});
		carousel.addClass('inserted');
	}
}


// Set current state in navigation
function letter_carousel_itemVisibleInCallback(carousel, item, idx, state) {
	var pager = $('#companies .all .tab .pager:visible');
	pager.find('a.page-num').removeClass("current");
	pager.find('a.page-num').eq(idx - 1).addClass("current")
};

function letter_carousel_initCallback(carousel) {
	var pager = $('#companies .all .tab .pager:visible');
	
	// Pager
	pager.find('a').not('.previous, .next').click(function() {
		var page = $(this).text();
		carousel.scroll($.jcarousel.intval(page));
		return false;
	});
	
	// Previous
	pager.find('a.previous').click(function() {
		carousel.prev();
		return false;
	});
	
	// Next
	pager.find('a.next').click(function() {
		carousel.next();
		return false;
	});
}

function init_tabs() {
	 if ($fast('.tabs').length) 
	 {
	     // Rewrite URLs to form suitable for UI Tabs
	     $('#search .nav a, #news .nav a, #companies .all .nav a').each(function (i) 
	     {
		  var href = $(this).attr('href').replace('.html', '');
		  if (href == "index" || href == "/") 
		  {
		      href = "internet";
		  }
		  href = '#' + href;
		  $(this).attr('href', href)
	     });
	     $fast('.tabs').not('.auctions').tabs();
	 }
}


function init_myservice(row, a) {
	var link = $(a);
	var id = link.attr('id').replace('a-', '');
	link.hoverIntent( {
		interval: 100, // milliseconds for onMouseOver polling interval
		over: function() {myservices_open({'id':id, 'row':row, 'link':link});},
		out: function () {}//myservices.close(id);} 
	});
}

var my_services_loaded_status = {'horoskop':0, 'program':0, 'email':0, 'sutaze':0, 'referaty':0};
var my_services_timeout = null;
function init_myservices() {
	$fast('#my-services .nav a').each(function(idx) {
		init_myservice('top', this);
	});
	
	$fast('#my-services .nav2 a').each(function(idx) {
		init_myservice('bottom', this);
	});
	
	$fast('#my-services').bind('mouseleave', function () {
	    my_services_timeout = window.setTimeout(function() {myservices_closeall();}, 2000);
	});
	
	$fast('#my-services').bind("mouseenter",function() {
		window.clearTimeout(my_services_timeout);
	});
}

var myservices_open_email_flag = false;
function myservices_open_email() {
	if(myservices_open_email_flag == true) myservices_open({'id':'email', 'row':'top', 'link':$fast('#a-email')});
}

var currently_open = null;
var next_to_open = null;
function myservices_open(service) {
	if(currently_open != null) {
		if(currently_open.id == service.id) {
			if(currently_open.state == 'closing') next_to_open = service;
			return;
		}
		next_to_open = service;
		if(currently_open.state == 'open') myservices_close(currently_open);
		return;
	}
	currently_open = service;
	currently_open.state = 'opening';
	
	var id = service.id;
	var link = $fast('#a-' + id);
	link.addClass('current');
	if(service.row == 'bottom') $fast("#my-services .nav2").addClass('opened');
	if(my_services_loaded_status[id] == 0) {
		$fast("#moje-sluzby-" + id).load("/moje-sluzby/" + id + "/");
		my_services_loaded_status[id] = 1;
	}
	$fast("#moje-sluzby-" + id).slideDown(function() {myservices_opened(service);});
	return false;
}

function myservices_opened(service) {
	if(currently_open == null) {
		alert("myservices_opened("+service.id+"): currently_open = null!!!");
		return;
	}
	if(currently_open.id != service.id) {
		alert("myservices_opened("+service.id+"): currently_open = "+currently_open.id+"; mismatch !!!");
	}
	if(currently_open.state != 'opening') {
		alert("myservices_opened("+service.id+"): currently_open state = "+currently_open.state+"; mismatch !!!");
	}
	currently_open.state = 'open';
	if(next_to_open != null) {
		myservices_close(currently_open);
	}
}

function myservices_close(service) {
	if(currently_open == null) {
		alert("myservices_close("+service.id+"): currently_open=null!!!");
		return;
	}
	if(currently_open.id != service.id) {
		alert("myservices_close("+service.id+"): currently_open="+currently_open.id+"; mismatch !!!");
	}
	if(currently_open.state != 'open') {
		alert("myservices_close("+service.id+"): currently_open state = "+currently_open.state+"; mismatch !!!");
	}
	currently_open.state = 'closing';
	$fast("#moje-sluzby-" + service.id).slideUp(function() {myservices_closed(service);});
}

function myservices_closed(service) {
	if(currently_open == null) {
		alert("myservices_closed("+service.id+"): currently_open=null!!!");
		return;
	}
	if(currently_open.id != service.id) {
		alert("myservices_closed("+service.id+"): currently_open="+currently_open.id+"; mismatch !!!");
	}
	if(currently_open.state != 'closing') {
		alert("myservices_closed("+service.id+"): currently_open state = "+currently_open.state+"; mismatch !!!");
	}
	currently_open = null;
	service.link.removeClass('current');
	if(service.row == 'bottom') $fast("#my-services .nav2").removeClass('opened');
	var l_next_to_open = next_to_open;
	next_to_open = null;
	if((l_next_to_open != null) && (l_next_to_open != 'close-all')) myservices_open(l_next_to_open);
}

function myservices_closeall() {
	if(currently_open != null) {
		if(currently_open.state == 'open') {
			myservices_close(currently_open);
			return;
		}
		if(currently_open.state == 'opening') {
			next_to_open = 'close-all';
			return;
		}
		if(currently_open.state == 'closing') {
			next_to_open = null;
			return;
		}
	}
}


var current_portals_page = 0;
function reload_portals() {
	var page = current_portals_page + 1;
	if(page > 4) page = 0;
	$fast("#portals").load("/portals/"+page+".html", function(){
		current_portals_page = page;
		start_portals_reload_timeout();
	});
}
var portals_reloader_timeout = null;
function start_portals_reload_timeout() {
	portals_reloader_timeout = window.setTimeout(reload_portals, 5000);
}

function init_portals_reloader() {
        $fast('#portals').bind('mouseleave', reload_portals);
        $fast('#portals').bind("mouseenter",function() {
                window.clearTimeout(portals_reloader_timeout);
        });
	start_portals_reload_timeout();
}


$(document).ready(function () {
	init_tabs();
	register_letter_load();
	init_myservices();
	myservices_open_email();
	//alert_cached_saves();
	init_portals_reloader();
});
