// JavaScript Document

// ONLOAD
$(function() {
		   
	// Sets page title hove animation
	$("#page_title").hover(function() {
		$("#email").animate({
			width: 'toggle', opacity: 'toggle'
		}, "slow");
	},function() {
		$("#email").animate({
			width: 'toggle', opacity: 'toggle'
		}, "slow");
	});
	
	// Add right click context menu
	addContext();
	
	// Keeps title top left
	// globals
	last_offset = Math.ceil(window.pageXOffset);
	scrolled = true;
	setInterval('keepTopCenter()', 333)
});

// Keeps title top left even when scrolling
function keepTopCenter() {
	//  get current scroll
	var x = Math.ceil(window.pageXOffset);
	
	// set scroll status to false if scroll changes
	if(x != last_offset) {
		scrolled = false;	
	}
	
	// scroll if changed ad no scroll
	if(x == last_offset && !scrolled) {
		$("#page_title_holder").animate({
			left: x+'px'
		}, "200", function() {
			scrolled = true;
		});
	}
	// write movement
	last_offset = x;
}

// Forces download of image by calling php script (in iframe to save page reload - AJAX not poss)
function forceImage(image) {
	var url = "force_download.php?id="+image;
	frames['hidden_iframe'].location.href = url;
	return false;
}

// Fake AJAX function to load image, adds image tag html, and covering load graphic, hides when load complete - ADD TIMEOUT
function ajaxImage(web, id) {
	
	// IMAGE LOCATION CHANGES FROM TEST TO LIVE - LIVE HAS PREFIX "../"
	
	var image = '<div id="load'+id+'" class="load_image_text">loading...</div>';
	image += '<img src="'+web+'" id="img'+id+'" style="max-height:500px; display:none; cursor:pointer;" onclick="jump('+"'"+id+"'"+');" />';	
	$("#"+id).html(image);
	$("#"+id).css("background", "url(custom_icons/ajax-loader.gif) no-repeat center");
	$("#"+id).css("cursor", "default");
	$("#"+id).removeAttr("onclick");
	$("#img"+id).load( function() {
		$("#load"+id).hide();
		$("#"+id).css("background", "none");
		$(this).parent().css("border", "#ffffff 1px solid");
		$(this).fadeIn("slow");
		$("#"+id).removeClass("image_load_div");
		$("#"+id).addClass("image_div");
		addContext();
	});
}

// Function to add right click context menu
function addContext() {
	$(".image_div").contextMenu('cMenu', {
		bindings: {
			'save': function(t) {
				forceImage(t.id);
			},
			'link': function(t) {
				//alert("Permalink:\nhttp://www.simonhughesdesign.com/?permalink="+t.id);
				var answer = confirm("Permalink:\nhttp://www.simonhughesdesign.com/?permalink="+t.id+"\n\nClick OK to view individual item.")	
				if (answer) {
					window.location = "?permalink="+t.id;
				}
				else {
					return false;
				}				
			},
			'mail': function(t) {
				//alert('Link currently disabled');
			},
			'cancel': function(t) {
				//alert('Link currently disabled');
			}
		},
		menuStyle: {
			border: 'none'
		},
		itemStyle: {
			fontFamily : '"Andale Mono", Monaco, sans-serif',
			fontSize : '10px',
			color: '#666666',
			border: 'none',
			padding: '1px',
			cursor: 'pointer'
		},
		itemHoverStyle: {
			color: '#666666',
			backgroundColor: '#fff880',
			border: 'none'
		}
	});
}

//BROWSER SPECIFIC - Change font for iPhone
$(function() {
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)))
	{
		$("#page_title").css("font-size", "20px");		
	}
});

//GO HOME (back to homepage)
function goHome() {
	window.location = "./";	
}

//SKIP  - When page loaded with Permalink value - skip to item
function skip(ID) {
	var pos = $("#"+ID).offset();
	var pos_left = (pos.left - 40);
	speed = Math.round(1000 * (pos_left / 2000));
	$(window).load(function () {
      $('#scroll_content').animate({scrollLeft:pos_left+"px"}, speed);
    });
}

//JUMP - Animate to clicked item
function jump(ID) {
	
	// NOTE: IE may require document.body.scrollLeft or document.documentElement.scrollLeft - TEST
	
	var current_scroll = window.pageXOffset;
	// image offset
	var pos = $("#"+ID).offset();
	// image jump position (taking account of current position)
	var pos_left = (pos.left - 40 - current_scroll);
	
	// calculate speed and animate
	speed = Math.abs(Math.round(1000 * (pos_left / 1000)));
	
	if(navigator.userAgent.match("Safari"))
	{
		$('body').animate({scrollLeft:'+='+pos_left+"px"}, speed);	
	}
	else if(navigator.userAgent.match("Firefox"))
	{
		$('html').animate({scrollLeft:'+='+pos_left+"px"}, speed);
	}
}