var BaseURL = '';

Compare = new Object();
Compare.places = new Array();
Compare.setButton = function() {
	if(Compare.places.length < 2) {
		$("#compareButton").fadeOut();
		$("#comparePanel").fadeOut();
	} else {
		var text = '';
		if(Compare.places.length < 5)
			text = 'vybrané';
		else
			text = 'vybraných';
		
		$("#countOfPlacesForCompare").text('(' + Compare.places.length + ' ' + text + ')');
		$("#compareButton").fadeIn();
		$("#comparePanel").fadeIn();
	}
	
	$.cookies.set("compare", Compare.places.join(","));
}
Compare.compare = function() {
	if(Compare.places.length > 1) {
		var ids = Compare.places.join(",");
		$.cookies.set("compare", "");
		document.location = BaseURL + "compare.form?places=" + ids;
	}
}
removeFromArray = function(arr, key) {
	var returnArray = new Array();

	for(var i = 0; i < arr.length; i++) {
		if(arr[i] != key)
			returnArray[returnArray.length] = arr[i];
	}
	
	return returnArray;
}

$(document).ready(function() {           

        $('a[rel="blank"]').click(function() {
					window.open($(this).attr("href"));
					
					return false;
        });
        
        /*login open*/
        $('a.loginLink').toggle(function() {
                $('#loginOpen').fadeIn('fast');
        },function() {
                $('#loginOpen').fadeOut('fast');
        }); 
         
        /*place select*/
        $('#placesSelect a.select').toggle(function() {
               $('#placesSelectMore').show('fast');
               $('#content').css('z-index','20');
        },function() {
               $('#placesSelectMore').hide('fast');
               $('#content').css('z-index','10');
        });
     

        /*fb plugin*/
        $('#fbPlugin').mouseenter(function() {
                $('#fbPluginPlace').show('fast');
        }).mouseleave(function() {
                $('#fbPluginPlace').hide('fast');
        });                    

        /*selectbox*/
	$('.selectbox').selectBox();       

        /*homepage balls*/
        $('#hpBall1 a').mouseenter(function() {
                $('#hpBall2 a').children('.bg1').fadeOut('slow');
                $(this).children('.bg1').fadeIn('slow');
        });
        $('#hpBall2 a').mouseenter(function() {
                $('#hpBall1 a').children('.bg1').fadeOut('slow');
                $(this).children('.bg1').fadeIn('slow');
        });        

        /*styled scrollbar*/
	$('.scroll-pane').jScrollPane();
	
	/*date picker*/
        Date.format = 'dd.mm.yyyy';
        $(function()
        {
        	$('.date-pick').datePicker({clickInput:true})
        });

        /*input focus*/
	$('#formProfile .inputText input').focus(function() {
		$(this).parent().css('background-position','left bottom');
        });        
        $('#formProfile .inputText input').blur(function() {
            	$('#formProfile .inputText').css('background-position','left top');
        });  
        
        /*autoclear input*/
        var clearMePrevious = '';
        $('.clearOnClick').focus(function(){
                if($(this).val()==$(this).attr('title'))
                {
                        clearMePrevious = $(this).val();
                        $(this).val('');
                }
        });
        $('.clearOnClick').blur(function(){
                if($(this).val()=='')
                {
                        $(this).val(clearMePrevious);
                }
        });        
        
        /*link more*/
        var showText="Zobrazit více";
        var hideText="Zobrazit méně";
        $('.showHideMore').append('<a href="#" class="showMore">'+showText+'</a>');
        $('.moreInfo').hide();
        $('.showHideMore a').click(function() {
                if ($(this).html() == showText) {
                        $(this).html(hideText);
                        $(this).addClass("hideMore");
                }
                else {
                        $(this).html(showText);
                        $(this).removeClass("hideMore");
                }
                $(this).parent().prev('.moreInfo').toggle('slow');
                return false;
        });          
        
        /*lightbox*/
        $('.lightbox a').lightBox();
        
        /*firefox styles*/
        if ( $.browser.mozilla ) {
                $('.place .btnDetail').css('line-height', '26px');
                $('.place .placeCompare').css('line-height', '26px');
                $('.place .placeRemove').css('line-height', '26px');                                
        };
        
        /*form profile edit psw again show*/
        $('#formProfile .psw input').focus(function() {
              $('#formProfile .pswAgain').css('display', 'block');     
        });        
                 
                  
        /*place rating*/ 
        $('.box300Like a.icon').mouseenter(function() {    
                if($('.box300Like .icons li').is('.activeClicked')) {                 
                        $(this).parent('li').addClass('active');
                                                
                        $('li.activeClicked .fbAvatars').hide();
                        $(this).next('.fbAvatars').show();
                }else{
                        $('.defaultText').hide();
                        $(this).next('.fbAvatars').show();
                        $(this).parent('li').addClass('active');
                };                      
        }).mouseleave(function() {
                if($('.box300Like .icons li').is('.activeClicked')) {            
                        $('.box300Like .icons li').removeClass('active');
                        
                        $('.box300Like .fbAvatars').hide();
                        $('li.activeClicked .fbAvatars').show();
                }else{
                        $('.box300Like .fbAvatars').hide();
                        $('.box300Like .icons li').removeClass('active');
                        $('.defaultText').show();                
                };        
        });              
 
 
  
  $("a[rel='popup']").click(function() {
		$('html, body').animate(
		{
			scrollTop: 0
		}, 1000);
		
		var id = $(this).attr("href");
		
		$("#popup_base").show();
		$(id).show();
		
		return false;
  });
  
  $("a.layerClose").click(function() {
		if($(this).attr("returnfalse") == "false")
			return true;
		
		if($(this).attr("window") != "universal_overlay")
			$("div.popupWindow").hide();
		
		var showAfterClose = $(this).attr("showAfterClose");
		if(showAfterClose != undefined && showAfterClose != "") {
			$("#" + showAfterClose).show();
		} else {
			if($(this).attr("window") == "universal_overlay")
				$("#universal_overlay").hide();
			else
				$("#popup_base").hide();
		}
		
		return false;
	});
	
	$("#nav span.close").click(function() {
		document.location = $(this).attr("redir-to");
	});
	
	/* $("#compareForm input").change(function() { */
	
	$("input.placecompare").change(function() {
		var id = $(this).attr("placeid");
		if($(this).attr("checked") == "checked") {
			$("input[placeid='" + id + "']").attr("checked", "checked");
			Compare.places[Compare.places.length] = id;
			
			if(Compare.places.length == 1 && $(this).attr("skipoverlay") != "true") {
				$("#universal_overlay .layerError").hide();
				$("#universal_overlay .layerInfo").show();
				
				$("#universal_overlay .layerInfo h2").html("Středisko bylo přidáno do porovnání.");
				$("#universal_overlay .layerInfo p").html("Přidáte-li další středisko, v pravé části obrazovky se objeví tlačíko “Zobrazit porovnání”, pomocí kterého srovnání zobrazíte.");
				
				$("#universal_overlay").fadeIn('fast');
			}
		} else {
			$("input[placeid='" + id + "']").attr("checked", false);
			Compare.places = removeFromArray(Compare.places, id);
		}
		
		Compare.setButton();
	});
	
	$("#compareButton, #comparePanel").click(function() {
		Compare.compare();
		
		return false;
	});
	
	if($.cookies.get("compare") != null && $.cookies.get("compare") != "") {
		var data = $.cookies.get("compare").toString();
		Compare.places = data.split(",");
		
		for(var i = 0; i < Compare.places.length; i++) {
			$("input[placeid='" + Compare.places[i] + "']").attr("checked", "checked");
		}
		
		Compare.setButton();
	}
	
	$("input[event='passAfterClick']").focus(function() {
		$(this).attr("type", "password");
	});
        
});

         

