var xmlhttp = false;
var mnmxmlhttp = Array ();
var mnmString = Array ();
var mnmPrevColor = Array ();
var responsestring = Array ();
var myxmlhttp = Array ();
var responseString = new String();

if(window.XMLHttpRequest)
{
	// Firefox   
	xmlhttp = new XMLHttpRequest();   
}
else if(window.ActiveXObject)
{
	// Internet Explorer   
	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");   
}
else
{
	// XMLHttpRequest non supporté par le navigateur
	alert("Your browser doesn't support XMLHTTPRequest Objects");
}

function myXMLHttpRequest ()
{
	var xmlhttplocal;
	if (window.XMLHttpRequest)
	{
		xmlhttplocal = new XMLHttpRequest();

		// Évite un bug du navigateur Safari :
		if (xmlhttplocal.overrideMimeType)
		{
			xmlhttplocal.overrideMimeType("text/xml");
		}
	}
	else
	{
		if (window.ActiveXObject)
		{

			try 
			{
				// essaie de charger l'objet pour IE
				xmlhttplocal = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e)
			{
				try
				{
					// essaie de charger l'objet pour une autre version IE
					xmlhttplocal = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e)
				{
					window.alert("Váš prohlížeč nepodporuje XMLHTTPRequest objekt.");
				} // try-catch
			} // try-catch

		}
	} // if-else
	return (xmlhttplocal);
};

/*function vote (id, instpath)
{
  	if (xmlhttp) {
		url = instpath + "/vote.php";
		strContent = new String();
		strContent = 'id=' + id;
		anonymous_vote = true;
		mnmxmlhttp[id] = myXMLHttpRequest ();
		
		if (mnmxmlhttp[id])
		{
			mnmxmlhttp[id].open ("POST", url, true);
			mnmxmlhttp[id].setRequestHeader ('Content-Type',
					   'application/x-www-form-urlencoded');

			mnmxmlhttp[id].send (strContent);
			errormatch = new RegExp ("^ERROR:");

			target1 = document.getElementById ('vote-' + id);

			mnmPrevColor[id] = target1.style.backgroundColor;
			
			mnmxmlhttp[id].onreadystatechange = function () 
			{
				if (mnmxmlhttp[id].readyState == 4) 
				{
					mnmString[id] = mnmxmlhttp[id].responseText;
					if (mnmString[id].match (errormatch)) 
					{
						mnmString[id] = mnmString[id].substring (6, mnmString[id].length);
						changemnmvalues (id, true);
					} else 
					{
						changemnmvalues (id, false);
					}
				}
			}
		}
	}
};

function changemnmvalues (id, error)
{
	split = new RegExp ("~--~");
	b = mnmString[id].split (split);
	//alert(id);
	target1 = document.getElementById ('voted-' + id);
	target2 = document.getElementById ('vote-' + id);
	if (error) {
		return false;
	}
	if (b.length <= 3) {
		target1.innerHTML = b[0];
		target1.style.backgroundColor = mnmPrevColor[id];
	}
	return false;
};*/


function clk(id)
{
	(new Image()).src="clic.php?id="+id;
	return true;
};

function CheckLen()
{
	fieldToCheck = 'description';
	fieldMaxLength = 255;
	
	for(var i=0;i < document.forms.length; i++)
	{
		for(j=0;j < document.forms[i].elements.length; j++)
		{
			if(document.forms[i].elements[j].name == fieldToCheck)
			{
				StrLen = document.forms[i].elements[j].value.length;
				if ( StrLen > fieldMaxLength )
				{
					document.forms[i].elements[j].value = document.forms[i].elements[j].value.substring(0,fieldMaxLength);
					CharsLeft = fieldMaxLength;
				} else {
					CharsLeft = StrLen;
				};
				var CharsLeft1 = fieldMaxLength - CharsLeft;
				
				// Updating reste field
				document.forms[i].reste.value = CharsLeft1;
			}
		}
	}

};

function confirm_broken( myid, myurl)
{
	confirmation = confirm( "!!! POZOR !!!\n\nOpravdu chcete označit stránku jako neaktivní?\n\n" + myurl + "\n\n" );
	if(confirmation)
	{
		(new Image()).src = "broken.php?id="+myid;
		alert( "Děkujeme za spolupráci !\n\nStránka bude ověřena v co možná nejkratší době." );
	};
};

function ac_return(field, item)
{
        // on met en place l'expression réguličre
        var regex = new RegExp('[0123456789]*-mini', 'i');
        // on l'applique au contenu
        var nomimage = regex.exec($(item).innerHTML);
        //on récupčre l'id
        id = nomimage[0].replace('-mini', '');
        // et on l'affecte au champ caché
        $(field.name+'_id').value = id;
}


function multiClass(eltId) {
	arrLinkId = new Array('l_0','l_1','l_2','l_3');
	intNbLinkElt = new Number(arrLinkId.length);
	arrClassLink = new Array('current','ghost');
	strContent = new String();
	for (i=0; i<intNbLinkElt; i++) {
		strContent = "menu"+arrLinkId[i];
		if ( arrLinkId[i] == eltId ) {
			document.getElementById(arrLinkId[i]).className = arrClassLink[0];
			document.getElementById(strContent).className = 'on content';
		} else {
			document.getElementById(arrLinkId[i]).className = arrClassLink[1];
			document.getElementById(strContent).className = 'off content';
		}
	}	
}

$().ready(function(){


	//------------------------------------------
	// Nacitani aktualit
	//------------------------------------------
	/*$.get('/ajax/aktuality.php', function (data){
		$('#aktuality_div').hide('slow', function(){
			$('#aktuality_div').html(data);
			$('#aktuality_div').show('slow');
		});
	});


	//------------------------------------------
	// Nacitani dat z KdyKde.cz
	//------------------------------------------
	$.get('/ajax/kdykde.php', function (data){
		$('#kdykde_div').hide('slow', function(){
			$('#kdykde_div').html(data);
			$('#kdykde_div').show('slow');
		});
	});*/


	//------------------------------------------
	// Nacitani dat pro pocasi
	//------------------------------------------
	/*$.get('/ajax/pocasi.php', function (data){
		$('#pocasi_div').hide('slow', function(){
			$('#pocasi_div').html(data);
			$('#pocasi_div').show('slow');
		});
	});*/


	//------------------------------------------
	// Nacitani dat pro zajezdy
	//------------------------------------------
	$.getJSON('/ajax/holiday.php', function (data){
		$('#zajezdy_total').html(data.total);
		$('#zajezdy_fm').html(data.fm);
		$('#zajezdy_lm').html(data.lm);
		$('#zajezdy_dest').html(data.dest);
		$('#zajezdy_ck').html(data.ck);
	}, "json");


	//------------------------------------------
	// Nacitani dat pro tipy na vylety
	//------------------------------------------
	/*$.get('/ajax/tipy.php', function (data){
		$('#tipy_div').hide('slow', function(){
			$('#tipy_div').html(data);
			$('#tipy_div').show('slow');
		});
	});


	//------------------------------------------
	// Nacitani dat pro tipy na vylety
	//------------------------------------------
	$.get('/ajax/clanky.php', function (data){
		$('#clankycest_div').html(data);
	});*/


	//------------------------------------------
	// Add a "rel" attrib if Opera 7+
	//------------------------------------------
	if(window.opera) {
		if ($("a.jqbookmark").attr("rel") != ""){ // don't overwrite the rel attrib if already set
			$("a.jqbookmark").attr("rel","sidebar");
		}
	}


	//------------------------------------------
    // Pridani stranky do oblibenych
    //------------------------------------------
	$("a.jqbookmark").click(function(event){


		//------------------------------------------
	    // prevent the anchor tag from sending the 
		//  user off to the link
	    //------------------------------------------
		event.preventDefault();
		var url = this.href;
		var title = this.title;

		//------------------------------------------
	    // Mozilla Firefox Bookmark
	    //------------------------------------------
		if (window.sidebar) {

			window.sidebar.addPanel(title, url,"");


		//------------------------------------------
	    // IE Favorites
	    //------------------------------------------
		} else if( window.external ) {

			window.external.AddFavorite( url, title);


		//------------------------------------------
	    // Opera 7+
		//  do nothing - the rel="sidebar" should do 
		//  the trick
	    //------------------------------------------
		} else if(window.opera) {

			return false;


		//------------------------------------------
	    // for Safari, Konq etc - browsers who do 
		//  not support bookmarking scripts
	    //------------------------------------------
		} else {
			alert('Bohužel Váš prohlížeč nepodporuje požadovanou akci, uložte záložku manuálně.');
		}
	});


	$('a.setHP'). click(function(){
		if ($.browser.msie) {
			this.style.behavior='url(#default#homepage)';
			if (this.setHomePage('http://www.cestujme.cz')) {
				alert('Stránka byla nastavena jako výchozí');
			} else {
				alert('Nelze nastavit tuto stránku jako výchozí!');
			}
		} else {
			alert('Tato možnost je přístupná pouze v prohlížečích Internet Explorer.');
		}
	});	


	//------------------------------------------
    //  Kontrola formularu
    //------------------------------------------
	$('.form_checker').val(Math.random()*11);


	//------------------------------------------
    //  Hodnoceni mist - vyplneni hvezdicek
    //------------------------------------------
    $('.rating_bar img').mouseover(function() {
    	var id_parts = $(this).attr('id').split('_');
    	for (var i = 1; i <= $('.rating_bar img').length; i++) {
    		if (i <= id_parts[1]) {
    			$('#img_'+i+'_'+id_parts[2]).attr('src', theme_dir + '/images/star.png');
    		} else {
    			$('#img_'+i+'_'+id_parts[2]).attr('src', theme_dir + '/images/star_blank.png');
    		}
    	}
    });
    $('.rating_bar img').each(function(){
    	$(this).mouseout(function() {
	    	var id_parts = $(this).attr('id').split('_');
	    	for (var i = 0; i <= id_parts[1]; i++) {
	    		$('#img_'+i+'_'+id_parts[2]).attr('src', theme_dir + '/images/star_blank.png');
	    	}
    	});
    });


    //------------------------------------------
    //  Hodnoceni mist - hlasovani
    //------------------------------------------
    $(".rating_bar img").each(function() {
    	$(this).click(function() {
	    	var parts = $($($(this).parent().get(0)).parent().get(0)).attr('rel').split('_');
	    	vote(parts[0], $(this).attr('title'), parts[1]);
    	});
    });


    //------------------------------------------
    //  Hodnoceni mist - vyplneni hvezdicek
    //------------------------------------------
    $('.bar_stars').mouseout(function(){
    	stars($(this));
    });
    stars();
});


//------------------------------------------
//  Hodnoceni mist - vyplneni hvezdicek
//------------------------------------------
function stars() {
	if (arguments.length == 0) {
		$("span[class^='voting_total']").each(function(){
			var parts = $(this).attr('class').split('_');
			var rating = $(this).html();
			var mod = parseFloat(rating);
			$("div[rel="+parts[2]+"_0] img").each(function(){
				if (mod == 0.5) {
					$(this).attr('src', theme_dir + '/images/star_half.png');
				} else if (mod > 0) {
					$(this).attr('src', theme_dir + '/images/star.png');
				} else {
					$(this).attr('src', theme_dir + '/images/star_blank.png');
				}
				mod--;
			});
		});
	} else {
		for (var i = 0; i < arguments.length; i++) {
			var parts = $($(arguments[i]).parent().get(0)).attr('rel').split('_');
			var rating = $("span[class^='voting_total_"+parts[0]+"']").html();
			var mod = parseFloat(rating);
			$("div[rel="+parts[0]+"_0] img").each(function(){
				if (mod == 0.5) {
					$(this).attr('src', theme_dir + '/images/star_half.png');
				} else if (mod > 0) {
					$(this).attr('src', theme_dir + '/images/star.png');
				} else {
					$(this).attr('src', theme_dir + '/images/star_blank.png');
				}
				mod--;
			});
		}
	}
}


//------------------------------------------
//Hodnoceni mist - hlasovani
//------------------------------------------
function vote(id, vote_nr, uid) {
    $.getJSON("/ajax/voting.php?id="+id+"&vote="+vote_nr+"&uid="+uid,
        function(data) {
            $(".voting_total_"+id).html(data+'&nbsp;&nbsp;');
            setInfo('Děkujeme, Váš hlas byl přijat.');
            stars();
        }
    );
}


//------------------------------------------
//Nastaveni informace
//------------------------------------------
function setInfo(text) {
	$('#rating_info').html(text);
    $("#rating_info").fadeIn("slow", function(){
        $("#rating_info").fadeTo(3000, 1, function(){
            $("#rating_info").fadeOut("slow", function(){
                $('#rating_info').html('&nbsp;');
            });
        });
    });
}

