/*
	name			: ClassBehaviours, the javascript framework based on class-name parsing
	update			: 9.11.9
	author			: Maurice van Creij
	dependencies	: jquery.classbehaviours.js
	info			: http://www.classbehaviours.com/

    This file is part of jQuery.classBehaviours.

    ClassBehaviours is a javascript framework based on class-name parsing.
    Copyright (C) 2008  Maurice van Creij

    ClassBehaviours is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    ClassBehaviours is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with ClassBehaviours. If not, see http://www.gnu.org/licenses/gpl.html.
*/

	// create the jQuery object if it doesn't already exist
	if(typeof(jQuery)=='undefined') jQuery = function(){};

	// create the root classbehaviours object if it doesn't already exist
	if(typeof(jQuery.classBehaviours)=='undefined') jQuery.classBehaviours = function(){};

	// create the handlers child object if it doesn't already exist
	if(typeof(jQuery.classBehaviours.handlers)=='undefined') jQuery.classBehaviours.handlers = function(){}

	// show or hide a node
	jQuery.classBehaviours.handlers.countDown = {
		// properties
		name: 'countDown',
		index: 0,
		// methods
		start: function(node){
			// start the count down
			node.id = (node.id) ? node.id : this.name + this.index++ ;
			this.count(node.id);
			//setTimeout('jQuery.classBehaviours.handlers.countDown.count("' + node.id + '")', 1000);
		},
		wait: function(waitStatus, waitReferer, waitError){
			jQuery.classBehaviours.utilities.setClassParameter(waitReferer, 'status', waitStatus);
		},
		insert: function(insertDoc, insertReferer, insertText){
			insertReferer.innerHTML = insertText.split('<body>')[1].split('</body>')[0];
		},
		// events
		count: function(id){
			var cnt = jQuery.classBehaviours.handlers.countDown;
			// get the root object
			objNode = document.getElementById(id);
			// assume default values
			allInputs = objNode.getElementsByTagName('INPUT');
			if(allInputs[0].value=='') allInputs[0].value = new Date().toUTCString();
			if(allInputs[1].value=='') allInputs[1].value = '0';
			if(allInputs[2].value=='') allInputs[2].value = document.location.href;
			// get the time properties
			theEnd = new Date(document.getElementById(id).getElementsByTagName('INPUT')[0].value);
			theOffset = parseInt(document.getElementById(id).getElementsByTagName('INPUT')[1].value);
			theUrl = document.getElementById(id).getElementsByTagName('INPUT')[2].value;
			// get the left over time
			leftOver = (theEnd - new Date()) + theOffset ;
			// if the countdown reaches 0
			if(leftOver<0){
				// retrieve the given URL
				jQuery.classBehaviours.ajax.addRequest(theUrl, cnt.insert, cnt.wait, null, objNode);
			}
			// else
			else {
				// format it
				leftOverDays  = parseInt(leftOver / 1000 / 60 / 60 / 24);
				leftOverHours  = parseInt(leftOver / 1000 / 60 / 60 - leftOverDays * 24);
				leftOverMinutes  = parseInt(leftOver / 1000 / 60 - leftOverHours * 60 - leftOverDays * 24 * 60);
				leftOverSeconds  = parseInt(leftOver / 1000 - leftOverMinutes * 60 - leftOverHours * 60 * 60 - leftOverDays * 24 * 60 * 60);
				// put it on the page
				allFields = document.getElementById(id).getElementsByTagName('DD');
				allFields[0].innerHTML = (leftOverDays<10 && leftOverDays>=0) ? '0' + leftOverDays : leftOverDays;
				allFields[1].innerHTML = (leftOverHours<10 && leftOverHours>=0) ? '0' +  leftOverHours : leftOverHours;
				allFields[2].innerHTML = (leftOverMinutes<10 && leftOverMinutes>=0) ? '0' +  leftOverMinutes : leftOverMinutes;
				allFields[3].innerHTML = (leftOverSeconds<10 && leftOverSeconds>=0) ? '0' +  leftOverSeconds : leftOverSeconds;
				// ask for the next step
				setTimeout('jQuery.classBehaviours.handlers.countDown.count("' + objNode.id + '")', 1000);
			}
		}
	}

	// add this addon to the jQuery object
	if(typeof(jQuery.fn)!='undefined'){
		// extend jQuery with this method
		jQuery.fn.countDown = function(){
			return this.each(
				function(){
					jQuery.classBehaviours.handlers.countDown.start(this);
				}
			);
		};
		// set the event handler for this jQuery method
		$(document).ready(
			function(){
				$(".countDown").countDown();
			}
		);
	}
