/** -------------------------------------------------------------------\
* globalni skripty pro CT24											|
*																	  |
* pro pouzivani tohoto skriptu je nutnost mit zavedenou nadstavbu	  |
* JQuery															   |
*																	  |
* @author Radek Zemanek <radek.zemanek@czech-tv.cz					   |
* @copyright Ceska televize											|
* @version 2.1.07.08.08												|
\---------------------------------------------------------------------*/

/** ################################################################# */

/** -------------------------------------------------------------------\
* definice funkci a objektu											|
\---------------------------------------------------------------------*/
	
/**
* otevreni noveho okna pro galerie a ostatni
*
* @param string $url
* @return boolean
*/
function popupGalleryWindow(url) {
	window.open(url, 'galleryWindow', 'toolbar=no,scrollbars=1,location=no,status=no,width=189,height=160,resizable=yes,menubar=no,directories=no');
	return false;
}

/**
* zjistime jaky pouzivame prohlizec plus verzi exploreru
*
* @return string | float | integer
*/
function getBroswer(){

	/** ################################################################# */

	/** -------------------------------------\
	* definice globalnich promennych objektu |
	\---------------------------------------*/


	/**
	* @var string jmeno prohlizece
	*/
	this.name = "";

	/**
	* @var integer | float verze prohlizece
	*/
	this.version = 0;

	/** ################################################################# */

	/** -------------------------------------\
	* definice public funkci				 |
	\---------------------------------------*/


	/**
	* inicializacni cast objektu, kde se pokusime zjistit zakladni jmeno a verzi
	*
	* @return void
	*/
	this.init = function() {
		this.name	= this.getName();
		this.version = this.getVersion();
	}

	/**
	* zjistime jmeno prohlizece
	*
	* @return string
	*/
	this.getName = function() {

		/**
		* @var string zkusime stesti pro explorer
		*/
		var n = navigator.appName;

		if (n == "Microsoft Internet Explorer") {
			return "ie";
		}
		else {
			return "other";
		}
	};

	/**
	* ziskani verze prohlizece, hlavne pro ie
	*
	* @return void
	*/
	this.getVersion = function() {

		/**
		* @var boolean zjistime jestli mame verzi 6.0
		*/
		var sest  = (document.all && document.fireEvent && document.createComment) ? true : false;

		/**
		* @var boolean zjistime jestli mame verzi 7.0
		*/
		var sedm  = (document.all && window.showModalDialog && window.XMLHttpRequest) ? true : false;

		/**
		* @var boolean zjistime jestli mame nizsi verzi nez 6.0
		*/
		var other = (document.all && window.showModalDialog) ? true : false;

		if (sedm) {
			return 7.0;
		}
		else if (sest) {
			return 6.0;
		}
		else if (other) {
			return 5;
		}
	}

	/** ################################################################# */

	/** -------------------------------------\
	* vykonavaci cast objektu				|
	\---------------------------------------*/

	this.init();
}

/**
* nastaveni velikosti pisma v clancich
*
* @param string $size
* @return void
*/
function setFontSize(size) {

	/**
	* @var string velikost textu
	*/
	var velikost = '100%';

	/**
	* @var string velikost nadpisu
	*/
	var velikosth1 = '145%';

	switch(size) {
		case 'xl':
			velikost = '150%';
			velikosth1 = '200%';
		break;

		case 'l':
			velikost = '125%';
			velikosth1 = '170%';
		break;

		case 'm':
			velikost = '100%';
			velikosth1 = '145%';
		break;

		case 's':
			velikost = '80%';
			velikosth1 = '120%';
		break;

		case 'ss':
			velikost = '70%';
			velikosth1 = '100%';
		break;

		default:
		break;
	}

	/**
	* nastaveni cookie pro ulozeni nastaveni
	*/
	setCookie("velikostTextu",size);

	/**
	* nastaveni velikosti pisem a nadpisu
	*/
	$('#article #leftColumn h1').css('font-size',velikosth1);
	$('#articlePerex').css('font-size',velikost);
	$('#articleContent').css('font-size',velikost);
}

/**
* objekt pro praci s entitami pro prevod zejmena do cookie a z cookie
*
* @return void
*/
entitUtil = function() {

	/** ################################################################# */

	/** -------------------------------------\
	* definice globalnich promennych objektu |
	\---------------------------------------*/

	/**
	* @var array znaky pro prevod
	*/
	this.ch = new Array();

	/**
	* @var array entity pro prevod
	*/
	this.en = new Array();

	/**
	* @var integer delka ulozenych znaku pro prevod
	*/
	this.len = 0;

	/** ################################################################# */

	/** -------------------------------------\
	* definice public funkci				 |
	\---------------------------------------*/

	/**
	* zavedeni noveho znaku a jeho preklad v entite a naopak
	*
	* @param char $znak
	* @param string $code
	* @return void
	*/
	this.addEnt = function(znak,code) {
		this.ch[znak]	   = new Array();
		this.ch[znak]['c']  = znak;
		this.ch[znak]['e']  = code;

		this.en[this.len]	   = new Array();
		this.en[this.len]['c']  = znak;
		this.en[this.len]['e']  = code

		this.len++;
	}

	/**
	* inicializace objektu a zavedeni prekladu nejznamejsich entit
	*
	* @return void
	*/
	this.init = function() {
		this.addEnt('š','/s001');
		this.addEnt('Š','/s002');
		this.addEnt('č','/c001');
		this.addEnt('Č','/c002');
		this.addEnt('ř','!r!');
		this.addEnt('Ř','!R!');
		this.addEnt('ž','!z!');
		this.addEnt('Ž','!Z!');
		this.addEnt('ď','!d!');
		this.addEnt('Ď','!D!');
		this.addEnt('ť','!t!');
		this.addEnt('Ť','!T!');
		this.addEnt('Č','!C!');
		this.addEnt('ě','!e!');
		this.addEnt('Ě','!E!');
		this.addEnt('ů','!u!');
		this.addEnt('ú','!uu!');
		this.addEnt('Ú','!U!');
		this.addEnt('á','/a001');
		this.addEnt('í','/i001');
		this.addEnt(';','!strednik!');
		/*this.addEnt(' ','! !');
		this.addEnt(';','! !');
		this.addEnt('<','! !');
		this.addEnt('>','! !');
		this.addEnt('&','! !');*/
	};

	/**
	* dekodovani entity
	*
	* @param string $s
	* @return string
	*/
	this.entitDecode = function(s){
		for(i = 0; i < this.len; i++) {
			s = this.str_replace(this.en[i]['c'], this.en[i]['e'], s);
		}

		return s;
	};

	/**
	* enkodovani entity
	*
	* @param string $s
	* @return string
	*/
	this.entitEncode = function(s){
		for(i = 0; i < this.len; i++) {
			s = this.str_replace(this.en[i]['e'], this.en[i]['c'], s);
		}
		return s;
	};

	/** ################################################################# */

	/** -------------------------------------\
	* definice private funkci				|
	\---------------------------------------*/

	/**
	* nahrazeni retezce za entity
	* private function
	*
	* @param string $search
	* @param string $replace
	* @param string $subject
	* @return string
	*/
	this.str_replace = function (search, replace, subject) {

		if (subject.indexOf(search) > -1) {
			/**
			* @var string kontejner pro preulozeni nahrazeneho retezce
			*/
			s  = subject.substring(0,subject.indexOf(search));
			s += replace;
			s += subject.substring(subject.indexOf(search) + search.length,subject.length);

			subject = this.str_replace(search,replace,s);
		}
		return subject;
	}

	/** ################################################################# */

	/** -------------------------------------\
	* vykonavaci cast objektu				|
	\---------------------------------------*/

	this.init();
}

/**
* nastaveni cookie
*
* @param string $id
* @param string $value
* @param integer $time
*/
setCookie = function(id, value, time) {

	/**
	* @var string datum vyprseni platnosti cookie
	*/
	var expires = "";

	/**
	* pokud mame nastavenou platnost, musime si prevest sekundy do formatu GMT
	*/
	if (time != 0 && time != null) {
		expires = new Date();
		expires.setTime(parseInt(expires.getTime(), 10) +  parseInt(time, 10));
		expires = expires.toGMTString();
	}
	else {
		expires = new Date();
		expires.setFullYear(expires.getFullYear()+1);
		expires = expires.toGMTString();
	}

	/**
	* @var string prevedeme si value hodnotu na string
	*/
	var st	= value.toString();

	/**
	* @var object zavedeme objekt pro praci s entitami a ceskymi znaky
	*/
	var stVal = new entitUtil();

	/**
	* @var string ziskame retezec pro ulozeni do cookie, specialni znaky prelozime pomoci objektu entitUtil()
	*/
	var cookieString = id + "=" + stVal.entitDecode(st) + ";" + (expires ? "expires=" + expires : "" ) + ";path=/;";

	document.cookie  = cookieString;
}

/**
* private function pro nacteni a rozdeleni cookie
*
* @param integer $offset
* @return string
*/
loadCookie = function(offset) {

	/**
	* @var integer zjistime si pozici u offsetu
	*/
	var endstr = document.cookie.indexOf (";", offset);

	if (endstr == -1) {
		endstr = document.cookie.length;
	}

	return unescape(document.cookie.substring(offset, endstr));
}

/**
* vytahne z cookie data
*
* @param string $name
* @return string || void
*/
getCookie = function (name) {

	/**
	* @var string argument pro vytazeni z cookie
	*/
	var arg	 = name + "=";

	/**
	* @var integer delka naseho argumentu
	*/
	var alen	= arg.length;

	/**
	* @var integer celkova delka cookie
	*/
	var clen	= document.cookie.length;

	/**
	* @var integer counter pro pozici v celkove cookie
	*/
	var i	   = 0;

	/**
	* @var object zavedeme objekt pro praci pro prevod na entity
	*/
	var stVal   = new entitUtil();

	while (i < clen) {

		/**
		* @var aktualni pozice v cookie
		*/
		var j = i + alen;

		if (document.cookie.substring(i, j) == arg) {
			textReturn = loadCookie(j);
			return stVal.entitEncode(textReturn);
		}

		i = document.cookie.indexOf(" ", i) + 1;

		if (i == 0) break;
	}

	return null;
}


/** ################################################################# */

/** ------------------------------------------------------------------\
| vykonani rutinich zalezitosti po nacteni stranky					|
\--------------------------------------------------------------------*/

/**
* pockame na nacteni stranky, potom zavolame nasi funkci
*
* @return void
*/
$(document).ready(function() {

	/**
	* po najeti na datum zobrazime kalendarium
	*
	* @param object $e
	* @return void
	*/
	$('#datum').mouseover(function(e){
		e.stopPropagation();
		e.preventDefault();
		$('#kalendariumInfo').show();
	});

	/**
	* po opusteni datumu schovame kalendarium
	*
	* @param object $e
	* @return void
	*/
	$('#datum').mouseout(function(e){
		e.stopPropagation();
		e.preventDefault();
		$('#kalendariumInfo').hide();
	});

	/**
	* po opusteni kalendaria schovame kalendarium
	*
	* @param object $e
	* @return void
	*/
	$('#kalendariumInfo').mouseout(function(e){
		e.stopPropagation();
		e.preventDefault();
		$('#kalendariumInfo').hide();
	});

	/**
	* po kliknuti na krizek, zavreme kalendarium
	*
	* @param object $e
	* @return void
	*/
	$('#closeKalendariumInfo').click(function(e){
		e.stopPropagation();
		e.preventDefault();
		$('#kalendariumInfo').hide();
	});

	/**
	* po kliknuti na zmenu fontu zmenime pismo a ulozime si velikost do cookie
	*
	* @param object $e
	* @return void
	*/
	$('#textova-velikost span').click(function(e){
		 e.stopPropagation();
		 e.preventDefault();

		 $('#textova-velikost span').removeClass('active');

		 /**
		 * @var string zjistime velikost pisma, kterou chceme zobrazovat
		 */
		 var strVelikost = $(this).attr('class');

		 setFontSize(strVelikost);

		 $(this).addClass('active');
	});

	/**
	* @var string zjistime pro jakou velikost mame zobrazit pismo
	*/
	var fontSize = getCookie('velikostTextu');

	if ((fontSize != null) && (fontSize != 'undefined') && (fontSize != 'null')) {
		$('#textova-velikost span').removeClass('active');
		$('#textova-velikost span.'+fontSize).addClass('active');

		setFontSize(fontSize);
	}

	/**
	* pri submitu ve vyhledavani, ulozime posledni hledanou hodnotu
	*
	* @return void
	*/
	$('#searchMasterForm').submit(function() {
		setCookie('searchMaster', $('#inpQueryMaster').val());
	});

	/**
	* pri kliku na a s tridou openGallery otevreme nove okno
	*
	* @param object $event
	* @return void
	*/
	$('.openGallery').click(function(event) {
		event.stopPropagation();
		event.preventDefault();

		/**
		* @var string adresa, na kterou budeme smerovat nove okno
		*/
		var url = $(this).attr('href');
		popupGalleryWindow(url);
	});

	/**
	* kliknutim na nejblok zmenime zalozku
	*
	* @param object $event
	* @return void
	*/
	$('#rightBlock > ul.menu > li').click(function (event) {

		$('#rightBlock > ul.menu > li').removeClass('currr');
		$(this).addClass('currr');

		/**
		* @var string regularni vyraz
		*/
		var re = /\-([\d]+)$/;

		/**
		* @var string zjistime jestli se jedna o nas potrebny retezec v id
		*/
		var showNr = $(this).attr('id').match(re);

		if(showNr) {
			$('div[id^=nejContenttt-]').hide();
			$('div[id=nejContenttt-'+ showNr[1]+']').show();
		}
	});
	
	
	/**
	* kliknutim na online kamery, zmenime data
	*
	* @param object $event
	* @return void
	*/
	$('#onlineCamersList p.typeCameras a').click(function (E) {
		E.stopPropagation();
		E.preventDefault();
		
		$('#onlineCamersList p.typeCameras a').removeClass('cur');
		$(this).addClass('cur');

		/**
		* @var string regularni vyraz
		*/
		var re = /\-([\d]+)/;

		/**
		* @var string zjistime jestli se jedna o nas potrebny retezec v id
		*/
		var showNr = $(this).attr('class');
			showNr = showNr.match(re);
			
		if(showNr) {
			
			$('#onlineCamersList p.item').removeClass('active');
			$('div[id=onlineCamerasListContent-'+ showNr[1]+'] p:first').addClass('active');
			
			$('div[id^=onlineCamerasListContent-]').hide();
			$('div[id=onlineCamerasListContent-'+ showNr[1]+']').show();
			$('#onlineCamersList .CamTitle p').html('<a href="' + $('div[id=onlineCamerasListContent-'+ showNr[1]+'] img:first').parent().attr('href') + '">' + $('div[id=onlineCamerasListContent-'+ showNr[1]+'] img:first').attr('title') + '</a>');
		}
	});
	
	
	/**
	* Na najeti kamerky se zobrazi jeji popis
	*
	*
	*/
	$('#onlineCamersList img').mouseover(function(E) {
		$('#onlineCamersList p.item').removeClass('active');
		$(this).parent().parent().addClass('active');
		$('#onlineCamersList .CamTitle p').html('<a href="' + $(this).parent().attr('href') + '">' + $(this).attr('title') + '</a>');
	});

	/**
	* pri kliknuti na a s tridou new_window otevreme nove okne s adresou href
	*
	* @return boolean
	*/
	 $('a').filter('.new_window').click(function(){
		  window.open(this.href);
		  return false;
	 } );

});

/** ################################################################# */

