/**
 * lh.js
 * @author Lewis Howles
 *
 * Default Niceties.
 */

var lh = {

	/*
	 * Set default text for inputs (title attribute)
	 */
   setInputTexts : function(){
	   $("input[type=text], textarea").each(
		   function(){
			   $(this).val($(this).attr('title'));
		   }
	  );
	},

	/*
	* Show / hide text from inputs
	*/
	inputText : function(){
	   $("input, textarea").focus(function(event){
		   if($(this).val() === $(this).attr('title'))
			   $(this).val("");
	   }).blur(function(event){
		   if($(this).val() === "")
			   $(this).val($(this).attr('title'));
	   });
	},

	/*
	* Set target blank on external links
	*/
	externalLinks : function(){
		$('a[rel="external"]').attr('target', '_blank');
		$('a[rel="external nofollow"]').attr('target', '_blank');
		$('a[rel="nofollow external"]').attr('target', '_blank');
		$('#twitter_update_list a').attr('target', '_blank')
	}
}

$(function() {
	lh.externalLinks();

	var $ticker = $('#ticker');

	$('li', $ticker).show();

	$('.remove', '#ticker').click(function(){
		$ticker.fadeOut();
	});

	$('ul', $ticker).ticker({
		cursorList: " ",
		rate: 50,
		delay: 4000
	}).trigger("play");

	$('#accordion').accordion();

	if ($('#faqs').length) {
		var $faqs = $('#faqs');

		$('.answer', $faqs).hide();

		$('h4', $faqs).toggle(function(){
			$(this).addClass('open').next('.answer').show();
		}, function(){
			$(this).removeClass('open').next('.answer').hide();
		});
	}

	if ($('#romancart').length) {
		var $price = $.getUrlVar('price');

		$('#romancart').attr('src','http://www.romancart.com/cart.asp?storeid=12353&itemname=Vulcan to the Sky&quantity=1&price='+$price);
	}

	if ($('#posters').length) {
		$('a.iframe').fancybox({
			'width' : '95%',
			'height' : '95%',
			'autoScale' : false,
			'transitionIn' : 'none',
			'transitionOut' : 'none',
			'type' : 'iframe'
		});
	}

	if ($('#partners').length) {
		var $filtered, $shuffled, $partners = $('#partners ul');
		$shuffled = $partners.shuffle();
		$filtered = $shuffled.children('li:lt(10)');
		$('li', $partners).remove('li');
		$($partners).append($filtered);
	}

	($('#banner').length) && $('#banner').innerfade({
		timeout : 15000,
		containerheight : '125px'
	});
	
	start();
});

$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});


$.fn.shuffle = function() {
  return this.each(function(){
	var items = $(this).children();
	return (items.length)
	  ? $(this).html($.shuffle(items))
	  : this;
  });
}

$.shuffle = function(arr) {
  for(
	var j, x, i = arr.length; i;
	j = parseInt(Math.random() * i),
	x = arr[--i], arr[i] = arr[j], arr[j] = x
  );
  return arr;
}

// ****  Time Zone Count Down Javascript  **** //
/*
Visit http://rainbow.arch.scriptmania.com/scripts/
 for this script and many more
*/

////////// CONFIGURE THE COUNTDOWN SCRIPT HERE //////////////////

var month = '4';     //  '*' for next month, '0' for this month or 1 through 12 for the month 
var day = '1';       //  Offset for day of month day or + day  
var hour = 0;        //  0 through 23 for the hours of the day
var tz = 0;         //  Offset for your timezone in hours from UTC
var lab = 'tzcd';    //  The id of the page entry where the timezone countdown is to show

function start() {
   displayTZCountDown(setTZCountDown(month,day,hour,tz),lab);
}
function setTZCountDown(month,day,hour,tz) {
	var toDate = new Date();
	if (month == '*')toDate.setMonth(toDate.getMonth() + 1); else if (month > 0) {
		if (month <= toDate.getMonth())toDate.setYear(toDate.getYear() + 1);
		toDate.setMonth(month-1);
	}
	if (day.substr(0,1) == '+') {
		var day1 = parseInt(day.substr(1));
		toDate.setDate(toDate.getDate()+day1);
	} else {
		toDate.setDate(day);
	}
	toDate.setHours(hour);
	toDate.setMinutes(0-(tz*60));
	toDate.setSeconds(0);
	var fromDate = new Date();
	fromDate.setMinutes(fromDate.getMinutes() + fromDate.getTimezoneOffset());
	var diffDate = new Date(0);
	diffDate.setMilliseconds(toDate - fromDate);
	return Math.floor(diffDate.valueOf()/1000);
}
function displayTZCountDown(countdown,tzcd) {
	if (countdown < 0) document.getElementById(tzcd).innerHTML = "Sorry, you are too late."; else {
		var secs = countdown % 60;
		if (secs < 10) secs = '0'+secs;
		var countdown1 = (countdown - secs) / 60;
		var mins = countdown1 % 60;
		if (mins < 10) mins = '0'+mins;
		countdown1 = (countdown1 - mins) / 60;
		var hours = countdown1 % 24;
		var days = (countdown1 - hours) / 24;
		document.getElementById(tzcd).innerHTML = '<span id="day">' + days + '<span class="text">day' + (days == 1 ? '' : 's') + '</span>' + '</span>' + '<span id="hour">' + hours + '<span class="text">hour' + (hours == 1 ? '' : 's') + '</span>' + '</span>' + '<span id="min">' + mins + '<span class="text">min' + (mins == 1 ? '' : 's') + '</span>' + '</span>' + '<span id="second">' + secs + '<span class="text">sec' + (secs == 1 ? '' : 's') + '</span>' + '</span>';
		setTimeout('displayTZCountDown('+(countdown-1)+',\''+tzcd+'\');',999);
	}
}
