var $loading = $('#loading');
var overlay = jQuery('<div id="overlay"> </div>');

$(document).ready(function() {   

var viewportHeight = window.innerHeight ? window.innerHeight : $(window).height();
var viewportWidth = window.innerWidth ? window.innerWidth : $(window).width();
jQuery.post('/v2/ajax/portview.php', {height: viewportHeight, width: viewportWidth});

$("body.livecams #wrap img").error(function () {
$(this).parent().parent().hide("fast");
pos3 = $(this).attr("id"); 
reportrefcam();
});

$('img.captify').captify({
speedOver: 'fast',
speedOut: 'fast',
hideDelay: 100,	
animation: 'slide',		
prefix: '',		
opacity: '0.8',					
className: 'caption-bottom',	
position: 'top',
spanWidth: '100%'
});

$(".display li img, .xsnazzy img.thumbcam").lazyload({placeholder : "http://snowcams.eu/gfx/blank.gif"});      
$("#search #ac_me").autocomplete("/v2/inc/suche.php", { minChars:3, matchSubset:1, matchContains:1, cacheLength:10, onItemSelect:navigation, formatItem:formatItem, selectOnly:1 });
$(".xsnazzy .yada, .display li, .display li, #coldfeet li").click(function() {window.location=$(this).find("a").attr("href");return false;});   

$("#skiresort").change(function(){
$loading.show(); 	 
document.location.href = 'http://snowcams.eu/' + $(this).children("option:selected").attr("value") + '';
});

$("#resorts").change(function(){
$loading.show(); 	 
document.location.href = '/webcams/' + $(this).children("option:selected").attr("value");
});

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

$("#skiresort").click(function(){
$("#skiresort").load("/v2/inc/sreview.php");
});
	
$(".sub").hover(function() {
overlay.appendTo(document.body);
}, function() {
$('#overlay').remove();
});
         
$("input").hover(function() {
$(this).css('color', '#B3CEEF');
$(this).css('border-color', '#27303F');
}, function() {
$(this).css('color', '#9cb0c9');
$(this).css('border-color', '#1D242F');
});
$("textarea").hover(function() {
$(this).css('color', '#FEDE65');
$(this).css('border-color', '#27303F');
}, function() {
$(this).css('color', '#CFB55A');
$(this).css('border-color', '#1D242F');
});    
$('#search input[type="text"]').focus(function() {
if (this.value == this.defaultValue){ 
this.value = '';
}
if(this.value != this.defaultValue){
this.select();
}
return false; 
});
$('#search input[type="text"]').blur(function() {
if ($.trim(this.value) == ''){
this.value = (this.defaultValue ? this.defaultValue : '');
}
return false; 
});

$('.xsnazzy .yada').hover(function() {
$(this).addClass('lightup');
}, function() {
$(this).removeClass('lightup');
return false; 
});

$("#open").click(function(){
$("#panel").load("/v2/ajax/panel.php"); 	
$("div#panel").slideDown("slow");	
return false; 
});		

$("#close").click(function(){
$("div#panel").slideUp("slow");	
return false; 
});		
	
$("#toggle a").click(function () {
$("#toggle a").toggle();
return false; 
});		

	$("img.thumbcam").css("opacity","0.8");
	$("img.thumbcam").hover(function () {
	$(this).stop().animate({
	opacity: 1.0
	}, 650);
	},
	function () {
	$(this).stop().animate({
	opacity: 0.8
	}, 450);
	});
	
//On Hover Over
function megaHoverOver(){

    $(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
    (function($) {
        //Function to calculate total width of all ul's
        jQuery.fn.calcSubWidth = function() {
            rowWidth = 0;
            //Calculate row
            $(this).find("ul").each(function() { //for each ul...
                rowWidth += $(this).width(); //Add each ul's width together
            });
        };
    })(jQuery); 

    if ( $(this).find(".row").length > 0 ) { //If row exists...

        var biggestRow = 0;	

        $(this).find(".row").each(function() {	//for each row...
            $(this).calcSubWidth(); //Call function to calculate width of all ul's
            //Find biggest row
            if(rowWidth > biggestRow) {
                biggestRow = rowWidth;
            }
        });

        $(this).find(".sub").css({'width' :biggestRow}); //Set width
        $(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin

    } else { //If row does not exist...

        $(this).calcSubWidth();  //Call function to calculate width of all ul's
        $(this).find(".sub").css({'width' : rowWidth}); //Set Width

    }
  
}
//On Hover Out
function megaHoverOut(){
  $(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
      $(this).hide();  //after fading, hide it
  });
}	

//Set custom configurations
var config = {
     sensitivity: 2, 
     interval: 100, 
     over: megaHoverOver, 
     timeout: 500, 
     out: megaHoverOut 
};

$("ul#tm li#rev .sub, ul#tm li#wcm .sub, .sub, ul#tm li#wht .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
$("ul#tm li#rev, ul#tm li#wcm, ul#tm li#wht").hoverIntent(config); //Trigger Hover intent with custom configurations		           
});

(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);

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

function populate_resorts() {
    $.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']);
        });
    });
}

function reportrefcam()
{
$.post("/v2/ajax/report.php", { id: pos3, autoreport: 1},
function(data10){	
});	
}

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

        $().ajaxStart(function() { 
        $loading.show(); 
        });
        
        $().ajaxStop(function() {
        $loading.hide("fast");
        });
        
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>";
}  
