$(document).ready(function() {

// Set default behaviors for jQuery toggle features

$('.toggle').prev().prepend('<a href="#" class="toggleLink-1">&#160;</a> ');
$('.toggle').prev().removeClass().addClass('toggleH-1');
$('.toggle').css('margin-left', '50px');
$('.toggle').hide();

$('.toggleLink-1, .toggleLink-2').click(function() {

	var newAClass = ($(this).attr('class') == 'toggleLink-1') ? 'toggleLink-2' : 'toggleLink-1';
	$(this).removeClass().addClass(newAClass);
	var newHClass = ($(this).parent().attr('class') == 'toggleH-1') ? 'toggleH-2' : 'toggleH-1';
	$(this).parent().removeClass().addClass(newHClass);
	$(this).parent().next('.toggle').slideToggle('fast');
	return false;

});

// Change title of main photo

if (!is_mobile()) {$('#photograph').attr('title', 'Enlarge Photograph');}

// force mobile devices to hide the title bar

/mobile/i.test(navigator.userAgent) && !location.hash && setTimeout(function() {if (!pageYOffset) {window.scrollTo(0, 1);}}, 2000);

});



$(function() {$.prompt.setDefaults({loaded:function(){}, persistent:false, top:'12%', overlayspeed:0, promptspeed:0, opacity:0.80});});

// jQuery - sets defaults for the jQuery Impromptu popups



function enlarge1(photo, width, height) {

// Writes enlarge link in main navigation menu

if (!is_mobile()) {var link = '<a class="navmenu" href="javascript:void(0)" onclick="enlarge(\'' + photo + '\', ' + width + ', ' + height + ');" title="Enlarge Photograph" id="enlarge_link">Enlarge<\/a>';
document.write(link);}}



function enlarge2(photo, width, height) {

// Writes enlarge link at base of photograph

if (!is_mobile()) {var link = '<a class="photo_larger" href="javascript:void(0)" onclick="enlarge(\'' + photo + '\', ' + width + ', ' + height + ');" title="Enlarge Photograph">&#160;<\/a>';
document.write(link);}}



function enlarge(photo, width, height) {

// Produces pop-up enlargement of a photograph

if (!is_mobile()) {

	if (width == 306) {width = 498;}
	if (width == 344) {width = 560;}
	if (width == 458) {width = 746;}

	if (height == 306) {height = 498;}
	if (height == 344) {height = 560;}
	if (height == 458) {height = 746;}

	var divHeight = height;
	var divWidth = width + 28;

	if ($(window).height()-85 < divHeight) {divHeight = $(window).height() - 85;}
	if ($(window).width()-64 < divWidth) {divWidth = (Math.round($(window).width() / 2) * 2 + 1) - 64;}

	var borderWidth = divWidth + 28;

	var HTML = '<div class="pop_outer"><div class="pop_inner" style="height:' + divHeight + 'px; width:' + (divWidth-2) + 'px;"><img class="enlargement" src="images/' + photo + '-Web-Extra-Large.jpg" width="' + width + '" height="' + height + '" alt="Photo Loading ..." title="" \/><\/div><\/div>';
	HTML += '<div class="close_holder"><a class="photo_smaller" href="javascript:void(0)" onclick="$.prompt.close();" title="Close Window">Close Window<\/a><\/div>';

	$.prompt(HTML, {prefix:'photo_pop', top:'0px'});

	return false;}

}



function qG(ID) {return document.getElementById(ID);}

// Makes getting an element by ID easier



function ltrim(stringToTrim) {return stringToTrim.replace(/^\s+/,"");}
function rtrim(stringToTrim) {return stringToTrim.replace(/\s+$/,"");}
function fulltrim(stringToTrim) {return stringToTrim.replace(/^\s+|\s+$/g,"");}

// Trims white space from the left, right, or both ends of a string



function newWindow(url) {var win = window.open(url); return false;}

// Opens a link in a new window and supresses the native link behaviour



var menu_timer = 0, menu_timer2 = 0, control_timer = 0, header_timer = 0;

// Initialize global variables for use with menu show and hide functions



function controlSet() {

// Sets event handlers for showing and hiding photo controls

if (!is_mobile()) {

	$('#photo_2').mouseenter(function() {

		clearTimeout(control_timer);
		control_timer = setTimeout(function(){$('#controls').fadeIn(200);}, 300);

	});

	$('#photo_2').mouseleave(function() {

		clearTimeout(control_timer);
		control_timer = setTimeout(function(){$('#controls').fadeOut(200);}, 300);

	});}}



function HideWindow(xid) {

// Hides a DOM menu element with the specified delay

var delay = 500;
clearTimeout(menu_timer);
var item = qG(xid).style;
menu_timer = setTimeout(function(){item.visibility = 'hidden';}, delay);}



function HideWindow2(xid) {

// Hides a DOM menu element with the specified delay

var delay = 500;
clearTimeout(menu_timer2);
var item = qG(xid).style;
menu_timer2 = setTimeout(function(){item.visibility = 'hidden';}, delay);}



function ShowWindow(xid) {

// Shows a primary DOM menu element with a specified delay

var delay = 500;
clearTimeout(menu_timer);
var item_fly = '';
if (xid == 'galleries') {var items_fly = new Array('Basic_tips', 'Top_tips', 'Top_chal');}
else if (xid == 'tutorials') {var items_fly = new Array('Rural_gals', 'Urban_gals', 'BW_gals');}
else {var items_fly = new Array('Rural_gals', 'Urban_gals', 'BW_gals', 'Basic_tips', 'Top_tips', 'Top_chal');}
hideOthers(item_fly, items_fly);
var item = qG(xid).style;
var items = new Array('galleries', 'tutorials', 'about', 'contact', 'guestbook');
if (qG('galleries').style.visibility == 'visible' || qG('tutorials').style.visibility == 'visible' || qG('about').style.visibility == 'visible' || qG('contact').style.visibility == 'visible' || qG('guestbook').style.visibility == 'visible') {
hideOthers(item, items); item.visibility = 'visible';}
else {menu_timer = setTimeout(function(){item.visibility = 'visible';}, delay);}}



function ShowWindow2(xid) {

// Shows a secondary DOM menu element with no delay

clearTimeout(menu_timer2);
var item = qG(xid).style;
var items = new Array('Rural_gals', 'Urban_gals', 'BW_gals', 'Basic_tips', 'Top_tips', 'Top_chal');
hideOthers(item, items);
item.visibility = 'visible';}



function hideOthers(item, items) {

// Hides all menu items that are not the one specified

for (var i=0; i<items.length; i++) {items[i] = qG(items[i]).style; if (items[i] != item) {items[i].visibility = 'hidden';}}}



function GetRandTip() {

// Uses jQuery AJAX to get a random photo tip from the archives, parse its date, and display it

	$.post('RSSrandomtip.php', function(data) {

		var randomtip = 'randomtip', day = 'randomDay', month = 'randomMonth', year = 'randomYear';
		var tip = fulltrim(data);

		if (tip.indexOf('(First published ') > -1) {tip = tip.replace('(First published ', '(');}

		var date = tip.substring(tip.length-14, tip.length);
		date = fulltrim(date);
		tip = tip.substring(0, tip.length-14);

		date = date.replace('(', '');  date = date.replace(')', '');  date = date.replace(',', '');
		date = date.split(' ');

		qG(month).innerHTML = date[0];
		qG(day).innerHTML = ((date[1].length < 2) ? '0' : '') + date[1];
		qG(year).innerHTML = date[2];
		qG(randomtip).innerHTML = tip;

		$('#tip_random').removeClass('tip_refresh1').addClass('tip_refresh2');
		$('#tip_todays').removeClass('tip_refresh2').addClass('tip_refresh1');

	});

	return false;

}



function GetTodayTip() {

// Uses jQuery AJAX to get today's photo tip from the archives, get its date, and display it

	$.post('RSStodaytip.php', function(data) {

		var randomtip = 'randomtip', day = 'randomDay', month = 'randomMonth', year = 'randomYear';
		var data_split = data.split(':::');

		var tip = data_split[0];
		var date = data_split[1];
		date = fulltrim(date);

		if (tip.indexOf('(First published ') == -1) {tip = tip.substring(0, tip.length-14);}

		date = date.replace('(', '');  date = date.replace(')', '');  date = date.replace(',', '');
		date = date.split(' ');

		qG(month).innerHTML = date[0];
		qG(day).innerHTML = ((date[1].length < 2) ? '0' : '') + date[1];
		qG(year).innerHTML = date[2];
		qG(randomtip).innerHTML = tip;

		$('#tip_random').removeClass('tip_refresh2').addClass('tip_refresh1');
		$('#tip_todays').removeClass('tip_refresh1').addClass('tip_refresh2');

	});

	return false;

}



function writeTip_refresh() {

// Writes the random and daily photo tip refesh links

	var str = '<span class="tip_refresh_wrap"><a class="tip_refresh1" id="tip_random" href="javascript:void(0);" onclick="return GetRandTip();" title="">Random</a> | <a class="tip_refresh2" id="tip_todays" href="javascript:void(0);" onclick="return GetTodayTip();" title="">Today\'s</a></span>';
	document.write(str);

}



function writeTool1() {

// Writes the first part of the RSS feed section of the RSS info page

var str='';
str+='<div class="tool_L">';
str+='<div class="DailyPhotoTips">';
document.write(str);}



function writeTool2() {

// Writes the second part of the RSS feed section of the RSS info page
// Position:relative in p tag necessary to fix IE7 "stacking" bug which repeats the last character below the div

var str='';
str+='<\/div>';
str+='<\/div>';
str+='<div class="tool_R">';
str+='<p style="position:relative;">Add Daily Photo Tips to your website by copying and pasting the html code below.&#160; The tool\'s colour and size can be easily changed.&#160; The Daily Photo Tips tool will update itself seven times per week.<\/p>';
str+='<\/div>';
document.write(str);}



function Set_Cookie(name, value, expires, path, domain, secure) {

// Writes the contents of an input field to a cookie

var today = new Date();
today.setTime( today.getTime() );
if (expires) {
expires = expires * 1000 * 60 * 60 * 24; }
var expires_date = new Date(today.getTime() + (expires));
document.cookie = name + "=" +escape(value) +
((expires) ? ";expires=" + expires_date.toGMTString() : "" ) + 
((path ) ? ";path=" + path : "" ) + 
((domain) ? ";domain=" + domain : "" ) +
((secure) ? ";secure" : "" ); }



function Get_Cookie(check_name) {

// Fills the contents of an input field from a cookie

var a_all_cookies = document.cookie.split(';');
var a_temp_cookie = '';
var cookie_name = '';
var cookie_value = '';
var b_cookie_found = false;
for (i = 0; i < a_all_cookies.length; i++) {
a_temp_cookie = a_all_cookies[i].split('=');
cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
if (cookie_name == check_name) {
b_cookie_found = true;
if (a_temp_cookie.length > 1) {
cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') ); }
return cookie_value;
break; }
a_temp_cookie = null;
cookie_name = ''; }
if ( !b_cookie_found ) {
return ''; } }



function generate_address(username, hostname) {

// Generates an email address in an unusual way to make it harder for spam robots to parse

var domain = '.com';
var atsign = '&#64;';
var addr = username + atsign + hostname + domain;
document.write('<p class="bulletindent"><' + 'a' + ' ' + 'class=' + '"white"' + ' ' + 'href=' + 'mail' + 'to:' + addr + ' title="Email Me">' + addr + '<\/a><\/p>');}



function Java_Back(pages, style){document.write ('<a class="'+ style +'" href="javascript:history.back('+ pages +')" title="Go Back">Go Back<\/a>');}

// Writes a back button to return to a previous page



function lL() {

// Replaces the first letter of the intro text span with a larger accent font, removes the default leading image

$("#C").css('display','none');

$("#D").each(function() {

	var el = $(this),
	text = ltrim(el.html()),
	first = text.slice(0, 1),
	rest = text.slice(1);

	var newRest = "", caps = 0;

	for (var i = 0; i < rest.length; i++) {
		var letter = rest.charAt(i);
		if (letter.search(/[A-Z]/) >= 0) {newRest += "<span class='scriptbigger'>" + letter + "</span>"; caps = 1;}
		else {newRest += letter;}
	}

	el.html("<span class='scriptletter'>" + first + "</span>" + newRest);
	if (caps) {$("span.scriptletter").css('margin-top','1px'); $("p.script").css('margin-top','1px');}

});}



function makeShare() {

// Returns a string of share links ready for insertion

var Title = document.title;
var Location = document.location.href;

// Write share data using a 2D array

var shareData = new Array();

shareData[0] = ['http://www.facebook.com/share.php?',	'u=',			'&t=',		'shareFacebook.png',		'Share on Facebook'];
shareData[1] = ['http://twitter.com/home?',		'status=Enjoying ',	'',		'shareTwitter.png',		'Tweet This'];
shareData[2] = ['http://digg.com/submit?',		'url=',			'&title=',	'shareDigg.png',		'Digg This'];
shareData[3] = ['http://delicious.com/post?',		'url=',			'&title=',	'shareDelicious.png',		'Share on Delicious'];
shareData[4] = ['http://www.stumbleupon.com/submit?',	'url=',			'&title=',	'shareStumbleupon.png',		'Stumble This'];

var shareString = '';

for (var i=0; i<shareData.length; i++) {

	var URL = shareData[i][1] + Location + ((shareData[i][2] != '') ? shareData[i][2] + Title : '');
	URL = shareData[i][0] + encodeURI(URL);

	shareString += '<a class="shareLink" title="' + shareData[i][4] + '" href="' + URL + '" onclick="return newWindow(\'' + URL + '\');" style="background:url(\'images/' + shareData[i][3] + '\')">&#160;</a>';

}

return '<span class="shareLinks">' + shareString + '</span>';

}



function writeNews() {document.write('<p id="newsLink" class="newsExpand"><a class="newsExpand" title="More News Items" href="javascript:void(0)" onclick="expandNews(\'newsLink\');">More ...</a><br /><br /><br /></p>');}

// Generates the HTML for the More button on the News & Info page



function expandNews(Button) {

// Gets more news and info from the input file (3 at a time) and adds them to the page

	if (!window.RSSnewsIndex) {window.RSSnewsIndex = 3;}
	else {window.RSSnewsIndex += 3;}

	$.post('RSSnewsparser.php?' + window.RSSnewsIndex, function(data) {

		var dataArray = data.split('||');
		$('#' + Button).before(dataArray[0]);
		$('#' + Button).prev().slideToggle('fast', function() {

			if (dataArray[1] == 'F') {$('#' + Button).html('No more news items to display<br /><br /><br />');}

		});		

	});

}



function copyPop() {

// Writes the copyright popup screen on all pages in main site

var Title = document.title;
Title = Title.replace(' - Mark Raymond Mason Fine Art Photography', '');
Title = Title.replace(' \(Artist Statement\)', '');

if (!is_mobile()) {

	var shareString = makeShare();

	var width = 504, divHeight = 524;
	var divWidth = width + 38;

	if ($(window).height()-85 < divHeight) {divHeight = $(window).height() - 85;}
	if ($(window).width()-64 < divWidth) {divWidth = (Math.round($(window).width() / 2) * 2 + 1) - 64;}

	var borderWidth = divWidth + 28;

	var Contents = qG('copyright_pop').innerHTML;
	Contents = Contents.replace(/This document does not have a formal title/g, Title);
	Contents = Contents.replace('Sharing this page helps promote my photography.', shareString);

	var HTML = '<div class="pop_outer"><div class="pop_inner2" style="height:' + divHeight + 'px; width:' + (divWidth-2) + 'px;"><div class="pop_inner_sleeve">' + Contents + '<\/div><\/div><\/div>';
	HTML += '<div class="close_holder"><a class="photo_smaller" href="javascript:void(0)" onclick="$.prompt.close();" title="Close Window">Close Window<\/a><\/div>';

	$.prompt(HTML, {prefix:'photo_pop', top:'0px'});

	return false;}}



function infoPop() {

// Writes the index info popup screen

if (!is_mobile()) {

	var width = 504, divHeight = 524;
	var divWidth = width + 38;

	if ($(window).height()-85 < divHeight) {divHeight = $(window).height() - 85;}
	if ($(window).width()-64 < divWidth) {divWidth = (Math.round($(window).width() / 2) * 2 + 1) - 64;}

	var borderWidth = divWidth + 28;

	var Contents = qG('blurb').innerHTML;

	var HTML = '<div class="pop_outer"><div class="pop_inner2" style="height:' + divHeight + 'px; width:' + (divWidth-2) + 'px;"><div class="pop_inner_sleeve">' + Contents + '<\/div><\/div><\/div>';
	HTML += '<div class="close_holder"><a class="photo_smaller" href="javascript:void(0)" onclick="$.prompt.close();" title="Close Window">Close Window<\/a><\/div>';

	$.prompt(HTML, {prefix:'photo_pop', top:'0px'});

	return false;}}



function is_mobile() {

// Tests for mobile devices (through screen orientation event)

if (typeof window.onorientationchange != "undefined") {return true;}
else {return false;}}


