$(document).ready(function()
{
	// Server root pathname
	root = '/';
	
	// jQuery tooltip for services page
	$('.tooltip').tooltip
	({
		track: true,
		delay: 0
	});
	
	// jQuery slideshow for portfolio page
	$('.ss_images').cycle
	({
		fx:     'fade',
		speed:   300,
		timeout: 12800,
		containerResize: false,
		next:   '#next',
		prev:   '#prev'		
	});
	
	// jQuery portfolio iframe slideshow
	if($('#portfolio-frame').length)
	{
		var linknum = 0;
		resizeIframe();
		$('.ss_images').cycle('pause');
		$('#portfolio-frame').attr('src', $('.ss_images > div > .details a')[linknum].href);		
		
		$('#next').click
		(
			function()
			{
				linknum++;
				
				if(linknum > $('.ss_images > div > .details a').length-1)
					linknum = 0;
				
				$('#portfolio-frame').attr('src', $('.ss_images > div > .details a')[linknum].href);
			}
		);
		
		$('#prev').click
		(
			function()
			{
				linknum--;
				
				if(linknum < 0)
					linknum = $('.ss_images > div > .details a').length-1;				
				
				$('#portfolio-frame').attr('src', $('.ss_images > div > .details a')[linknum].href);				
			}
		);
	}
	
	// jQuery load contact form in background when page is loaded
	$('#contact-form').load(root+'contact.php');
	
	// jQuery animated sliding contact form
	$('.link_contact, .pagelink_contact').click
	(
		function()
		{ 
			var speed = 500;
			var top = $('#contact-form').css().position().top;
			var height = $('#contact-form').height();
		
			if(top == -height)
			{
				$('.link_contact').addClass("link_contact_active");
				$("#contact-form").stop().animate
				(
					{ top: 0 },
					speed, 
					'linear'				
				);
			}
			else 
			{
				$('.link_contact').removeClass("link_contact_active");
				$("#contact-form").stop().animate
				(
					{ top: -height },
					speed, 
					'linear'				
				);
			}
			
			return false;
		}	
	);
	
	// jQuery image colourbox
	$('.tooltipimg').live
	(
		'click', 
		function()
		{
			$(this).colorbox
			({
				open: true,
				overlayClose: true
			});
			
			return false;
		}
	);
	
	if(!(/MSIE 6.0/.test(navigator.userAgent)))
	{
		// jQuery links fade effect
		$('.navi .links:not(.right) ul li a').blend();
	
		// Background wallpaper
		displayWallpaper();
	}
});

$(window).resize(function()
{
	resizeIframe();
});

function resizeIframe()
{
	var portfolioframe = document.getElementById('portfolio-frame');
	
	iframeHeight = document.body.offsetHeight - portfolioframe.offsetTop;
	portfolioframe.style.height = iframeHeight+"px";
}

function displayWallpaper()
{
	var path = window.location.pathname;
	var page = path.substring(path.lastIndexOf('/') + 1);
	var current = (page == "") ? 'index' : 'other';
	
	if(path.indexOf('portfolio') != -1)
	{
		if(path.lastIndexOf('paper') == -1 && path.lastIndexOf('logo') == -1)
		{
			return true;
		}
	}
	
	var wallpapers = root+"images/wallpapers/";
	var images = new Array();
	
	// Background images for index page
	images['index'] = new Array
	(
		wallpapers + "bg2.png",
		wallpapers + "bg3.png"	
	);
	
	// Background images for other pages
	images['other'] = new Array
	(
		wallpapers + "bg1.png",
		wallpapers + "bg2.png"
	);
	
	var numberOfImages = images[current].length;
	var whichImage = Math.floor(Math.random()*numberOfImages);
	var wallpaperDiv = document.createElement("div");
	
	wallpaperDiv.setAttribute("id", "wallpaper");
	wallpaperDiv.style.backgroundImage = "url("+images[current][whichImage]+")";
	
	document.body.insertBefore(wallpaperDiv, document.body.firstChild);
	if(!(/MSIE/.test(navigator.userAgent)))
		$('#wallpaper').hide().fadeIn(2000);
}

function bookmark(link)
{
	var url = 'http://www.xtidesign.com';
	var title = 'XTi Design';
	
	if((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) // IE
	{
		window.external.AddFavorite(url,title);
	}
	else if(/firefox/.test(navigator.userAgent.toLowerCase())) // Firefox
	{
		window.sidebar.addPanel(title,url,'');
	}
	else if(/webkit/.test(navigator.userAgent.toLowerCase())) // Safari and Chrome
	{
		alert("Press CTRL-D to bookmark");
	}
	else if(/opera/.test(navigator.userAgent.toLowerCase())) // Opera
	{ 
		link.setAttribute('href', url);
		link.setAttribute('title', title);
		link.setAttribute('rel', 'sidebar');
		link.click();
		return false;
	}
}

function popUpProperties(inobj)
{
	// Used to display javascript object properties
	op = window.open();
	op.document.open('text/plain');
	for (objprop in inobj) 
	{
		op.document.write(objprop + ' => ' + inobj[objprop] + '\n');
	}
	op.document.close();
}