// JavaScript Document
<!--

function toggleDiv(element){
}

function showDiv2(element){
  	var closeElement="0";
  	var closeMap="";
  	var openMap="";
	/*if(element=="dropper_199") {closeElement="dropper_200"; openMap="map_CZ"; closeMap="map_SK";}
  	if(element=="dropper_200") {closeElement="dropper_199"; openMap="map_SK"; closeMap="map_CZ";}
  	*/
     if(document.getElementById(element).style.display == 'none')
     {
       document.getElementById(element).style.display = 'block';
       document.getElementById(closeElement).style.display = 'none';
       
      // document.getElementById(openMap).style.display = 'block';
       document.getElementById(closeMap).style.display = 'none';
     }
     else if(document.getElementById(element).style.display == 'block')
     {
       document.getElementById(element).style.display = 'none';
     }

}

function showDiv(element){
	$(element).slideDown(2000);

}



function setLinks() {
  if (!document.getElementsByTagName) return false;
  var links = document.getElementsByTagName("a");
  if (links.length == 0) return false;
  for (var i = 0; i < links.length; i++) {
    var relation = links[i].getAttribute("rel");
    if (relation == "external") {
      links[i].onclick = function() {
        return !window.open(this.href);
      }
    }     
  }
}

function showOnMap(){
					 //window.alert(document.getElementById("gpsx").value+" -- "+document.getElementById("gpsy").value);
					 window.open("http://najchaty.sk/googlemap.php?open=map&gpsx="+document.getElementById("gpsx").value+"&gpsy="+document.getElementById("gpsy").value, "NajChaty.sk | Map", "status = 1, height = 600, width = 580, resizable = 0");
					}
					
function showMapButton(){
	document.getElementById("showOnMapButton").style.display = "inline";
	document.getElementById("showOnMapButtonLabel").style.display = "block";
}

function shrinkGB( ) {
    $("iframe").animate( { height:"200px" }, { queue:false, duration:1000 } )				 
	
        //GB_show('Thank you');
} 


function hideGB(){
	GB_hide();
}
$(document).ready(function(){
    //$("moreCalendar").show(2000)
	$("#showr").click(function () {
		$("moreCalendar").show(2000);
    });
    $("#hidr").click(function () {
      $("div").hide(2000);
    });
	
	
	// Favorites
	$('[addtofavs]').bind("click", function() {
		var data=$(this).attr("addtofavs");
		var favCookie=cookieArrayRead("favCookie");
		var thisIndex=favCookie.length;
		var stored=0;		
		
		for (i in favCookie) {			
		//	var temp=favCookie[i].split(",,");	
			var target=data.split(",,");
			//console.log("in?"+temp[3]+"="+target[3]);
			if (favCookie[i]==data) {
			//if (temp[3]==target[3]) {
				stored=1; var storedAs=i;
				break;
			}
		}
		if (stored==0) {
			if (thisIndex==15) {
				return false;
			}
			favCookie[thisIndex]=data;
			cookieArrayWrite("favCookie", favCookie);
			if ($(this).html().length>0) {
				//$(this).html(lang.favorites.remove);
			}
			
					
			updateFavorites();
			
			
			if($(this).hasClass("tool_pridat"))	$(this).parent().parent().parent().parent().parent().siblings("a").children("img").effect("transfer", { to: "#fav"+thisIndex}, 1200);	
			//else $(".bigimage").effect("transfer", { to: "#fav"+thisIndex}, 1200);	
			
			$(this).toggleClass("selected", 0);
			
			$(this).html("Odobrat z oblúbených");
	
			var nameParsed=data.split(",,");
			var namePos=nameParsed[0].lastIndexOf("/");
			nameParsed=nameParsed[0].substr(namePos+1);
			$.ajax({
				type: "GET",
				url: "/classes/favorites.ajax.php",
				data: "id="+nameParsed,
				dataType: "text"
			});
		} else {
			delete favCookie[storedAs];
			cookieArrayWrite("favCookie", favCookie);
			thisIndex=storedAs;
			$(this).toggleClass("selected", 1);
				$(this).html("Pridať k oblúbeným");
			if ($(this).html().length>0) {
				//$(this).html(lang.favorites.add);
			} else {
				$(this).removeClass("selected");
			}
			updateFavorites();
		}
		return false;
	});
	
	$('[addtofavs]').each(function(i) {
		var data=$(this).attr("addtofavs");
		var favCookie=cookieArrayRead("favCookie");
		if (favCookie.length>0) {
			for (i in favCookie) {
				if (favCookie[i]==data) {
					if ($(this).html().length>0) {
						//$(this).html(langlang.favorites.remove);
					} else {
						$(this).addClass("selected")
					}
				}
			}
		}
	});

	
	
  });
  

  
  //PRETTYPHOTO
		$(document).ready(function(){
				
			$("a[rel^='prettyOverlay'],a[rel^='prettyPhoto']").prettyPhoto({
				animationSpeed: 'normal', /* fast/slow/normal */
				padding: 40, /* padding for each side of the picture */
				opacity: 0.35, /* Value betwee 0 and 1 */
				showTitle: true, /* true/false */
				allowresize: true /* true/false */
			});
		});
	
	//GREYBOX REDUX
	var GB_ANIMATION = true;
      $(document).ready(function(){
        //$("a.greybox").click(function(){
		$("a#showOnMap, a.tool_mapa, a.tool_mail").click(function(){		
		var t = this.title || $(this).text() || this.href;
          GB_show(t,this.href,600,580);
          return false;
        });
      });
		


  
  
/*
$("#showr").click(function () {
		$("div").show(2000);
    });
    $("#hidr").click(function () {
      $("div").hide(2000);
    });
*/



function cookieArrayRead(name) {
	var v=""+cookieRead(name);
	var temp;
	var newValue=new Array();
	if (v.length>0) {
		v=v.split("~");
		for (var i in v) {
			v[i]=v[i]+"";
			if (v[i].length>0){
				temp=v[i].split("^");
				newValue[temp[0]]=temp[1];
			}
		}
	}
	return newValue;
}

function cookieRead(name) {
  var cookieValue = "";
  var searchString = name + "=";
  if(document.cookie.length > 0) {
    offset = document.cookie.indexOf(searchString);
    if (offset != -1) { 
      offset += searchString.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end));
    }
  }
  return cookieValue;
}

function cookieWrite(name, value, hours) {
  var expire = "";
  if(hours != null) {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
  }
  var path="; path=/";
  document.cookie = name + "=" + escape(value) + expire + path;
  
}

function cookieArrayWrite(name, v, hours) {
	var valueArray=new Array();
	var a=0;
	var i;
	for (i in v) {
		valueArray.push(i+"^"+v[i]);
	};
	
	
	var valueJoined=valueArray.join("~");
	cookieWrite(name, valueJoined, 750);
}


function updateFavorites() {
	var favCookie=cookieArrayRead("favCookie");var favHtml=""; var newFavs=new Array();
	var ii=0;
	if (favCookie.length>0) {
		for (var i=0; i<favCookie.length; i++) {
			if (favCookie[i]) {
				var data=favCookie[i].split(",,");
				if (data.length==5) {
					newFavs[ii]=favCookie[i];
					favHtml=favHtml+'<div class="favoritHolder" id="fav'+i+'"><img class="browsePic" width="40" alt="'+data[1]+'" onmouseout="hidetrail();" onmouseover="showtrail(\'/'+data[2]+'\',\''+_utf8_decode(unescape(data[1]))+'\',\'\',\'5.0000\',\'\',\'1\',285,1);" src="/'+data[2]+'"/><a class="blue" href="'+data[0]+'">'+_utf8_decode(unescape(data[1]))+'</a><span class="LinkCityBr">'+_utf8_decode(unescape(data[4]))+'</span></div>';
					//favHtml=favHtml+'<a class="fav'+ii+'" id="fav'+ii+'" href="'+data[0]+'" title="'+data[1]+'"><span><img src="'+data[2]+'" alt="'+data[1]+'" width="40"/>'+data[1]+'</span></a>';
					ii++;
				}
			}
		}
	}else{
		favHtml = "Pridajte obľúbené";
	}
	
	
	$(".favorites_box").html(favHtml);
	cookieArrayWrite("favCookie", newFavs);
}

// private method for UTF-8 decoding
    function _utf8_decode(utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }
		
		string = string.replace(/[+]/g," ");

        return string;
    }


//-->
