SearchDefaultValue = "Hledej podle interpreta, songu nebo alba ...";
var SearchDefaultColor = "#999999";
var SearchActiveColor = "";
var UrlBase = '/ajax';
AdvancedSearch = false;


function lookupAjax(){
	var oSuggest = $("#search-box")[0].autocompleter;

	oSuggest.findValue();

	return false;
}

function findValue(li) {
	if( li == null ) return alert("No match!");

	// if coming from an AJAX call, let's use the CityId as the value
	if( !!li.extra ) var sValue = li.extra[0];

	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;

	//alert("The value you selected was: " + sValue);
	$("#search-box").focus();
}

function selectItem(li) {
	findValue(li);
}

function formatItem(row) {
	return row[0]; // + " (id: " + row[1] + ")";
}


function SearchBlur(){
	var oSearchTextbox = $('#search-box');
	/*
	if(oSearchTextbox.attr('value') == "" || oSearchTextbox.attr('value') == SearchDefaultValue){
		oSearchTextbox.attr('value',SearchDefaultValue);
	}
	*/
	if(oSearchTextbox.attr('value') == ""){
		oSearchTextbox.css('background','url("/public/style/images/search-input-bg.png") no-repeat');
		oSearchTextbox.css('background-repeat','no-repeat")');
	} else {
		oSearchTextbox.css('background-image','none');
	}
};
function SearchFocus(){
	var oSearchTextbox = $('#search-box');
	if(oSearchTextbox.attr('value') == ""){
		oSearchTextbox.css('background','url("/public/style/images/search-input-bg.png") no-repeat');
		oSearchTextbox.css('background-repeat','no-repeat")');
	} else {
		oSearchTextbox.css('background-image','none');
	}
	oSearchTextbox.css('background-image','none');

};

/**
 *
 * @access public
 * @return void
 **/
function Najeti(znamka){
	//alert("test");
	//alert($(this).attr('id'));
	//alert($(this));
	$(this).addClass("ahoj");
	if (znamka ==1)
	{
		//obj2.parentNode.setAttribute("style","background-position: left -30px;");
		//alert("test2222");
	}
}

// Death link
function deadLink(id){
	$.ajax({
	  	type: 'POST',
		url: UrlBase + '/deadlink.php',
		dataType: 'json',
		data: {
			'id' : id,
			'rand' : Math.random()
		},
		success: function(r) {
		  		switch (r.status)
				{
					case 'already' :
						$('#deadlink').html("Hlášení jsme už obdrželi.");
						break;

					case 'ok' :
						$('#deadlink').html("Děkujeme. Link bude obnoven do 24h.");
						break;
				}
		},
		failure: function() {
			alert('Došlo k chybě při komunikaci se serverem!');
		},
		error: function () {
			alert('Došlo k chybě při komunikaci se serverem!');
		}

	});

}

/**
 *
 * @access public
 * @return void
 **/
function regFb(){
	alert("reg");
	document.location.href = "/facebook-prihlaseni.php";
}
$(document).ready(function() {
	Init();
});

	function addfavorite(id){
		$.ajax({
		  type: 'POST',
		  url: UrlBase + '/favorite.php',
		  dataType: 'json',
		  data: {
			'hit' : id,
			'action' : 'add',
			'rand' : Math.random()
		  },
		  success: function() {
		    loadfavorite(true);
		  }
		});
	}

	function removefavorite(id){
		$.ajax({
		  type: 'POST',
		  url: UrlBase + '/favorite.php',
		  dataType: 'json',
		  data: {
			'hit' : id,
			'action' : 'rem',
			'rand' : Math.random()
		  },
		  success: function() {
		    loadfavorite(true);
		  }
		});
	}

	function removeAllPlaylist(id){
		$.ajax({
		  type: 'POST',
		  url: UrlBase + '/favorite.php',
		  dataType: 'json',
		  data: {
			'action' : 'removeall',
			'rand' : Math.random()
		  },
		  success: function() {
		    loadfavorite(true);
		  }
		});
	}
	// nacitani playlistu
	function loadfavorite(reload){
		$('#person_playlist').css('display','block');


		$('#playlist').addClass("reloader2");
		$.ajax({
		  type: 'POST',
		  url: UrlBase + '/favorite.php',
		  data: {
			'action' : 'load',
			'rand' : Math.random()
		  },
		  success: function(r) {
		    $('#playlist').html(r);
					// zavreny playlist
			if (getCookie("pl_box") == "1")
			{

				//$('#playlist').css('display','block');
				/*
				$('#playlist').slideDown('slow');
				$('.open-playlist').hide();
				$('.close-playlist').show();
				*/
			}
			if (reload)
			{
				if ($('#playlist').is(":visible")) {

				} else {
					// playlist je skrytý, nachvíli odkryju
					$('#playlist').slideDown('slow');
					$('#playlist').slideUp('slow');
					$('.open-playlist').show();
					$('.close-playlist').hide();
				}
			}
			/*
			if ($('#playlist').is(":visible")) {

			} else {
				// playlist je skrytý, nachvíli odkryju
				$('#playlist').slideDown('slow');
				$('.open-playlist').hide();
				$('.close-playlist').show();
			}

			*/
		    $('#playlist').removeClass("reloader2");
		   // alert($("#playlist table tr").length);
		    if($("#playlist table tr").length > 0) {
		    	$(".playlist-header h3").html("Playlist (" + $('#playlist table tr').length + ")");
			}
		  }
		});
	}
	function rate(id,znamka){
	$('#obal11').addClass("reloader");
		var rand = 48178;
		$.ajax({
		  	type: 'POST',
			url: UrlBase + '/rate.php',
			dataType: 'json',
			data: {
				'id' : id,
				'z' : znamka,
				'rand' : rand
			},
		  success: function(r) {

		  		switch (r.status)
				{
					case 'ok' :
						$('#vote' + r.id).css('background-position',r.position);
						$('#vote' + r.id).parent().children('.result').html(r.result);
						$('#vote' + r.id).children().remove();
						//alert('Vaš hlas je započten');
						break;

					case 'already_voted' :
						alert('Už jste hlasoval/a!');
						break;

					case 'wrong_code':
						alert('Hodnocení nelze uložit - zkus to, prosím, později!');
						break;

					case 'graylist' :
						alert("Tato IP adresa je dočasně zablokovaná - čekej " + Math.ceil(r.timeout / 60) + " min. do hlasování.");
						break;

					case 'blacklist' :
						alert("Tato IP adresa je na černé listině - nemáš povoleno hlasovat.\nNa černou listinu byla tato IP adresa zařazena z důvodu velkého množství podovodných hlasování");
						break;

					case 'db_insert' :
						alert('Hodnocení nelze uložit - zkus to, prosím, později!');
						break;
				}
			$('#obal11').removeClass("reloader");
		  },
			failure: function() {
				$('#obal11').removeClass("reloader");
				alert('Došlo k chybě při komunikaci se serverem!');
			},

			error: function () {
				$('#obal11').removeClass("reloader");
				alert('Došlo k chybě při komunikaci se serverem!');
			}
		});
	}

/**
 *
 * @access public
 * @return void
 **/

/**
 *
 * @access public
 * @return void
 **/
function rate_clean(e){
		var pos = $(e.parentNode).children(':input[name="pos"]').val();
		$(e.parentNode).css('background-position', pos);
		var res = $(e.parentNode).children(':input[name="res"]').val();
		$(e.parentNode.parentNode).children('.result').html(res);
		//alert (value);
}
function Init() {
	/*
	$('#search-box').blur(function() {
	  //alert('Handler for .blur() called.');
	  SearchBlur();
	});
	$('#search-box').focus(function() {
	  //alert('Handler for .focus() called.');
	  SearchFocus();
	});
	*/
	loadfavorite(false);

	//$('#person_playlist').css('display','block');
	/*
	if($("#playlist table").length) {
	//	$('#playlist').css('display','block');
	} else {
		$('#playlist').css('display','none');
	}
	*/
/*
	if ($('#person_playlist table')) {

		$('#playlist').css('display','block');
	} else {
		$('#playlist').css('display','none');
	}
	*/
	//SearchBlur();

	$('.star-1').click(function(e) {
		e.preventDefault();
		var znamka = 1;
		var id_vote = $(this.parentNode).attr("id").substring(4);
		rate(id_vote, znamka);
	});
	$('.star-2').click(function(e) {
		e.preventDefault();
		var znamka = 2;
		var id_vote = $(this.parentNode).attr("id").substring(4);
		rate(id_vote, znamka);
	});
	$('.star-3').click(function(e) {
		e.preventDefault();
		var znamka = 3;
		var id_vote = $(this.parentNode).attr("id").substring(4);
		rate(id_vote, znamka);
	});
	$('.star-4').click(function(e) {
		e.preventDefault();
		var znamka = 4;
		var id_vote = $(this.parentNode).attr("id").substring(4);
		rate(id_vote, znamka);
	});
	$('.star-5').click(function(e) {
		e.preventDefault();
		var znamka = 5;
		var id_vote = $(this.parentNode).attr("id").substring(4);
		rate(id_vote, znamka);
	});


	$('.star-1').mouseover(function() {
		$(this.parentNode).css('background-position', 'left -30px');
		$(this.parentNode.parentNode).children('.result').html('Špatný');
	});


	$('.star-1').mouseout(function() {
		//$(this.parentNode).css('background-position', '0 0');

		var id_vote = $(this.parentNode).attr("id").substring(4);
		rate_clean(this);
	//	var name = $(this.parentNode).children(':input[name="pos"]').attr("name");

	});
	$('.star-2').mouseover(function() {
		$(this.parentNode).css('background-position', '-96px -30px');
		$(this.parentNode.parentNode).children('.result').html('Ujde to');
	});

	$('.star-2').mouseout(function() {
		rate_clean(this);
		//$(this.parentNode).css('background-position', '0 0');
	});
	$('.star-3').mouseover(function() {
		$(this.parentNode).css('background-position', '-192px -30px');
		$(this.parentNode.parentNode).children('.result').html('Dobrý');
	});

	$('.star-3').mouseout(function() {
	//	$(this.parentNode).css('background-position', '0 0');
		rate_clean(this);
	});
	$('.star-4').mouseover(function() {
	  $(this.parentNode).css('background-position', '-288px -30px');
	  $(this.parentNode.parentNode).children('.result').html('Super');
	});
	$('.star-4').mouseout(function() {
	//	$(this.parentNode).css('background-position', '0 0');
		rate_clean(this);
	});
	$('.star-5').mouseover(function() {
	  $(this.parentNode).css('background-position', '-384px -30px');
	  $(this.parentNode.parentNode).children('.result').html('Super pecka');
	});
	$('.star-5').mouseout(function() {
	//	$(this.parentNode).css('background-position', '0 0');
		rate_clean(this);
	});
		/*
	$("#search-box").autocomplete(
		"/search2.php",
		{
			multiple: false,
			minChars: 2,
			selectFirst: false,

			mustMatch: false,
        	matchContains: false,

			onItemSelect:selectItem,
			onFindValue:findValue,
			formatItem:formatItem,
			autoFill:false
		}
	);
	*/
/*
	$("#search-box").autocomplete(
		"/search2.php",
		{
			delay:0,
			minChars:1,
			matchSubset:100,
			matchContains: false,
			cacheLength:0,
			onItemSelect:selectItem,
			onFindValue:findValue,
			formatItem:formatItem,
			autoFill:false
		}
	);
	*/
		$("#search-box").autocomplete(
		UrlBase + "/search.php",
		{
		source: UrlBase + "/search.php",
        minLength: 2,
        matchContains: true,
        selectFirst: false,
        onItemSelect:selectItem,
			onFindValue:findValue,
			formatItem:formatItem
		}
	);


	$('#data_grid').delegate('div .h-title', 'mouseover', function() {
		$(this).addClass('akt-row');
		return false;
	});

	$('#data_grid').delegate('div .h-title', 'mouseout', function() {
		$(this).removeClass('akt-row');
		return false;
	});
		$('#data_grid').delegate('div .h-title-sudy', 'mouseover', function() {
		$(this).addClass('akt-row');
		return false;
	});

	$('#data_grid').delegate('div .h-title-sudy', 'mouseout', function() {
		$(this).removeClass('akt-row');
		return false;
	});

	$('#topDL').delegate('tr', 'mouseover', function() {
		$(this).addClass('akt-row');
		return false;
	});

	$('#topDL').delegate('tr', 'mouseout', function() {
		$(this).removeClass('akt-row');
		return false;
	});

	$('#playlist').delegate('tr', 'mouseover', function() {
		$(this).addClass('akt-row');
		return false;
	});

	$('#playlist').delegate('tr', 'mouseout', function() {
		$(this).removeClass('akt-row');
		return false;
	});
	// autoFill: true = doplnuje text do přikazového radku
	/*
	$("#search-box").autocomplete(
		"/search2.php",
		{
	        matchContains:false,
	        minChars:1,
	        autoFill:false,
	        mustMatch:false,
	        cacheLength:20,
	        max:20
		}
	);
*/
};

function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString()) + "; path=/";
document.cookie=c_name + "=" + c_value;
}

// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function getCookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}



