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

sc = {
  common : {
    init     : function(){
    	//no common functionality
    	$('.ie .cards ul li:odd').css({'margin-right': '1.6em'});
    	
    	$('.ie .cards ul li:nth-child(5n)').css({'margin-right': '0'});
    	
    }
  },
  card : {
  	init     : function(){  		  		
  		//set up image area
  		$("#cardImage #outside").append('<div id="outsidePreview"></div>');
  		$("#cardImage #inside").append('<div id="insidePreview"></div>');
  	
  		//font selection
  		var font = $("#cardForm span.eselect select option[value='"+$("#cardForm span.eselect select").val()+"']").text();
  		$("#outsidePreview, #insidePreview").addClass(font);
  		$("#cardForm span.eselect select").change(function(){
  			font = $("#cardForm span.eselect select option[value='"+$("#cardForm span.eselect select").val()+"']").text();
  			$("#outsidePreview, #insidePreview").attr({'class':''});
  			$("#outsidePreview, #insidePreview").addClass(font);
  		});
  		
  		$("#outsidePreview").hide();
  		$("#cardForm span.etext input").attr({'maxlength':'26'});
  		$("#cardForm span.etext input").keyup(function(){
			if($(this).parent().index("span.etext") == 0)
			{
				if($(this).val() != '')
				{
					$("#outsidePreview").show().html($(this).val());
				}
				else
				{
					
					$("#outsidePreview").hide().html($(this).val());
				}
			}
  		});
  		
  		$("#cardForm span.etextarea textarea").keyup(function(){
			if($(this).parent().index("span.etextarea") == 0)
			{
				if($(this).val() != '')
				{
					$("#insidePreview").show().html($(this).val().replace(/\n\r?/g, '<br />'));
					offset = $("#insidePreview").height()/2;
					// console.log(offset);				
					$("#insidePreview").css({'margin-top':"-"+offset+"px"});
				}
				else
				{
					
					$("#insidePreview").hide().html($(this).val());
				}
			}
  		});
  		
  		// Changing textarea to input fields
  		$("span.etextarea").eq(1).children("textarea").hide();  		
  		
  		$("span.etextarea").eq(1).append('<label for="eshop_custfirstname">First Name</label><input type="text" id="eshop_custfirstname" name="eshop_custfirstname" class="required" /><label for="eshop_custlastname">Last Name</label><input type="text" id="eshop_custlastname" class="required" name="eshop_custlastname" /><label for="eshop_addressline1">Address Line 1</label><input type="text" id="eshop_addressline1" name="eshop_addressline1" class="required" /><label for="eshop_addressline2">Address Line 2</label><input type="text" id="eshop_addressline2" name="eshop_addressline2" /><label for="eshop_cityortown">City or Town</label><input type="text" id="eshop_cityortown" name="eshop_cityortown" class="required" /><label for="eshop_county">County</label><input type="text" id="eshop_county" name="eshop_county" /><label for="eshop_postcode">Postcode</label><input type="text" id="eshop_postcode" name="eshop_postcode" class="required"/>');  		  		  		  		
  		// Form validation
  		$("form.addtocart").validate();
  		
  		// Making inputs fill in hidden textarea
  		$("input[type=submit].eshopbutton").click(function() {
  			var firstName = $('#eshop_custfirstname').val();  			
  			var lastName = $('#eshop_custlastname').val();
  			var addressLine1 = $('#eshop_addressline1').val();
  			var addressLine2 = $('#eshop_addressline2').val();
  			var CityOrTown = $('#eshop_cityortown').val();
  			var county = $('#eshop_county').val();
  			var postcode = $('#eshop_postcode').val();
  			$("span.etextarea").eq(1).children("textarea").append(firstName+', '+lastName+', '+addressLine1+', '+addressLine2+', '+CityOrTown+', '+county+', '+postcode);
  		});
  		
  		//paginated customisation
  		$("#cardForm legend").after('<h2>Customise your card</h2>')
  		
  		$("#cardForm span.eselect, #cardForm span.etext, #cardForm span.etextarea").wrap('<fieldset class="panel"/>');		
		
		// text of label to stick into legend
		var legendText = "Tell us where you'd like to send the card.";
		$("fieldset.panel").eq(3).prepend("<h2>"+legendText+"</h2>");				  		
  		
  	}
  }
}

UTIL = {

	fire : function(func,funcname, args){
		//change to match above namespace
		var namespace = sc;
		funcname = (funcname === undefined) ? 'init' : funcname;
		if (func !== '' && namespace[func] && typeof namespace[func][funcname] == 'function'){
			namespace[func][funcname](args);
		} 

	}, 

	loadEvents : function(){
		var bodyId = document.body.id;
		// hit up common first.
		UTIL.fire('common');
		// do all the classes too.
		$.each(document.body.className.split(/\s+/),function(i,classnm){
			UTIL.fire(classnm);
		});
	}
};

$(document).ready(UTIL.loadEvents);
