jQuery(document).ready(function ($) {

	goHome();

	//$( 'li.page-item-6 a' ).attr( 'href', '/categoria/catalogo-generale/abbigliamento-medicale/' );

	$( 'div.preview > a' ).qtip({ 
		style: {
			name: 'open'
		},
		position: {
			corner: {
				target: 'rightMiddle',
				tooltip: 'leftMiddle'
			},
			adjust: {
				x: -10
			}
		}
	});
	
	$( '#listSameProducts > a' ).qtip({
		style: {
			name: 'open'
		},
		position: {
			corner: {
				target: 'topMiddle',
				tooltip: 'bottomMiddle'
			},
			adjust: {
				y: 10
			}
		}
	});
	
	//#listSameProducts > a
	
	if ($.isPage('prodotti')) {
		startScrollPane();
	}
	else if ($.isPage('categoria')) {
		//zoom();	
		startScrollPane();
	} else if ($.isPage('prodotto')) {
		startScrollPane();
	} else if ($.isPage('chi-siamo') || $.isPage('checkout') || $.isPage( 'condizioni-di-vendita' )  ) {
		startScrollPane();
	} else if ($.isPage('dove-siamo')) {
		initializeMap();
	} else if ($.isPage('richiedi-il-catalogo')) {
		$('form[name=richiediCatalogo]').validate({
			keyUp: true,
			shaker: false,
			returnClass: true
		});

		$('form[name=richiediCatalogo]').submit( function()
		{
			if ( $(this).hasClass('validato') ) {
				$.post( '/wp-content/themes/open/lib/validateEmail.php', { email: $('input.mail').val() }, function (r) {
					if (r == 'ko') {
						$('input.mail').addClass('error');
						$('input.mail').parent().children('span.required').show();
					}
					else {
						var nome = $('input[name=nome]').val();
						var cognome = $('input[name=cognome]').val();
						var email = $('input[name=email]').val();
						var azienda = $('input[name=azienda]').val();
			
						$.post('/wp-content/themes/open/lib/richiesteCatalogo.php', {
							  nome: nome,
							  cognome: cognome,
							  email: email,
							  azienda: azienda
						  });
			
						var ajaxPage = $('input[name=ajaxPage]').val();
						var link = 'Scarica il catalogo facendo <a href="' + ajaxPage + 'scaricaCatalogo.php" target="_blank">click qui!</a>';
						$('p.theLink').html(link).show();
					  }
				  });
			
				  return false;
			}
		});

	}
});



//======FUNCTIONS======/
// @info 	Check current pag
// @usage	if($.isPage('pageName'))
jQuery.isPage = function (checkFor) {

	var url = window.location.pathname;
	var page = url.split('/');

	if (checkFor == page[1]) {
		return true;
	} else {
		return false;
	}

}

//Move goHome
function goHome() {

	//
	jQuery(".goTo").hover(

	function () {
		jQuery(this).stop().animate({
			marginTop: ("10px")
		}, 100);
	},

	function () {
		jQuery(this).stop().animate({
			marginTop: ("0px")
		}, 300);
	})
}


//Magnify
function zoom() {

	var options = {
		zoomWidth: 200,
		zoomHeight: 200,
		xOffset: 10,
		yOffset: 0,
		position: "right",
		//and MORE OPTIONS
		showPreload: true,
		lens: false

	}
	jQuery('.zoom').jqzoom(options);

}


//Scrollpane
function startScrollPane() {

	jQuery('.scrollpane').jScrollPane({
		showArrows: true,
		scrollbarWidth: 15,
		arrowSize: 14
	});

}


//Google map
function initializeMap() {
	if (GBrowserIsCompatible()) {

		//Setto le coordinate
		var coordsNewClinicLife = new GLatLng(45.650679, 9.549462);

		//Creo la mappa 
		var mapNewClinicLife = new GMap2(document.getElementById("map"));

		mapNewClinicLife.setCenter(coordsNewClinicLife, 12);
		mapNewClinicLife.setUIToDefault();

		//Definisco il path per la tip.png
		var tip = new GIcon(G_DEFAULT_ICON);
		tip.iconSize = new GSize(200, 80);
		tip.shadowSize = new GSize(200, 80);
		tip.image = "http://www.openantinfortunistica.it/wp-content/themes/open/images/tipOpen.png";

		markerOptions = {
			icon: tip
		};

		//Creo le tip
		mapNewClinicLife.addOverlay(new GMarker(coordsNewClinicLife, markerOptions));

	}
}

$.fn.qtip.styles.open = { // Last part is the name of the style
	width: 180,
	padding: 8,
	border: {
		width: 3,
		radius: 5,
		color: '#ffc05d'
	},
	background: '#faf9f9',
	color: '#006166',
	tip: true
};
