$(document).ready(function(){

	if (navigator.appVersion.indexOf("MSIE") != -1){
		$('#headerwrapper').css({'position':'relative','left':'-5px'});
	}

	quickmenuDefault();
	if( $( '#productShop li' ).length > 1 ){
		$('#productShop').easySlider({
			numeric : true,
			speed : 600,
			numericId : 'controlsproductShop',
			auto : true,
			continuous : true
		});
	}
	controlswidth( 'controlsproductShop' );
	if( $( '#productTenFiftyFive li' ).length > 1 ){
		$('#productTenFiftyFive').easySlider({
			numeric : true,
			speed : 400,
			numericId : 'controlsproductTenFiftyFive',
			auto : true,
			continuous : true
		});
	
		controlswidth( 'controlsproductTenFiftyFive' );
		
	}

	
	/* Setta il valore di default nello slider SHOP
	   e al click assegna l'id univoco del prodotto */
	
	//productShopStickerValue();
	stickerhover(); // hover sullo sticker prodotti shop
	
	/* Hover Menu */
	
	rolloverMenuSecondary(); // hover menu FUN

	quickmenu(); // hover quinck menu StoreLocator | Newsletter | Search
	
	/* Funzioni del QuickMenu */
	
	
	quickmenuSearch();
	quickmenuNewsletter();

	scrollDefault(); // funzione del menu StoreLocator

	/* Scroll Page */
	
	
	scrollToTop(); //scroll top page
	
	 $('body').click(function() {
	 	if( !$('#dropdownCountry').hasClass('hide') ){
			$('#dropdownCountry').addClass('hide');
			$('li.city .city-opacity').remove();
		}
		
		if( !$('#dropdownCity').hasClass('hide') ){
			$('#dropdownCity').addClass('hide');
		}
	 });
	 
	 $('.country, .city, p.errorcity').click(function(event){
     	event.stopPropagation();
 	 });
 	 
 	 
 	 
 	 $('p.errorcity').click(function(){
 	 	$('#dropdownCountry').removeClass('hide');
 	 	$('#dropdownCity').addClass('hide');
 	 });
	
	
	
	$.getJSON('/services/getCountry.php', function(data) {
		var country = 'it';
		if (data.response != null && data.response && data.data.country != null && data.data.country != '') {
			country = data.data.country;
		}
		$('#countryFlag').css('background-image', 'url(http://55dsl.com/wp-content/themes/default/images/flags/' + country + '.jpg)');
	});
	
	$.getJSON('/services/getCart.php', function(data) {
		var products = 0;
		if (data.response != null && data.response && data.data.n_prod != null && data.data.n_prod != '') {
			products = data.data.n_prod;
		}
		$('#cartCount').html(products);
	});
	
});

function scrollToTop(){
	$('.backtotop').click(function(){
		$('html, body').animate({scrollTop:417}, 1500)
	});
}

function getOffsetY(){
	
	var  scrOfY = 0;
		if( typeof( window.pageYOffset ) == 'number' ) {
			//Netscape compliant
			scrOfY = window.pageYOffset;
		} else if( document.body && ( document.body.scrollTop ) ) {
			//DOM compliant
			scrOfY = document.body.scrollTop;
		} else if( document.documentElement && (  document.documentElement.scrollTop ) ) {
			//IE6 standards compliant mode
			scrOfY = document.documentElement.scrollTop;
		}
	
	return scrOfY;
	
}

$(window).load(function() {
	mCustomScrollbars();
});

function share( url, title){
	window.open('http://www.facebook.com/sharer.php?u='+url+'&t='+title,'sharer','toolbar=0,status=0,width=626,height=436');return false;
}

function mCustomScrollbars(){
	
	/* 
	malihu custom scrollbar function parameters: 
	1) scroll type (values: "vertical" or "horizontal")
	2) scroll easing amount (0 for no easing) 
	3) scroll easing type 
	4) extra bottom scrolling space for vertical scroll type only (minimum value: 1)
	5) scrollbar height/width adjustment (values: "auto" or "fixed")
	6) mouse-wheel support (values: "yes" or "no")
	7) scrolling via buttons support (values: "yes" or "no")
	8) buttons scrolling speed (values: 1-20, 1 being the slowest)
	*/
	
	//$("#dropdownCountry").mCustomScrollbar("vertical",100,"easeOutCirc",1.05,"fixed","no","no",0);
	//$("#dropdownCity").mCustomScrollbar("vertical",100,"easeOutCirc",1.05,"fixed","no","no",0);
	if( $("#sidebarSlider").length > 0 ){
		$("#sidebarSlider").mCustomScrollbar("vertical",100,"easeOutCirc",1.05,"fixed","no","no",0);
	}
	
	
}

/* function to fix the -10000 pixel limit of jquery.animate */
$.fx.prototype.cur = function(){
    if ( this.elem[this.prop] != null && (!this.elem.style || this.elem.style[this.prop] == null) ) {
      return this.elem[ this.prop ];
    }
    var r = parseFloat( jQuery.css( this.elem, this.prop ) );
    return typeof r == 'undefined' ? 0 : r;
}

/* function to load new content dynamically */
function LoadNewContent(id,file){
	$("#"+id+" .customScrollBox .content").load(file,function(){
		mCustomScrollbars();
	});
}

function scrollDefault(){
	
	$('li.country').click(function() {
		$('#dropdownCity').addClass('hide');
		if( $('#dropdownCountry').hasClass('hide') ) {
			appendDropdown( this, '#dropdownCountry' );
			// $("#dropdownCountry").mCustomScrollbar("vertical",100,"easeOutCirc",1.05,"fixed","no","no",0);
			$('#dropdownCountry').removeClass('hide');
			$(this).parent('ul').children('li.city').append($('<div/>').addClass('city-opacity'));
		} else {
			$('#dropdownCountry').addClass('hide');
			$('li.city .city-opacity').remove();
		}
	});
	
	
	
	$('li.city').click(function() {
		appendDropdown( this, '#dropdownCity' );
		/*
		$('div.dragger_container').show();
		$('div.dragger').show();
		*/
		$('li.city .city-opacity').remove();
		$('#dropdownCountry').addClass('hide');
		if( $('#dropdownCity').hasClass('hide') ){
			$('#dropdownCity').removeClass('hide');
			$('#dropdownCity').mCustomScrollbar("vertical",100,"easeOutCirc",1.05,"fixed","no","no",0);
		} else {
			$('#dropdownCity').addClass('hide');
		}
		
	});
	
	
	$('.chooseCountry').click(function(){
		$('#NewsletterCountry').css({'left':'0'});	
				
		if( $('#NewsletterCountry').attr('class') ){
			$('#NewsletterCountry').removeAttr('class');
		} else {
			$('#NewsletterCountry').removeAttr('style');
			$('#NewsletterCountry').attr('class','hide');
		}
	});
	
	$('#NewsletterCountry p').click(function(){
			
		$('.selectNewsletter').html( $(this).text() ).addClass('countrySelected');
		$('#NewsletterCountry').css({'left':'-9999px'});
			
	});
}

function appendDropdown(control, dropdown){
	var ofs = $(control).offset();
	isQuickDropdown = $(control).parent().parent().hasClass('store-up');
	
	//$(dropdown).appendTo('body');
	$(dropdown).css('left', ofs.left + 'px');
	$(dropdown).css('top', ofs.top + $(control).outerHeight(isQuickDropdown) + 'px');
	
	if ( dropdown == '#dropdownCity' ) {
		$(dropdown).width($(control).outerWidth());
		$(dropdown + ' .container').width($(control).outerWidth());
		$(dropdown + ' .content').width($(control).outerWidth());
	}
	
	if (dropdown == '#dropdownCountry' && !isQuickDropdown) {
		//$('#dropdownCountry div.content').children('div').removeClass('country-col');
		
		$('#dropdownCountry').addClass('vertical');
	} else {
		//$('#dropdownCountry div.content').children('div').addClass('country-col');
		$('#dropdownCountry').removeClass('vertical');
	}
}

function quickmenu() {
	
	$('#quickmenu li a').click(function(){
		
		var page = $(this).text();
		
		switch( page ){
			
			case 'Store Locator':
				activeStoreLocator();
				break;
				 
			case 'Newsletter':
				activeNewsletter();
				break;
				
			case 'Search':
				activeSearch();
				break;
		}
		
	});
}

function quickmenuSearch(){
	
	var valDefault = 'Type here...';
	$('#search').val( valDefault );
	
	$('#search').click(function(){
		if( $(this).val() == valDefault ){
			$('input#search').val('');
		}
	});
	
	$('#search').blur(function(){
		if( $(this).val() == '' ){
			$(this).val( valDefault );
		}
	});
	
	$('.cancel').click(function(){
		$('input#search').val('').focus();
	});
	
}

function quickmenuNewsletter(){
	
	var valDefault = 'Write your email here';
	
	$('#newsletter').click(function(){
		if( $(this).val() == valDefault ){
			$('input#newsletter').val('');
		}
	});
	
	$('#newsletter').blur(function(){
		if( $(this).val() == '' ){
			$(this).val( valDefault );
		}
	});
	
}

/*
Codice per il controllo del #JSON */

var codearray = new Array();
var productShop = new Array();

var defaultUrl = 'http://shop.55dsl.com';

$.ajax({
	url: "http://shop.55dsl.com/shop/service/get_products_list.php",
	dataType: "jsonp",
	success: function( jsonShop ) { 
		if( jsonShop.response ){
			for(i=0; i<jsonShop.data.length; i++){
				productShop[i] = jsonShop.data[i];
			}
		}
		
		
		
	}
});

//call at every easyslider clock
function setValueProductShop( index ){
	$('#productShop li').each(function( i ){
		if( i == index ){
			
			$(this).addClass('selected');
			controlCodeToShop( i, productShop );
		} else {
			$(this).removeClass('selected');
		}
	});
}

function controlCodeToShop( ncode, productShop ){
	if( productShop ){
		for( i=0; i<productShop.length; i++){	
			if( codearray[ncode] == productShop[i].code ){
				setUrlShop( productShop[i].url );
			}
			else if ( codearray[ncode] == '' ) {
				setUrlShop( defaultUrl );
			}
		
		}
		
	} 

}

function setUrlShop( url ){
	
	$('#sticker a').attr('href', url );
}

function stickerhover(){
	
	$('#sticker,.sticker').mouseover(function(){
		
		$(this).width('90px');
		$('a.like', $(this)).css('left','84px');
		$('a', $(this)).css('text-indent','0');
		
	});
	
	$('#sticker, .sticker').mouseout(function(){
		
		$(this).width('50px');
		$('a.like', $(this)).css('left','42px');
		$('a', $(this)).css('text-indent','-9999px');
		
	});
}

function rolloverMenu(){
	
	var idBody = $('body').attr('id');
	var memActive = '';
	var active = $('#menu li.active').text();
	
	if( idBody != 'home' || idBody != 'search' || idBody != 'store' || idBody != 'newsletter' || idBody != 'contact_us' || idBody != 'about_us' || idBody != 'legal_terms' ){
		
		$('#menu').mouseover(function(){
			$('#menu ul.primary li').removeClass('active');
			
		});
		
		$('#menu').mouseout(function(){
			
			$("#menu ul.primary li").each(function () {
          		var act = $(this).text();
          		if( act == active ){
          			$(this).addClass('active');
          		}
        	});
		});
	}
	
}

function rolloverMenuSecondary(){
	
	$('li.fun').hover(function(){
		$('#submenufun').show();
	});
	
	$('#submenufun').mouseleave(function(){
		$(this).hide();
	});
	
	$('li.blog').hover(function(){
		$('#submenufun').hide();
	});
	
}

function controlswidth( idcontrol ){
	
	var singlecontrols = $('ol#'+idcontrol+' li a').width();
	var widthcontrol = $('ol#'+idcontrol+' li').length;
	var widthtot = 0;
	

	$('ol#'+idcontrol+'').width(function(){
		
		for(i=1; i<widthcontrol; i++){
			widthtot += singlecontrols;
		}
		
		$('ol#'+idcontrol+'').width(widthtot + 20);

	});
}

function opacitySlider(){

	$('.opacitySlider').append('<div id="left" class="opacity"></div>');
	$('.opacitySlider').append('<div id="right" class="opacity"></div>');
	
	setOpacityWidht();
	
	$(window).resize(function(){
		setOpacityWidht();
	});

}

function setOpacityWidht(){
	var ww = $(window).width();
	var size = (ww - 850)/2;
	
	if( size < 0 ){
		size = 0;
	}
	
	$('.opacity').width(size + 1 );
}



/* Load Default */

function quickmenuDefault() {
	
	var SelectDefault = 'storelocator';
	$('#quickmenu li.' + SelectDefault).addClass('active');
	$('#quickmenu .response' + SelectDefault).addClass('active');
	
	$('#quickmenu li.newsletter').css({'background-position':'-30px 0px'});
	$('#quickmenu li.search').css({'background-position':'0px 0px'});
}

/* Function Active/Disactive */

function activeStoreLocator() {
	$('#quickmenu li.newsletter').removeClass('active');
	$('#quickmenu li.search').removeClass('active');
	$('#quickmenu li.storelocator').addClass('active');
	
	$('#quickmenu .responsenewsletter').removeClass('active');
	$('#quickmenu .responsesearch').removeClass('active');
	$('#quickmenu .responsestorelocator').addClass('active');
	
	$('#quickmenu li.newsletter').css({'background-position':'-30px 0px'});
	$('#quickmenu li.search').css({'background-position':'0px 0px'});

}

function activeNewsletter() {
	$('#quickmenu li.storelocator').removeClass('active');
	$('#quickmenu li.search').removeClass('active');
	$('#quickmenu li.newsletter').addClass('active');
	
	$('#quickmenu .responsesearch').removeClass('active');
	$('#quickmenu .responsestorelocator').removeClass('active');
	$('#quickmenu .responsenewsletter').addClass('active');
	
	$('#quickmenu li.newsletter').css({'background-position':'0px 0px'});
	$('#quickmenu li.search').css({'background-position':'-30px 0px'});
	
	$('#dropdownCity, #dropdownCountry').addClass('hide');
}

function activeSearch() {
	$('#quickmenu li.storelocator').removeClass('active');
	$('#quickmenu li.newsletter').removeClass('active');
	$('#quickmenu li.search').addClass('active');

	$('#quickmenu .responsestorelocator').removeClass('active');
	$('#quickmenu .responsenewsletter').removeClass('active');
	$('#quickmenu .responsesearch').addClass('active');
	
	$('#quickmenu li.newsletter').css({'background-position':'0px 0px'});
	$('#quickmenu li.search').css({'background-position':'0px 0px'});
	
	$('#dropdownCity, #dropdownCountry').addClass('hide');
}

function addHoverFade(parent, child){
	
	$(parent).hover(function(){
		$(child, $(this)).fadeIn();
	}, function(){
		$(child, $(this)).fadeOut();
	});
}

function setCatolog( nameCat ){
	
	$('#catalog').remove();
	$('#prevBtn, #nextBtn').remove();
	
	$('#main').append('<div id="catalog"></div>');
	$('#catalog').html('');
	$('#catalog').html('<ul></ul>');
	
	for (var item in catalog[nameCat]){
	
		$('#catalog ul').append('<li class="item_'+item+'"><img src="'+ catalog[nameCat][item].image+ '" /></li>');
		
		var hotspotArray = catalog[nameCat][item].hotspot;
		
		for( var hotspotIndex in hotspotArray){
		
			$('#catalog ul li.item_'+item).append('<a href="#" title="#" class="cl_'+hotspotIndex+'" /></a>');
			$('#catalog ul li.item_'+item+' a.cl_'+hotspotIndex )
				.css('top',hotspotArray[hotspotIndex].y + 'px')
				.css('left',hotspotArray[hotspotIndex].x + 'px');
				
			$('#catalog ul li.item_'+item+' a.cl_'+hotspotIndex ).attr('href',hotspotArray[hotspotIndex].link);
		
		}
		
		
	}
	
	
	$('#catalog').easySlider({
		speed : 400
	});

}

