// UTF8‼
$(document).ready(function(){

	var v_sym_vis_n = '► ';
	var v_sym_vis_y = '▼ ';

	$('#content .col h2, #foot .col h3').prepend(v_sym_vis_n);
	function f_homepage_text() {
		$('#content .col, #foot .col:not(:last)').each(function(){
			$.data(this,'visible','N');
		}).children('p,ul').hide();
		$('#content .col, #foot .col:not(:last)').unbind().click(function(){
			if ($.data(this,'visible') == 'N') {
				$(this).children('h2,h3').addClass('current').text(v_sym_vis_y + $(this).children('h2,h3').text().replace(v_sym_vis_n,''));
				$(this).children('p,ul').show();
				$.data(this,'visible','Y');
			} else {
				$(this).children('h2,h3').removeClass('current').text(v_sym_vis_n + $(this).children('h2,h3').text().replace(v_sym_vis_y,''));
				$(this).children('p,ul').hide();
				$.data(this,'visible','N');
			}
			try{pageTracker._trackEvent('Print Nav','Click',$(this).children('h2,h3').text() + ' (Home {m})');}catch(err){}
		});
	}
	f_homepage_text();
	
	function f_content_display(fv_rel) {
		$('#subnav a').removeClass('current');
		$('#subnav a[rel="' + fv_rel + '"]').addClass('current');
		switch (fv_rel) {
			case 'contact':
				$('#content .container,#foot .col:not(:last)').hide();
				$('#foot .col:last').show();
				try{pageTracker._trackPageview('/printing/contact/');}catch(err){}
			break;
			case 'digital':
				$('#foot .col:last').hide();
				// Check If Page Needs Loading
				if ($('#homepagecontent').length == 0){
					f_load(v_url_root + 'printing/');
				} else {
					$('#content .container,#foot .col:not(:last)').show();
					try{pageTracker._trackPageview('/printing/');}catch(err){}
				}
			break;
			case 'litho':
				$('#foot .col:last').hide();
				// Check If Page Needs Loading
				if ($('#lithopagecontent').length == 0){
					f_load(v_url_root + 'printing/litho/');
				} else {
					$('#content .container,#foot .col:not(:last)').show();
					try{pageTracker._trackPageview('/printing/litho/');}catch(err){}
				}
			break;
			case 'design':
				$('#foot .col:last').hide();
				// Check If Page Needs Loading
				if ($('#designpagecontent').length == 0){
					f_load(v_url_root + 'printing/design/');
				} else {
					$('#content .container,#foot .col:not(:last)').show();
					try{pageTracker._trackPageview('/printing/design/');}catch(err){}
				}
			break;
		}
	}

	// Clicks
	$('#nav a').click(function(){
		location.href = $(this).attr('href');
		try{pageTracker._trackEvent('Print Nav','Click',$(this).attr('href') + ' (Nav {m})');}catch(err){}
		return false;
	});
	$('#subnav a').click(function(){
		f_content_display($(this).attr('rel'));
		location.href = '#head';
		try{pageTracker._trackEvent('Print Nav','Click',$(this).attr('href') + ' (Sub Nav {m})');}catch(err){}
		return false;
	});
	$('#portfolio a').click(function(){
		$('#portfolio').hide();
		f_load($(this).attr('href'));
		location.href = '#head';
		try{pageTracker._trackEvent('Print Nav','Click',$(this).attr('href') + ' (Thumb {m})');}catch(err){}
		return false;
	});
	$('#portfolio_slides').click(function(){
		try{pageTracker._trackEvent('Print Nav','Click',$(this).attr('href') + ' (Thumb {m})');}catch(err){}
		location.href = $(this).attr('href');
		return false;
	});
	$('#portfolio_link').click(function(){
		try{pageTracker._trackEvent('Print Nav','Click',$(this).attr('href') + ' (CTA {m})');}catch(err){}
		location.href = $(this).attr('href');
		return false;
	});
	$('#foot .nav a').click(function(){
		var this_href = $(this).attr('href');
		location.href = this_href;
		if (this_href.slice(0,25) == 'http://maps.google.co.uk/') {
			try{pageTracker._trackEvent('Print Nav','Click','http://maps.google.co.uk/… (Contact {m})');}catch(err){}
		} else {
			try{pageTracker._trackEvent('Print Nav','Click',this_href + ' (Contact {m})');}catch(err){}
		}
		return false;
	});
	$('#terms_link').click(function(){
		location.href = $(this).attr('href');
		try{pageTracker._trackEvent('Print Nav','Click',$(this).attr('href') + ' (Foot {m})');}catch(err){}
		return false;
	});

	// Trigger Current Tab
	if (location.hash.slice(1) == '' || location.hash.slice(1) == 'head') {
		if (location.href.replace(v_url_root,'').slice(0,9) == 'litho') {
			f_content_display('litho');
		} else {
			switch (location.href.replace(v_url_root + 'printing/','').replace(location.hash,'')) {
				case '': f_content_display('digital'); break;
				case 'litho/': f_content_display('litho'); break;
				case 'design/': f_content_display('design'); break;
			}
		}
	} else {
		f_content_display(location.hash.slice(1));
	}
	
	// AJAX Loading & Errors
	$(document)
		.ajaxStart(function(data){
			$('#content').html('<p style="text-align:center;padding:20px;"><img src="' + v_url_root + 'media/img/m/loading.gif" alt="Loading" /></p>');
		},'html')
		.ajaxError(function(data){
			$('#content').html('<p><b>Error 404:</b> The server can not find the requested page.</p>');
		},'html');
	
	// Load Content Page
	function f_load(v_href) {
		$.get(v_href,{'HTTP_X_REQUESTED_WITH':'XMLHttpRequest'},function(data){
			$('#content').html(data);
			//if (v_href == v_url_root) {
				$('#content .col h2').prepend(v_sym_vis_n);
				f_homepage_text();
				$('#foot .col:not(:last)').show();
			//}
		},'html');
		try{pageTracker._trackPageview('/printing'+v_href.replace('http://m.flaydemouse.com/printing',''));}catch(err){}
	}

	$.preloadImages(v_url_root + 'media/img/m/loading.gif');

});

jQuery.preloadImages = function() {
	jQuery.each(arguments,function(e) {
		jQuery('<img src="' + this + '" />');
	});
}

/*var currentRotation = null;
setInterval(checkOrientAndLocation,1000);
function checkOrientAndLocation(){
	if(currentRotation != window.orientation){
		setOrientation();
	}
}
function setOrientation(){
	switch(window.orientation){
		case 0: orient = 'portrait'; break;
		case 90: orient = 'landscape'; break;
		case -90: orient = 'landscape'; break;
	}
	currentRotation = window.orientation;
	document.body.setAttribute("orient",orient);
	setTimeout(scrollTo,0,0,1);
}*/

var currentWidth = null;
var currentRotation = null;
if (document.addEventListener) {
	addEventListener("load",function() {
		setTimeout(updateLayout,0);
	},false);
}
function updateLayout() {
	if (typeof window.innerWidth != 'undefined') {
		if (window.innerWidth != currentWidth) {
			currentWidth = window.innerWidth;
			var orient = currentWidth <= 320 ? "profile" : "landscape";
			document.body.setAttribute("orient",orient);
			setTimeout(function(){
				window.scrollTo(0,1);
			}, 100);           
		}
	} else {
		if (typeof window.orientation != 'undefined') {
			if (currentRotation != window.orientation) {
				switch(window.orientation){
					case 0: orient = 'portrait'; break;
					case 90: orient = 'landscape'; break;
					case -90: orient = 'landscape'; break;
				}
				currentRotation = window.orientation;
				document.body.setAttribute("orient",orient);
				setTimeout(function(){
					window.scrollTo(0,1);
				}, 100);
			}
		}
	}
}
setInterval(updateLayout,400);
