$(function(){

	function openModal(id,top) {
		$(id).lightbox_me({
			modalCSS: {top: top+'px'}
		});
	}

	// Various Modals
	$('#open-terms, #lbl-open-terms').click(function() { openModal('#modal-terms',50); return false; });
	$('#lbl-open-terms-comp').click(function() { openModal('#modal-terms-comp',50); return false; });
	$('.fb #lbl-open-terms-comp').click(function() { openModal('#modal-terms-comp-fb',50); return false; });
	$('.fb #lbl-open-terms').click(function() { openModal('#modal-terms',200); return false; });
	$('#open-inspiration').click(function() { ftGoalTag5033(); openModal('#modal-inspiration',50); return false; }); // The ftGoalTag5033 is the Pixel Tracking script (see below).
	$('.fb #open-inspiration').click(function() { ftGoalTag5033(); openModal('#modal-inspiration',800); return false; });
	$('.open-example').click(function() { openModal('#modal-example',300); return false; });
	$('.fb .open-example').click(function() { openModal('#modal-example',800); return false; });
	$('#open-privacy').click(function() { openModal('#modal-privacy',200); return false; });
	$('.fb #open-privacy').click(function() { openModal('#modal-privacy',200); return false; });

	//Example Gallery
	function setActive(el) {
		$('#galleryThumbs a').removeClass('active');
		$(el).addClass('active');
	}

	function imageClick(sel) {
		var source = $(sel+' img').attr('src');
		$('#main-gallery-image').attr('src',source);
		setActive(sel);
	}

	$('#image1').click(function() { imageClick('#image1'); return false; });
	$('#image2').click(function() { imageClick('#image2'); return false; });
	$('#image3').click(function() { imageClick('#image3'); return false; });
	$('#image4').click(function() { imageClick('#image4'); return false; });
	$('#image5').click(function() { imageClick('#image5'); return false; });

	/* Gallery */
	$('#share-entry').click(function () {
		$('#share-popup').toggle();
	});

	$('#search').focus(function () {
		$('#search-label').hide();
	});

	$('#search-label').click(function () {
		$(this).hide();
		$('#search').trigger('focus');
	});

	// Vote for entry - Captcha Barrier
	$('#vote-entry').click(function () {
		$('#voting-popup').toggle();
	});

	$('a#submit_vote').click(function() {
		if ($('#recaptcha_response_field').val() == '') {
			$('#recaptcha_response_field').css('background','#f88');
			$('#recaptcha_response_field').css('border','1px solid #f00');
			return false;
		} else {
			$('#formVote').submit();
		}
	});

	$('#recaptcha_response_field').focus(function() {
		$(this).css('background','#fff');
		$(this).css('border','1px solid #acadaf');
	});

	$('#vote_popup_close').click(function() {
		$('#voting-popup').hide();
	});

	$('#thanks_popup_close').click(function() {
		$('#thanks-popup').hide();
	});

	function recaptchaResult(b) {
		if (b) {
			alert('recaptcha wrong');
		} else {
			alert('recaptcha correct');
		}
	}

	//Search form inline label
	$('#search').blur(function () {
		if ($('#search').val() == '') {
			$('#search-label').show();
		}
	});

	if ($('#search').val() != '') { $('#search-label').hide(); }


	//Countdown
	$('#defaultCountdown').countdown({until: new Date(2011, 8-1, 8)});

	// -- Pixel Tracking Implementation
	// Homepage - Enter Now button
	$('#home-enternow').click(function () { ftGoalTag5027(); });

	function ftGoalTag5027(){
		var ftRand = Math.random() + "";
		var num = ftRand * 1000000000000000000;
		var ftGoalTagPix5027 = new Image();
		ftGoalTagPix5027.src = "http://servedby.flashtalking.com/spot/1391;5027;582/?Homepage Enter Now" + num;
		ftGoalTagPix5027.onload = ftLoaded5027;
	}

	function ftLoaded5027() {
		top.location.href="/design-a-den/";
	}

	// Homepage - Find out more button
	$('#home-findoutmore').click(function () { ftGoalTag5028(); });

	function ftGoalTag5028(){
		var ftRand = Math.random() + "";
		var num = ftRand * 1000000000000000000;
		var ftGoalTagPix5028 = new Image();
		ftGoalTagPix5028.src = "http://servedby.flashtalking.com/spot/1391;5028;582/?Homepage Find Out More" + num;
		ftGoalTagPix5028.onload = ftLoaded5028;
	}

	function ftLoaded5028() {
		top.location.href="/fun-with-fab/";
	}

	// Design-a-Den - Upload your entry now link
	/* -- Removed. This was breaking in Facebook.
	$('#dad-uploadyourentry').click(function () { ftGoalTag5030(); });
	$('#dad-enternow').click(function () { ftGoalTag5030(); });

	function ftGoalTag5030(){
		var ftRand = Math.random() + "";
		var num = ftRand * 1000000000000000000;
		var ftGoalTagPix5030 = new Image();
		ftGoalTagPix5030.src = "http://servedby.flashtalking.com/spot/1391;5030;582/?Upload Your Entry Now" + num;
		ftGoalTagPix5030.onload = ftLoaded5030;
	}

	function ftLoaded5030() {
		top.location.href="?sub=form";
	}
	*/

	// Design-a-Den - Download link
	$('#dad-download').click(function () { ftGoalTag5031(); });

	function ftGoalTag5031(){
		var ftRand = Math.random() + "";
		var num = ftRand * 1000000000000000000;
		var ftGoalTagPix5031 = new Image();
		ftGoalTagPix5031.src = "http://servedby.flashtalking.com/spot/1391;5031;582/?Design a Fab Den Print Download" + num;
		ftGoalTagPix5031.onload = ftLoaded5031;
	}

	function ftLoaded5031() {
		top.location.href="../downloads/FAB_Den_Template2011.pdf";
	}

	// Design-a-Den - Inspiration Gallery link (activated above).
	function ftGoalTag5033(){
		var ftRand = Math.random() + "";
		var num = ftRand * 1000000000000000000;
		var ftGoalTagPix5033 = new Image();
		ftGoalTagPix5033.src = "http://servedby.flashtalking.com/spot/1391;5033;582/?Design A Fab Den View Some Inspiration" + num;
		return true;
	}

	// Design-a-Den - Download link
	$('#nav-facebook').click(function () { ftGoalTag5037(); });

	function ftGoalTag5037(){
		var ftRand = Math.random() + "";
		var num = ftRand * 1000000000000000000;
		var ftGoalTagPix5037 = new Image();
		ftGoalTagPix5037.src = "http://servedby.flashtalking.com/spot/1391;5037;582/?Fab Facebook Link" + num;
		ftGoalTagPix5037.onload = ftLoaded5037;
	}

	function ftLoaded5037() {
		top.location.href="http://www.facebook.com/fabicelolly";
	}

});
