(function($){var pageTracker;$.geekGaTrackPage=function(account_id){var host=(("https:"==document.location.protocol)?"https://ssl.":"http://www.");var src=host+'google-analytics.com/ga.js';$.ajax({type:'GET',url:src,success:function(){pageTracker=_gat._getTracker(account_id);pageTracker._trackPageview();},error:function(){throw"Unable to load ga.js; _gat has not been defined.";},dataType:'script',cache:true});};
$.geekGaTrackEvent=function(category,action,label,value){if(typeof pageTracker!=undefined){pageTracker._trackEvent(category,action,label,value);}else{throw"Unable to track event; pageTracker has not been defined";}};})(jQuery);

var $loading = $('#loading');
var $iconnav = $('#iconnav');
function populate_country() {
    $.getJSON('/v2/ajax/fetch_resort.php', {country:$('#country').val()}, function(data) {
        var select = $('#resorts');
        var options = select.attr('options');
        $('option', select).remove();
        $.each(data, function(index, array) {
        options[options.length] = new Option(array['resort']);
        });
    });
}

$(document).ready(function() {   
$('.swap, #currentswitch, #activecam, #xcams, #advertise, #newurl, #suggest').hide(); 

$("#country").click(function(){
// populate_country();
	$('#country').change(function() {
		$loading.show(); 
		populate_country();
	    $("#resorts").show("fast");	
	});
});

	$("[id^=rating_]").hover(function() {

		rid = $(this).attr("id").split("_")[1];
		$("#rating_"+rid).children("[class^=star_]").children('img').hover(function() {

			$("#rating_"+rid).children("[class^=star_]").children('img').removeClass("hover");

			var hovered = $(this).parent().attr("class").split("_")[1];
	
			while(hovered > 0) {
				$("#rating_"+rid).children(".star_"+hovered).children('img').addClass("hover");
				hovered--;  
			}
		},
		function() {
		 //OUT
		$("[id^=rating_]").children("[class^=star_]").children('img').removeClass("hover");
		});

	});

	$("[id^=rating_]").children("[class^=star_]").click(function() {

		var current_star = $(this).attr("class").split("_")[1];
		var rid = $(this).parent().attr("id").split("_")[1];

		$('#rating_'+rid).load('/fivestars/send.php', {rating: current_star, id: rid});

	});
		   

	
	/* This flag will prevent multiple comment submits: */
	var working = false;
				
	$('#addCommentForm').submit(function(e){	
	
 		e.preventDefault();
		if(working) return false;
		
		working = true;
		$('#submit').val('Working..');
		$('span.error').remove();
		
		$.post('/comment/submit.php',$(this).serialize(),function(msg){

			working = false;
			$('#submit').val('Submit');
			
			if(msg.status){

				$(msg.html).hide().insertBefore('#addCommentContainer').slideDown();
				$('#msg').val('');
			}
			else {

				$.each(msg.errors,function(k,v){
					$('label[for='+k+']').append('<span class="error">'+v+'</span>');
				});
			}
		},'json');

	});
	

$("#resorts").change(function(){	 
document.location.href = '/webcams/' + $(this).children("option:selected").attr("value");
});
   
	$('#activecam').fadeTo(300,1,function(){
	$('#activecam').fadeIn("slow");
	$('.swap').show(); 
	});
	
	$('#xcams').fadeTo(3500,1,function(){
    $('#xcams').fadeIn("slow");
  	});
  
  	$iconnav.fadeTo("slow", 0.5);
	 $iconnav.hover(function(){
	 $iconnav.fadeTo("slow", 1.0); 
	 },function(){
	 $iconnav.fadeTo("slow", 0.5); 
	 });     
  	
 $('img.captify').captify({
		speedOver: 'fast',
		speedOut: 'normal',
		hideDelay: 800,	
		animation: 'slide',		
		prefix: ' ',		
		opacity: '0.8',					
		className: 'caption-bottom',	
		position: 'top',
		spanWidth: '100%'
 });
	
    $("#search #ac_me").autocomplete("/v2/inc/suche.php", { minChars:4, matchSubset:1, matchContains:1, cacheLength:10, onItemSelect:navigation, formatItem:formatItem, selectOnly:1 });
    
     $("a.thumb img").error(function () {
     $(this).parent().parent().hide("fast");
     pos3 = $(this).attr("id"); 
     reportrefcam();
     });
          
    function reportrefcam()
	{
	$.post("/v2/ajax/report.php", { id: pos3},
	function(data10){
    });	
	}
      
	$("#open").click(function(){
		$("#panel").load("/v2/ajax/panel.php"); 
		$("div#panel").slideDown("slow");	
	});	
	
	$("#close").click(function(){
		$("div#panel").slideUp("slow");	
	});		
	
	$("#toggle a").click(function () {
		$("#toggle a").toggle();
	});		
	
    // $("ul.showcase .thumb img").lazyload({placeholder : "/v2/gfx/blank.gif", effect : "fadeIn"});
                 
	 $(".addcat").hover(function() {
     $(this).css('cursor', 'crosshair');
     });	  
    
    $(".vote").css({opacity: 0.7});
    $(".vote").hover(function() {
     $(this).css({opacity: 0.9});
     }, function() {
$(this).css({opacity: 0.7});
return false; 
});
       	       


      	  
	 $('#thanks').fadeTo(10000,1,function(){
     $('#thanks').hide("slow");
  	 });
  
  $(".swap span").hover(function() {
$(this).css('color', '#fff');

}, function() {
$(this).css('color', '#9cb0c9');

return false; 
});

$("textarea").hover(function() {
$(this).css('color', '#FEDE65');
$(this).css('border-color', '#27303F');
}, function() {
$(this).css('color', '#CFB55A');
$(this).css('border-color', '#1D242F');
});

$("input").hover(function() {
$(this).css('border-color', '#27303F');
}, function() {
$(this).css('border-color', '#1D242F');
});
        
$('#search input[type="text"],input#city[type="text"], input#author[type="text"],input#email[type="text"],input#url[type="text"]').focus(function() {
    		    if (this.value == this.defaultValue){ 
    		    	this.value = '';
				}
				if(this.value != this.defaultValue){
	    			this.select();
	    		}
});
$('#search input[type="text"],input#city[type="text"], input#author[type="text"],input#email[type="text"],input#url[type="text"]').blur(function() {
    		    if ($.trim(this.value) == ''){
			    	this.value = (this.defaultValue ? this.defaultValue : '');
				}
});


    		
});

$(window).load(function(){
$.geekGaTrackPage('UA-1808769-1');		
});	

 function navigation(li)
       {
       if (li.extra)
       {
               location.href = "/webcams/" + li.extra[0];
       }
       }

        $().ajaxStart(function() { 
        $loading.show(); 
        });
        
        $().ajaxStop(function() {
        $loading.hide("fast");
        });
        

jQuery.fn.delay = function(time,func){
	this.each(function(){
		setTimeout(func,time);
	});
	
	return this;
};

function selectItem(li) {
	if (li.extra) {
	return "<a href='/webcams/" + row[0] + "'>" + row[1] + "</a>";
	}
}

function formatItem(row) {
	return "<a href='/webcams/" + row[0] + "'>" + row[1] + "</a>";
}      
