var $loading = $('#loading');

$(document).ready(function() {   

$(".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.7',					
className: 'caption-bottom',	
position: 'bottom',
spanWidth: '100%'
});
      
$("#search #ac_me").autocomplete("/v2/inc/suche.php", { minChars:3, matchSubset:1, matchContains:1, cacheLength:10, onItemSelect:navigation, formatItem:formatItem, selectOnly:1 });
$("#main-content li img, #wrap .xsnazzy img.thumbcam").lazyload({placeholder : "/v2/gfx/blank.gif"});
$("#wrap .xsnazzy .yada, #main-content li").click(function() {window.location = $('a:first', this).attr('href');});       
$("#skiresort").change(function(){
$loading.show(); 	 
document.location.href = 'http://sionvalais.com/' + $(this).children("option:selected").attr("value") + '';
});

populate_country();
	$('#country').change(function() {
        $loading.show(); 
		populate_country();
	});

$("#resorts").change(function(){
$loading.show(); 	 
document.location.href = '/webcams/' + $(this).children("option:selected").attr("value");
});
	         
$("#wrap input, #search input, #search select").hover(function() {
$(this).css('color', '#af9e99');
$(this).css('border-color', '#3F3837');
}, function() {
$(this).css('color', '#6f6461');
$(this).css('border-color', '#211A18');
return false; 
});

$("textarea").hover(function() {
$(this).css('color', '#FEDE65');
$(this).css('border-color', '#3F3837');
}, function() {
$(this).css('color', '#CFB55A');
$(this).css('border-color', '#211A18');
});
        
$('#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; 
});

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

$("#open").click(function(){
$("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.7");
	$("img.thumbcam").hover(function () {
	$(this).stop().animate({
	opacity: 1.0
	}, "slow");
	},
	function () {
	$(this).stop().animate({
	opacity: 0.7
	}, "slow");
	});
	
//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, // number = sensitivity threshold (must be 1 or higher)
     interval: 100, // number = milliseconds for onMouseOver polling interval
     over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
     timeout: 500, // number = milliseconds delay before onMouseOut
     out: megaHoverOut // function = onMouseOut callback (REQUIRED)
};

$("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(){
$("#skiresort").load("/v2/inc/sreview.php");

$.geekGaTrackPage('UA-1808769-1');	
});

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']);
        });
    });
}

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>";
}  
