if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i)) {
var viewportmeta = document.querySelectorAll('meta[name="viewport"]')[0];
if (viewportmeta) {
viewportmeta.content = 'width=device-width, minimum-scale=1.0, maximum-scale=1.0';
document.body.addEventListener('gesturestart', function() {
viewportmeta.content = 'width=device-width, minimum-scale=0.25, maximum-scale=1.6';
}, false);
}
}


//Home Page Cycle
//////////////////////////////////////////////
function slider() {
	if ( $('#home').length > 0) {
	 $('.slideshow').cycle({
			fx: 'fade', // transition type
			speed: 2000,
			timeout: 6000
		});
	}
}
//////////////////////////////////////////////


//Modal Popup
//////////////////////////////////////////////
function popup() {
	$('#overlay, #popup').hide(); //popup
	$('a.access, #popup a.close').click(function() {
	$("#overlay, #popup").fadeToggle('slow');
	return false;
  });
}
//////////////////////////////////////////////


function modalLaunch() {
	//Select all anchor tag with rel set to tooltip
	$('.modal').click(function(e) {
		//stop the click
		e.preventDefault();
		//grab the URL
		var link = $(this).attr("href");
		ajaxModal(link);
	});
}

function ajaxModal(path) {
	var url = path;
	//load the content via AJAX
	//trouble loading? use the proxy: "/dev/macadamia/simple-proxy.php?url="+url+"&mode=native"
	$.get(url, function(data) {
		//loading complete, append data
		$('body').append('<div id="modalBox"></div>');
		$('#modalBox').html(data);
		//$('#overlay').hide();
		$('#overlay').show();
		$('#overlay').fadeIn('fast');
		
		//show the overlay once everything is loaded
		$('#overlay').load(function () {
			$('#overlay').fadeIn('fast');
		  });
		
		//close and remove the overlay
		$('#overlay a.close').click(function(e) {
			e.preventDefault();
			$('#overlay').fadeOut('fast').remove();
		});
		
	});
}


//Simple Brown Tooltip
//////////////////////////////////////////////
function simpletooltip() {
	xOffset = 10;
	yOffset = 20;		
	$("a.tooltip").hover(function(e){	
		this.t = this.title;
		this.title = "";									  
		$("body").append("<p id='tooltip'>"+ this.t +"</p>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#tooltip").remove();
    });	
	$("a.tooltip").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};
	
//////////////////////////////////////////////


//Tooltips for product category page
//////////////////////////////////////////////

function tooltip(){	
	if ( $('a.preview').length > 0) {
		xOffset = 10;
		yOffset = 30;
		$("a.preview").hover(function(e){
			this.t = this.title;
			this.title = "";	
			//var c = (this.t != "") ? "<br/>" + this.t : "";
			//$("body").append("<p id='preview'><img src='"+ this.href +"' alt='Image preview' />"+ c +"</p>");								 
			$(this).parent().children('.tool-box')
				.css("top",(e.pageY - xOffset) + "px")
				.css("left",(e.pageX + yOffset) + "px")
				.fadeIn("fast");						
	    },
		function(){
			this.title = this.t;
			$(this).parent().children('.tool-box').stop(true, true).fadeOut("fast");
	    });	
		$("a.preview").mousemove(function(e){
			$(this).parent().children('.tool-box')
				.css("top",(e.pageY - xOffset) + "px")
				.css("left",(e.pageX + yOffset) + "px");
		});	
	}		
};

function remoteTip(){	
	if ( $('.prod-list a').length > 0) {
		var xOffset = 25;
		var yOffset = -25;
		var toolTip;
		$(".prod-list a").hover(function(e){
			//match the text of the anchor to the text of the tooltip
			var text = $(this).html();
			//find the related tooltip content
			toolTip = $('.tooltips').find('span:contains('+text+')').parent().parent();
			//find the location target and define coordinates
			var target = $('.tooltips').find('span:contains('+text+')').parent().parent().parent().find('a.preview span');
			var targetX = target.offset().left;
			var targetY = target.offset().top;
			//show it
			toolTip
				.css("top",(targetY - yOffset) + "px")
				.css("left",(targetX + xOffset) + "px")
				.fadeIn("fast");						
	    },
		function(){
			//hide it
			toolTip.stop(true, true).fadeOut("fast");
	    });	
	}
};

//////////////////////////////////////////////

//Shopping
//////////////////////////////////////////////

function addToCartPro() {
	
	//click function for selecting sizes to purchase
	$('.add_to_cart_pro .preview').click(function(event){
		event.preventDefault();
		//don't do anything if the user clicks on the selected button
		if($(this).hasClass('active') == true){
			//remove the active state
			$(this).removeClass('active');
			$(this).parent().find('.overlay-pro-order').hide();
		}else{
			//add the active state
			$('.overlay-pro-order').hide();
			$(this).addClass('active');
			$(this).parent().find('.overlay-pro-order').show();
		}
		
		$('.cancel').click(function(event){
			event.preventDefault();//stop the click
			$('.add_to_cart_pro .active').removeClass('active');//remove the active state from the button
			$('.overlay-pro-order').hide();//hide the div
		});
		
	});
	
	
	//ajax submit
	$(".add-retail-items-pro").click(function(event){
		event.preventDefault();
		//submit the form via ajax
	     $(this).closest('.add_to_cart_pro').ajaxSubmit({
			success: function(data) {
				//after a successful submit...
		        $('.checkout-box').load('/_includes/cart_pro_quickcart');
	        }//end success
	    });//end ajaxsubmit
	});//end click
	
}

function addToCartRetail() {
	
	//click function for selecting sizes to purchase
	$('.purchase .sku').click(function(event){
		event.preventDefault();
		//don't do anything if the user clicks on the selected button
		if($(this).hasClass('active') == true){
			//remove the active state
			$(this).removeClass('active');
			//set the hidden field back to 0
			$(this).children('input[name*="quantity"]').val(0);
		}else{
			//add the active state
			$(this).addClass('active');
			//set the hidden field to 1
			$(this).children('input[name*="quantity"]').val(1);
		}
	});
	
	//ajax submit
	$(".add-retail-items-cart").click(function(event){
		event.preventDefault();
		//submit the form via ajax
	     $(".add_to_cart_retail").ajaxSubmit({
			success: function(data) {
				//after a successful submit...
		        $('body').append('<div id="modalBox"></div>'); 
	
				$('#modalBox').load('/_includes/cart_modal', function() {
					$('#overlay').show();
					//close and remove the overlay
					$('#overlay a.close').click(function(event) {
						event.preventDefault();
						$('#modalBox').fadeOut('fast').remove();
					});
				});//end load
	        }//end success
	    });//end ajaxsubmit
	});//end click
	
	
	//ajax submit
	$(".add-items-retail").click(function(event){
		event.preventDefault();
		//submit the form via ajax
	     $(this).closest('.add_to_cart_retail').ajaxSubmit({
			success: function(data) {
				//after a successful submit...
		        $('body').append('<div id="modalBox"></div>'); 
	
				$('#modalBox').load('/_includes/cart_modal', function() {
					$('#overlay').show();
					//close and remove the overlay
					$('#overlay a.close').click(function(event) {
						event.preventDefault();
						$('#modalBox').fadeOut('fast').remove();
					});
				});//end load
	        }//end success
	    });//end ajaxsubmit
	});//end click
	
}
//////////////////////////////////////////////


//Cookies
//////////////////////////////////////////////

function setCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function deleteCookie(name) {
    setCookie(name,"",-1);
}

//region switcher in the toolbar
function setRegion() {
	$('#country a').click(function(event){
		//stop the click
		event.preventDefault();
		//grab the hash and remove the #
		var hash = $(this).attr('href'); //grab the hash value
		var splitUri = hash.split('#');
		var country = splitUri[1];
		//set a cookie containing the country code for one day
		setCookie('exp_region', country, 1);
		//empty the cart by calling a template that contains a empty cart tag
		$.get('/_includes/cart_clear', function(data) {
		//refresh the page to show the updated content
			location.reload(true);
		});
	});
}

//////////////////////////////////////////////


//Toolbar
//////////////////////////////////////////////
function utilityDrop(selector) {
	//click function for selecting sizes to purchase
	$('nav.secondary .'+selector+' a.select').click(function(event){
		event.preventDefault();
		if($(this).hasClass('active') == true){
			//remove the active state
			$(this).removeClass('active');
			//hide the menu
			$('#'+selector+'').stop(true, true).slideUp('fast');
		}else{
			$('nav.secondary a.select').next().stop(true, true).slideUp('fast');
			$('nav.secondary a.select').removeClass('active');
			//add the active state
			$(this).addClass('active');
			//show the menu
			$('#'+selector+'').stop(true, true).slideDown('fast');
		}
	});
}
//////////////////////////////////////////////

//mapping
///////////////////////////////////////////////
function plotMap(){
	if ( $('#map_canvas').length > 0) {
		$('#map_canvas').jMapping({
			metadata_options: {type: 'html5'},
			info_window_max_width: 400
		});	
	}
}
///////////////////////////////////////////////

//form validation
///////////////////////////////////////////////
function validate(){
	if ( $('form').length > 0) {
		$('form').each(function(index) { //for each list item
			$(this).validate({
				//submitHandler: function() { alert("Submitted!") },
				focusInvalid: true,
				errorPlacement: function(error, element) {
					error.appendTo( element );
				}
			});
		});
	}
}
///////////////////////////////////////////////

//set the state
///////////////////////////////////////////////
function setState(){
	//set the billing state field
	if ( $('#customer_state').length > 0) {
		//change the value of the select to match the hidden field
		var initialVal = $('#customer_state').val();
		$('#customer_state_select').find('option[value*="'+initialVal+'"]').attr('selected', 'selected')
		
		//change the value of the hiden field to the select value
		$('#customer_state_select').change(function(event){
			var value = $(this).val();
			$('#customer_state').val(value)
		});
	}
	
	//billing state select for checkout
	if ( $('#state').length > 0) {
		var initialVal = $('#customer_state').val();
		$('#state').find('option[value*="'+initialVal+'"]').attr('selected', 'selected')
	}
	
	//set the shipping state field
	if ( $('#shipping_state').length > 0) {
		//change the value of the select to match the hidden field
		var initialVal = $('#shipping_state').val();
		$('#shipping_state_select').find('option[value*="'+initialVal+'"]').attr('selected', 'selected')
		
		//change the value of the hiden field to the select value
		$('#shipping_state_select').change(function(event){
			var value = $(this).val();
			$('#shipping_state').val(value)
		});
	}
	
	//shipping state select for checkout
	if ( $('#shipping_state_holder').length > 0) {
		var initialVal = $('#shipping_state_holder').val();
		$('#shipping_state').find('option[value*="'+initialVal+'"]').attr('selected', 'selected')
	}
	
}
///////////////////////////////////////////////



//Ready
//////////////////////////////////////////////
$(document).ready(function() {
	slider(); //homepage slider
	popup(); //Modal popup
	modalLaunch(); //AJAX Modal
	simpletooltip(); //tooltip
	tooltip(); //tooltip
	remoteTip(); //tooltip
	validate(); //form validation
	$('input, textarea').placeholder();//placeholder inputs
	plotMap(); //google map
	setState();//set the state on the edit page
	addToCartPro(); //checkbox add to cart
	addToCartRetail(); //checkbox add to cart
	setRegion(); //allows region switching
	
	utilityDrop("lang"); //language dropdown
	utilityDrop("country"); //country dropdown
	utilityDrop("pro"); //pro section dropdown
	utilityDrop("locator"); //salon locator dropdown
	
});
//////////////////////////////////////////////
