// check today
var today = new Date();
var year  = today.getYear();

if(!Prototype.Browser.IE)
	year+=+1900;
var month = today.getMonth()+1;
var date  = today.getDate();

// see if there's any time left
var timeLeft = (year == 2008 && month == 12 && date < 20) ? true : false;

// messages
var msg = new Array();

msg[0] = '<h2>Wil jij ook een Christmas Sock?</h2><p>Breng dan snel je campagne onder bij <a href="http://www.totalexperience.nl" title="Total Experience">Total Experience</a>. <br />Je kunt bij ons terecht voor web 2.0 toepassingen, oogverblindend grafisch ontwerp, <br />aanstekelijke virals en copy die doel treft.</p>';
msg[1] = '<h2>Helaas, er kunnen geen sokken meer worden gekozen.</h2><p>Heb je al een sok gekozen? We sturen hem je snel toe!</p>';
msg[2] = '<h2>Je hebt je Christmas Sock gekozen.<br /><br /><object width="400" height="321"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=2464143&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=2464143&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1&amp;autoplay=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="321"></embed></object></div></h2>';
msg[3] = '<h2>Helaas, iemand was u voor.<br /> Voelt u zich vooral vrij een ander cadeau te kiezen.</h2>';

msg[4] = '<p>| <a href="" class="closeBtn" onclick="hideOverlay(); return false;" title="sluiten">Terug naar de Sokken</a> |</p>';

// window load
Event.observe(window, 'load', function(){
	
	// check for IE mouseovers
	if(Prototype.Browser.IE){
		$$('.block li').invoke('observe', 'mouseover', liHoverHandler);
		$$('.block li').invoke('observe', 'mouseout', liOutHandler);
	}
	
	if(hashash && timeLeft){
		
		$('overlay').hide();
	
		$$('.block a').invoke('observe', 'click', boxClickHandler);
		$$('.claimed a').invoke('stopObserving');
		$$('.claimed a').invoke('observe', 'click', disableClick);
		
		Event.observe('yesBtn','click', yesClickHandler);
		Event.observe('noBtn','click', noClickHandler);
		
		$$('.closeBtn').invoke('observe', 'click', hideOverlay);
		
	}else{
		$$('.block a').invoke('observe', 'click', notLoggedInClickHandler);
		$$('.claimed a').invoke('stopObserving');
		$$('.claimed a').invoke('observe', 'click', disableClick);
	}

	// year, month, date, hours
	//countdown_clock(08, 12, 15, 17, 00);

	if(!timeLeft)
		$('countdown').hide();
		
	
});

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- show overlay
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

function showOverlay(){
	arrayPageSize = getPageSize();
	$('overlay').style.height = (arrayPageSize[1] + 'px');
	$('overlay').show();
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- box click handler
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

function boxClickHandler(evt){

	evt.target.setAttribute('id', 'selected');

	showOverlay()
	
	new Effect.ScrollTo('message', {offset: -100});
	
	evt.stop();
}


/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- list hover handlers
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

function liHoverHandler(evt){

	if(evt.target.hasAttribute('href'))
		evt.target = evt.target.up();
		
	evt.target.addClassName('hover');
}

function liOutHandler(evt){
	if(evt.target.hasAttribute('href'))
		evt.target = evt.target.up();
		
		evt.target.removeClassName('hover');
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- hide overlay
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

function hideOverlay(){
	$('overlay').hide();
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- disable clicks
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

function disableClick(evt){
	evt.stop();
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- yes click handler
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

function yesClickHandler(evt){
	
	var person = person_id;
	
	new Ajax.Request('/select_present/',{
		method: "get",
		parameters: { 
			present_id: $('selected').previous().innerHTML, 
			person_id: person 
		},
		onSuccess: function(ajaxRequest) {
			var jsonObject = ajaxRequest.responseText.evalJSON(); 

			if(jsonObject.ack['ok'] == true){
				$('selected').innerHTML = "<em>"+company_name+"</em>";
				$('selected').up().addClassName('claimed');
			
				showThanxMessage()
			
				$$('.block a').invoke('stopObserving')
				$$('.block a').invoke('observe', 'click', disableClick)
			}else{
				showCustomErrorMessage(jsonObject.ack.alert)	
			}
			
		}
	});

	evt.stop();
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- when you're not logged in
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

function notLoggedInClickHandler(evt){
	showOverlay()
	new Effect.ScrollTo('message', {offset: -100});
	
	if(timeLeft)
		$('message').innerHTML = msg[0] + msg[4]; // sorry, you're not a customer [yet]
	else
		$('message').innerHTML = msg[1] +  msg[4]; // time's up
			
	evt.stop();
}


/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- messages
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

function showThanxMessage(){
	$('message').innerHTML = msg[2] + msg[4];
}

function showErrorMessage(){
	$('message').innerHTML = msg[3] + msg[4];
}
function showCustomErrorMessage(msg){
	$('message').innerHTML = '<h2>'+ msg +'</h2><p>| <a href="" class="closeBtn" onclick="hideOverlay(); return false;" title="sluiten">Terug naar de Sokken</a> |</p>';
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- no click handler
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

function noClickHandler(evt){
	$('selected').removeAttribute('id')
	hideOverlay()
	evt.stop();
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- getPageSize()
- Returns array with page width, height and window width, height
- Core code from - quirksmode.org
- Edit for Firefox by pHaez
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
