// UTF8‼
$(document).ready(function(){

	var v_sym_vis_n = '► ';
	var v_sym_vis_y = '▼ ';

	$('#homepagecontent .col h2, #foot .col h3').prepend(v_sym_vis_n);
	function f_homepage_text() {
		$('#homepagecontent .col, #foot .col:not(:last)').each(function(){
			$.data(this,'visible','N');
		}).children('p,ul').hide();
		$('#homepagecontent .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('Photo Nav','Click',$(this).children('h2,h3').text() + ' (Home {m})');}catch(err){}
		});
		//$('#homepagecontent .col:first').click();
	}
	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,#portfolio,#foot .col:not(:last)').hide();
				$('#foot .col:last').show();
				try{pageTracker._trackPageview('/photography/contact/');}catch(err){}
			break;
			case 'other':
				$('#portfolio, #foot .col').hide();
			break;
			default:
				$('#portfolio,#foot .col:last').hide();
				// Check If Home Page Needs Loading
				if ($('#homepagecontent').length == 0){
					f_load(v_url_root);
				} else {
					$('#content .container,#foot .col:not(:last)').show();
					try{pageTracker._trackPageview('/photography/');}catch(err){}
				}
			break;
		}
	}

	// Clicks
	$('#nav a').click(function(){
		location.href = $(this).attr('href');
		try{pageTracker._trackEvent('Photo 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('Photo Nav','Click',$(this).attr('href') + ' (Sub Nav {m})');}catch(err){}
		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('Photo Nav','Click','http://maps.google.co.uk/… (Contact {m})');}catch(err){}
		} else {
			try{pageTracker._trackEvent('Photo Nav','Click',this_href + ' (Contact {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,'').replace(location.hash,'') == '') {
			f_content_display('services');
		} else {
			f_content_display('other');
		}
	} else {
		f_content_display(location.hash.slice(1));
	}

});

/*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);
