/* Web Design and Programming by Cube Connection Ltd, Copyright 2006 - www.CubeConnection.co.uk */
function ShowEmail(username, hostname, friendlyname, displaystyle) {
	var linktext;
	if (friendlyname == "") {
		if (displaystyle == "") {
			linktext = username + "@" + hostname;
			document.write("<a href=" + "mail" + "to:" + username + "@" + hostname + ">" + linktext + "</a>");
		}
		else {
			linktext = username + "@" + hostname;
			document.write("<a href=" + "mail" + "to:" + username + "@" + hostname + " class='" + displaystyle + "'>" + linktext + "</a>");
		}
	}
	else {
		if (displaystyle == "") {
			linktext = username + "@" + hostname;
			document.write("<a href=" + "mail" + "to:" + username + "@" + hostname + ">" + friendlyname + "</a>");
		}
		else {
			linktext = username + "@" + hostname;
			document.write("<a href=" + "mail" + "to:" + username + "@" + hostname + " class='" + displaystyle + "'>" + friendlyname + "</a>");
		}
	}
	return true;
}

function stopPropagateOnClick(event) {
	var e = (window.event) ? window.event : event;
	//The following line stop onclick event bubbling in W3C browsers
	e.cancelBubble = true;
	// The following line stop onclick event bubbling in IE
	if (e.stopPropagation) e.stopPropagation();
}

showGallery = function () {
	//Show Banner
	$(".main_image .desc").show(); //Show Banner
	$(".main_image .block").animate({ opacity: 0.85 }, 1); //Set Opacity

	//Click and Hover events for thumbnail list
	$(".image_thumb ul li:first").addClass('active');
	$(".image_thumb ul li.viewfull").click(function () {
		//Set Variables
		var imgAlt = $(this).find('img').attr("alt"); //Get Alt Tag of Image
		var imgTitle = $(this).find('a').attr("href"); //Get Main Image URL
		var imgDesc = $(this).find('.block').html(); 	//Get HTML of block
		var imgDescHeight = $(".main_image").find('.block').height(); //Calculate height of block	

		if ($(this).is(".active")) {  //If it's already active, then...
			return false; // Don't click through
		} else {
			//Animate the Teaser				
			$(".main_image .block").animate({ opacity: 0, marginBottom: -imgDescHeight }, 250, function () {
				$(".main_image .block").html(imgDesc).animate({ opacity: 0.85, marginBottom: "0" }, 250);
				$(".main_image img").attr({ src: imgTitle, alt: imgAlt });
			});
		}

		$(".image_thumb ul li").removeClass('active'); //Remove class of 'active' on all lists
		$(this).addClass('active');  //add class of 'active' on this list only
		return false;

	}).hover(function () {
		$(this).addClass('hover');
	}, function () {
		$(this).removeClass('hover');
	});

	//Toggle Teaser
	$("a.collapse").click(function () {
		$(".main_image .block").slideToggle();
		$("a.collapse").toggleClass("show");
	});
};

runSlideShow = function () {
	//Set Default State of each portfolio piece
	$(".paging").show();
	$(".paging a:first").addClass("active");

	//Get size of images, how many there are, then determin the size of the image reel.
	var imageWidth = $(".window").width();
	var imageSum = $(".image_reel img").size();
	var imageReelWidth = imageWidth * imageSum;

	//Adjust the image reel to its new size
	$(".image_reel").css({ 'width': imageReelWidth });

	//Paging + Slider Function
	rotate = function () {
		var triggerID = $active.attr("rel") - 1; //Get number of times to slide
		var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide

		$(".paging a").removeClass('active'); //Remove all active class
		$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)

		//Slider Animation
		$(".image_reel").animate({
			left: -image_reelPosition
		}, 500);
	};

	//Rotation + Timing Event
	rotateSwitch = function () {
		play = setInterval(function () { //Set timer - this will repeat itself every 3 seconds
			$active = $('.paging a.active').next();
			if ($active.length === 0) { //If paging reaches the end...
				$active = $('.paging a:first'); //go back to first
			}
			rotate(); //Trigger the paging and slider function
		}, 4000); //Timer speed in milliseconds (4 seconds)
	};

	rotateSwitch(); //Run function on launch

	//On Hover
	$(".image_reel a").hover(function () {
		clearInterval(play); //Stop the rotation
	}, function () {
		rotateSwitch(); //Resume rotation
	});

	//On Click
	$(".paging a").click(function () {
		$active = $(this); //Activate the clicked paging
		//Reset Timer
		clearInterval(play); //Stop the rotation
		rotate(); //Trigger rotation immediately
		rotateSwitch(); // Resume rotation
		return false; //Prevent browser jump to link anchor
	});
};

formPopup = function () {
	//When you click on a link with class of poplight and the href starts with a # 
	$('a.changeimage[href^=#]').click(function () {
		var popID = $(this).attr('rel'); //Get Popup Name
		var popURL = $(this).attr('href'); //Get Popup href to define IDs
		var popDesc = $(this).parent().parent().find('p').html() //Get the Image Description

		//Pull Query & Variables from href URL to get cid and iid and update form
		var query = popURL.split('?');
		var param = query[1].split('&');
		$('input[name=cid]').val(param[0].split('=')[1]); //set the cid value
		$('input[name=iid]').val(param[1].split('=')[1]); //set the iid value
		$('span#imgno').text(param[1].split('=')[1]);
		$('input[name=desc]').val(popDesc); //set the desc input box if one
		$('input[name=del]').attr('checked', false);

		//Pull Query & Variables from href URL to get width - decided not to use next 3 lines
		//var query = popURL.split('?');
		//var dim = query[1].split('&');
		//var popWidth = dim[0].split('=')[1]; //Gets the first query string value
		var popWidth = 600;

		//Fade in the Popup and add close button
		$('#' + popID).fadeIn().css({ 'width': Number(popWidth) }).prepend('<a href="#" class="close"><img src="/images/icons/closex.png" class="btn_close" title="Close Window" alt="Close" /></a>');

		//Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and border width defined in the css
		var popMargTop = ($('#' + popID).height() + 60) / 2;
		var popMargLeft = ($('#' + popID).width() + 60) / 2;

		//Apply Margin to Popup
		$('#' + popID).css({
			'margin-top': -popMargTop,
			'margin-left': -popMargLeft
		});

		//Fade in Background
		$('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
		$('#fade').css({ 'filter': 'alpha(opacity=80)' }).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies 

		return false;
	});

	//Close Popups and Fade Layer
	$('a.close, #fade').live('click', function () { //When clicking on the close or fade layer...
		$('#fade , .popup_block').fadeOut(function () {
			$('#fade, a.close').remove();  //fade them both out
		});
		return false;
	});

	$('input[name=del]').click(function () {
		if ($(this).is(':checked')) {
			$('.uploadfields :input').attr('disabled', true);
			$('.uploadfields').css('color', '#ccc');
			$('.uploadfields :input').css('color', '#ccc');
		}
		else {
			$('.uploadfields :input').removeAttr('disabled');
			$('.uploadfields').css('color', '');
			$('.uploadfields :input').css('color', '');
		}

		return true;
	});
};

