$(document).ready(function() {
	homeGallery();
	headerGallery();
	jsddm_load();
	directions();
	misc();
});

function oc(a)
{
  var o = {};
  for(var i=0;i<a.length;i++)
  {
    o[a[i]]='';
  }
  return o;
}

function headerGallery(){
	var rand_array = new Array();
	var a = 0;
	while(a < 4){
		var random = Math.floor(Math.random()*15);
		if(random == 0){
			continue;
		}
		if(!(random in oc(rand_array))){
			rand_array[a] = random;
			a++;
		}
	}

	var str = "";
	for ( var i in rand_array )
	{
		str += "{ src: '/assets/images/headers/header-" + rand_array[i] + ".jpg', dir: 'up'},\n";
	}

	str = str.substr(0,str.length - 2);
	str = "var arr = [{ src: '/assets/images/headers/header-0.jpg', dir: 'up'}," + str + "]";
	eval(str);

	$('#flashheader').crossSlide(
	{
	  sleep: 3,  //px/sec
	  fade: 3    //sec
	}, arr);
}

function homeGallery(){
	//if($('#thumbsbase-landscape') == 0){
	//	return false;
	//}
	
	var rand_array = new Array();
	var a = 0;
	var html_out = "";
	while(a < 5){
		var random = Math.floor(Math.random()*13);
		if(random == 0){
			continue;
		}
		if(!(random in oc(rand_array))){
			rand_array[a] = random;
			a++;

			html_out += '<li><a href="/assets/images/large/IMG_'+random+'.jpg">';
			html_out += '<img src="/assets/images/thumbs/IMG_'+random+'.jpg" width="80" height="80" border="0" alt="Gallery"/></a></li>';
		}
	}

	//Insert into the UL.
	$('#thumbsbase-landscape').html(html_out);
	return true;
}


var timeout         = 500;
var closetimer		= 0;
var ddmenuitem      = 0;

function jsddm_open(){
	jsddm_canceltimer();
	jsddm_close();
	$("#thingstodo").addClass('test');
	ddmenuitem = $(this).find('ul').eq(0).css('visibility', 'visible');
}

function jsddm_close(){
	if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');
}

function jsddm_timer(){
	closetimer = window.setTimeout(jsddm_close, timeout);
}

function jsddm_canceltimer()
{	
	if(closetimer){
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

function jsddm_load(){
	$('.jsddm > li').bind('mouseover', jsddm_open);
	$('.jsddm > li').bind('mouseout',  jsddm_timer);

	var sQ = location.search.substring();
	if(sQ.indexOf('showoffer')!= -1){
		showOffer();
	}
}

document.onclick = jsddm_close;

function directions(){
	$("#btnGetDirections").click(function(){
		sPostCode = $('#txtPostCode').val();

		if(sPostCode){
			window.open('http://maps.google.com/maps?f=d&daddr=14+Charlton+Street,+Llandudno,+LL30,+United+Kingdom+(Llandudno+Hostel)&saddr=' + escape(sPostCode) + '&hl=en');
		}else{
			alert('eek');
		}
	});
}

function misc(){
	$('#txtPostCode').focus(function(){
		$(this).val('');
		$(this).addClass('standard');
	});

	$('$btnGetDirections').click(function(){
		window.open('');
	});

	$('.thumbshow a').lightBox();
	$('.offershow a').lightBox();

	$('.offershow a').click(function(){
		pageTracker._trackPageview ('/BannerOfferClick')
	});
}

function formatArrival(){
	var iDay = document.forms.frmAv.day.value;
	var sMonth = document.forms.frmAv.month.value;
	var iYear = document.forms.frmAv.year.value;
	iYear = iYear.substr(2);

	document.forms.frmAv.arrival.value = iDay+' '+sMonth+' '+iYear;
}

function LaunchPop(width,height) {
	formatArrival();
	//OpenWin = this.open('/redirect.php?s=powerhostels&l=' + escape('http://www.powerhostels.com/hbaffiliate/index.cfm?fuseaction=checkhos&currency=GBP&affid=4069&lan=en&hostel=4069&arrival=' + document.forms.frmAv.arrival.value + "&nights="+document.forms.frmAv.nights.value), "oBookingWindow", "toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,dependent=no,directories=no,width=" + width + ",height=" + height + ",x=50,y=50");
	OpenWin = this.open('/redirect.php?s=guestlink&l=' + escape('http://web.guestlink.co.uk/thedms.aspx?dms=2&dc=MW&wc=book&gc=book&pid=0822316&startdate=' + document.forms.frmAv.arrival.value + "&nd="+document.forms.frmAv.nights.value+"#Availability"), "oBookingWindow", "toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,dependent=no,directories=no,width=" + width + ",height=" + height + ",x=50,y=50");		
}
