
function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=550,height=600,left = 565,top = 225');");
}

function validatePrivPolicy()
{
	if(document.forms[0].chkPrivPol.checked == false)
	{
		alert("Please tick the Privacy Policy check box to confirm that you have read our Privacy Policy before you submit your details.");

		return false;
	}
	return true;
}


jQuery(document).ready(function($) {
	// Stuff to do as soon as the DOM is ready
	var tPanelTimeout;
	
	$(window).resize(function() {
		
		// console.log("header: "+$('#header').height());
		// console.log("footer: "+$('#footer').height());
		// console.log("window: "+$(window).height());
		
		var padding = $('#header').height() + $('#footer').height();
		var height = Math.max(($(window).height() - padding), $('#content').height());
		$('#content').css("height",  height + 'px');
	});
	
	$(window).resize();
	
	$(".navSearch-nojava").replaceWith("<input name='type' class='searchHidden' type='hidden' value='All' />")
	$("#navSearch-field").click(function() {
		if ($(this).val() == "Search" ) {
			$(this).val("");
		}
	});
	$("#navSearch-field").blur(function() {
		if ($(this).val() == "" ) {
			$(this).val("Search");
		}
	});
	
	
	// navGenre
	$("#header .navGenre ul li a").live("mouseover", function() {
		$(this).animate({
			height: "25px",
			top: "-3px",
			paddingTop: "11px"
		}, 100 );
	});
	
	$("#header .navGenre ul li a").live("mouseout", function() {
		$(this).animate({
			height: "22px",
			top: "0px",
			paddingTop: "8px"
		}, 100 );
	});
	// Ends navGenre
	
	// navSearch
	$("#header .navSearch .btn-dropdown").removeClass("hide");
	
	// $("#header .navSearch .btn-dropdown").live("mouseover", function() {
	// 		$(".navSearch-dropdown").removeClass("hide");
	// 		$(this).css("backgroundPosition","bottom left");
	// 	});
	
	$("#header .navSearch .btn-dropdown").click(function() {
		if( $(".navSearch-dropdown").hasClass("hide") ) {
			$(".navSearch-dropdown").removeClass("hide");
			$("#header .navSearch .btn-dropdown").css("backgroundPosition","bottom left");
			clearTimeout(tPanelTimeout);
			return false;
		} else{
			$(".navSearch-dropdown").addClass("hide");
			$("#header .navSearch .btn-dropdown").css("backgroundPosition","top left");
			clearTimeout(tPanelTimeout);
			return false;
		}
	});
	
	$("#header .navSearch .navSearch-dropdown a").click(function() {
		$(".navSearch-dropdown").addClass("hide");
		var selection = $(this).text();
		$("#header .navSearch .btn-dropdown").text(selection);
		$("#header .navSearch .btn-dropdown").css("backgroundPosition","top left");
		clearTimeout(tPanelTimeout);
		return false;
	});
	
	$("#header .navSearch .navSearch-dropdown").mouseenter(function() {
		$("#header .navSearch .btn-dropdown").css("backgroundPosition","bottom left");
		clearTimeout(tPanelTimeout);
		return false;
	});
	
	$("#header .navSearch .navSearch-dropdown, #header .navSearch .btn-dropdown").mouseleave(function() {
		clearTimeout(tPanelTimeout);
		tPanelTimeout=setTimeout(" $(\".navSearch-dropdown\").addClass(\"hide\"); $(\".btn-dropdown\").css(\"backgroundPosition\",\"top left\") ",900);
		return false;
	});
	
	$("#header .navSearch .navSearch-dropdown a").click(function() {
		clearTimeout(tPanelTimeout);
		var rel = $(this).attr("rel");
		$(".searchHidden").attr("value",rel);
		return false;
	});
	// Ends navSearch
	
	// SG
	$(".sg .item").mouseenter(function() {
		if ( !$(this).hasClass("selected") ) {
			$(this).animate({
				opacity: 1
			}, 500 );
		}
	});
	// Ends SG
	
	// Start slideshow
	// Sets the navigation buttons for the carousel
	function setSlideShowNavigation()
	{
		var itemsPerPage = 4;
		
		// Try get the number of items in the list
		var numberOfItems = $('ul#sliderList > li').length;
		
		// Now we have the total number of items and the items per page,
		//  work out the maximum number of pages
		var maxNumberOfPages = Math.ceil(numberOfItems / itemsPerPage);
		
		var navigationContent = '';
		
		if (maxNumberOfPages > 1)
		{
			navigationContent = '<a href="#" rel="0" class="btn btn-arw-l mar-5-r sliderNav" title="Slide left" alt="Left arrow"></a>';
		
			// With these we know how many items we need
			var i = numberOfItems;
			var iItemCounter = 0;
			var selected = '';
			
			// Loop through the items and make buttons for all the pages
			while (i > 0)
			{
				if (iItemCounter == 0)
				{
					selected = 'selected';
				}
				else
				{
					selected = '';	
				}
				
				navigationContent = navigationContent+'<a href="#" rel="'+(iItemCounter)+'" class="btn btn-cir '+selected+'" alt="Slide to Panel '+(iItemCounter+1)+'"></a>';
				
				i = i - itemsPerPage;
				iItemCounter++;
			}
			
			navigationContent = navigationContent+'<a href="#" rel="0" class="btn btn-arw-r mar-5-l sliderNav" title="Slide right" alt="Right arrow"></a>';
		}
		
		// Set the control div content
		$("#carouselControls").html(navigationContent);
	}
	
	setSlideShowNavigation();
	
	// Gallery dots
    $(".ss .controller .btn-cir").click(function() 
	{
		var oldPos = $(".ss .controller .selected").attr("rel");
		var rel = $(this).attr("rel");

		// If this button isn't already selected
		if ( !$(this).hasClass("selected") ) 
		{
			$(".ss .controller .btn-cir").removeClass("selected");
			$(this).addClass("selected");
			
			slide(".viewer ul", rel, oldPos);
			$(".ss .controller .btn-arw-l").attr("rel",rel);
			$(".ss .controller .btn-arw-r").attr("rel",rel);			
		}

		return false;
    });
	
	// This is the onClick event for the new item carousel 
	$(".ss .controller .sliderNav").click(function() 
	{
		var itemsPerPage = 4;
		
		// Try get the number of items in the list
		var numberOfItems = $('ul#sliderList > li').length;
		
		// Now we have the total number of items and the items per page,
		//  work out the maximum number of pages
		var maxNumberOfPages = Math.ceil(numberOfItems / itemsPerPage);	
		
		// Get the current position of the carousel
		var oldPos = $(".ss .controller .selected").attr("rel");
		var rel = $(this).attr("rel");
	
		// And default the new position to 0
		var newPos = 0;		
		
		// If the user clicked the left arrow
		if ($(this).hasClass("btn-arw-l")) 
		{			
			// Check if they're not at the first position
			if (rel > 0) 
			{
				// Set the new position to one before the current one
				newPos = parseInt(rel) - 1;	
			} 
			else 
			{
				// Otherwise set it to the last possible page
				newPos = maxNumberOfPages-1;
			}
			
			// Set the right button to the current position
			$(".ss .controller .btn-arw-r").attr("rel", newPos);
			$(this).attr("rel", newPos);			
		} 
		else 
		{
			// If the current position is not the last possible position
			if (rel < maxNumberOfPages-1) 
			{
				// Then add one to the current position
				newPos = parseInt(rel) + 1;
			} 
			else 
			{
				// Otherwise, back to square one
				newPos = 0;
			}
			
			// And set the left arrow to this new position
			$(".ss .controller .btn-arw-l").attr("rel",newPos);
			$(this).attr("rel", newPos);
		}
		
		$(".ss .controller .btn-cir").removeClass("selected");
		$(".btn-cir[rel="+newPos+"]").addClass("selected");
		
		slide(".viewer ul", newPos, oldPos);
		
		return false;
	});
	
	// Ends slideshow
	
	
	// Tabs
	$(".tabs .tab-content").css("display","none");
	$(".tabs .tab-selected").css("display","block");
	
	$(".tabs .item").click(function() {

		if ( !$(this).hasClass("selected") ) {
			$(".tabs .item").addClass("tab-inactive");
			$(this).removeClass("tab-inactive");
		}
		
		if ( $(this).attr("rel") == 'fb' ) {
			$("#tab-tw").removeClass("tab-selected").hide();
			$("#tab-fb").addClass("tab-selected").show();
		} else if ( $(this).attr("rel") == 'tw' ) {
			$("#tab-fb").removeClass("tab-selected").hide();
			$("#tab-tw").addClass("tab-selected").show();
		} else if ( $(this).attr("rel") == 'wi' ) {
			$("#tab-tl").removeClass("tab-selected").hide();
			$("#tab-wi").addClass("tab-selected").show();
		} else if ( $(this).attr("rel") == 'tl' ) {
			$("#tab-wi").removeClass("tab-selected").hide();
			$("#tab-tl").addClass("tab-selected").show();
		}

		return false;
	});
	//
	
	jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
	return -c * ((t=t/d-1)*t*t*t - 1) + b;
	}
	
	jQuery.easing.easeOutQuint = function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t*t*t + 1) + b;
	}
	
	// image border fix for IE7
	$('.imgborder').mouseover(function() {
		// See if the imgborder class has a link inside it, and if so, apply the hover (added because of IE 7 bug)
		if ($(this).find("a").attr("href") > "") {
			$(this).addClass('imgborder-hover');
		}
	}).mouseout(function() {
		$(this).removeClass('imgborder-hover');
	});
	
	$("#featured-articles a").click(function() {		
		if (!$(this).hasClass("selected")) {
		
			var type = $(this).find("img").attr("rel");
			var newImage = $(this).attr("rel");
			
			
			// perform slide/cross-fade
			fadeImage($("#featured-articles a.selected").attr("rel"), newImage, type);
			moveSelected(newImage, type);
	
			$("#featured-articles a").removeClass("selected");
			$(this).addClass("selected");
			
			
			
			var status = $("#article-slideshow").attr("rel");
			
			if (status == 'play') {
				// stop auto rotating of images
				$("#article-slideshow").attr("rel","pause");
				clearTimeout ( slideshowTimer );
			}
		}
		
		return false;
	});
	
	// Start rotating through featured articles
	startSlideshow();
});

var slideshowTime = 5500;
var imageFade = 700;
var infoSlide = 350;

function moveSelected(newImage, type) {
	if (type == 'featSmall') {
		var top = 19 + (75 * (parseInt(newImage) - 1))+'px';
	} else {
		var top = 27 + (91 * (parseInt(newImage) - 1))+'px';
	}
	
	$("#featured-articles img.selected").animate({top: top}, 200);
}

function fadeImage(oldImage, newImage, type) {
	if (type == 'featSmall') {
		var margin = '326px';
		var top = '298px';
		var marginReset = '198px';
		var topReset = '188px';
	} else {
		var margin = '392px';
		var top = '364px';
		var marginReset = '265px';
		var topReset = '255px';
	}
	
	moveSelected(newImage, type);
	
	$(".featured-article-"+oldImage+" .info").animate({marginTop: margin}, infoSlide);
	$(".featured-article-"+oldImage+" .info-bg").animate({top: top}, infoSlide, function() {	
		
		$(".featured-article-"+newImage+" img").addClass("behind").removeClass("hide").css("opacity","1");
		$(".featured-article-"+newImage).addClass("behind").removeClass("hide");
		
		$(".featured-article-"+newImage+" img").stop().animate({"opacity": "1"}, imageFade, 'linear', function() {
			$(".featured-article-"+newImage+" .info").animate({marginTop: marginReset}, infoSlide);
			$(".featured-article-"+newImage+" .info-bg").animate({top: topReset}, infoSlide);
			$(".featured-article-"+newImage).removeClass("behind").addClass("show");
		});
		
		$(".featured-article-"+oldImage+" img").stop().animate({"opacity": "0"}, imageFade, 'linear', function() {
			$(this).removeClass("show").addClass("hide");
		});
		
		$(".featured-article-"+oldImage+" .info").animate({marginTop: margin}, 50);
	});	
}

function startSlideshow() {
	slideshowTimer = setTimeout ( "rotateSlideshow()", slideshowTime );
	$("#article-slideshow").attr("rel","play");
}

function rotateSlideshow() {
	$("#article-slideshow").attr("rel","play");
	var oldImage = $("#featured-articles a.selected").attr("rel");
	
	if (oldImage == 4) {
		var newImage = 1;
	} else {
		var newImage = parseInt(oldImage) + 1;
	}
	
	var type = $("#featured-articles a.selected img").attr("rel");
	
	fadeImage(oldImage, newImage, type);
	
	$("#featured-articles a").removeClass("selected");
	$("#featured-articles a[rel="+newImage+"]").addClass("selected");
	
	slideshowTimer = setTimeout ( "rotateSlideshow()", slideshowTime );
	var slideshowStart = true;
}

function slide(e, r, o) {
	var d = 800;
	var p = parseInt(r) * 652;	
			
	if (parseInt(o) > parseInt(r)) {
		var t = (parseInt(o) - parseInt(r)) * d;
	} else {
		var t = (parseInt(r) - parseInt(o)) * d;
	}
	
	$(e).stop().animate({right: p+"px"}, t, 'easeOutQuint');
	
}

// Facebook Connect functions
function onConnected(user_id) {
	$('#fb-top').css('display', 'inline');
}
function onNotConnected() {
	$('#fb-top').css('display', 'none');
}

















