// $Date: 2009/04/13 11:08:29 $
// $Name: ukconcept_v1_r84 $
// $Revision: 1.3 $
// $State: Exp $

var selectedMonth = "";
var selectedDestination = "";
var selectedFlighttime = "";
var initCount = 0;
var useDummyContentPane = false;


function init(){
	// workaround for anim flickering in gecko
	var ua = navigator.userAgent.toLowerCase();
	
	if (ua.indexOf('gecko') > -1 && ua.indexOf('safari') <= -1)
	{
		useDummyContentPane = true;
	}

	initTemperatures();

	if (document.getElementById('dummy').innerHTML != '')
	{
		if (useDummyContentPane)
		{
			
			document.getElementById('whats-hot-wrapper').innerHTML = document.getElementById('dummy').innerHTML;
			document.getElementById('dummy').innerHTML = '';
		}
		
	}
	
	initTemperatureBar();
	// get a list of all nav links (which we're adding ajax to)
	// first parameter to getElementsByClassName is classname
	// second is the parent element to start searching from
	// third is the type of tag e.g. a (anchor tag)
	var elements = getElementsByClassName('monthLink',document,'a');

	// loop through all the elements
	// note that there is only 1 in this example
	for (var i = 0; i< elements.length; i++) {
		// for the "click" event for each link, let the function navLinkHandler handle the event
		addEvent(elements[i],"click",navLinkHandler,false );
		//console.log(elements[i])
	}

	initCountryList();
	anim.start();
	if (initCount == 0)
		//trackDestination();
	ChildrenInit();
	fillDay();
	writeMonths();
	initCount++;
	setTimeout(selectHSDestination,500);
}

function selectHSDestination(){
	if ($('destinationList').selectedIndex > -1)
	{
	
		var selectedDestListOption = $('destinationList').options[$('destinationList').selectedIndex];
		if (selectedDestListOption.id)
		{
			var dest = selectedDestListOption.id.substring('option-list-'.length);
			if ($('option-'+dest))
			{
				$('searchCriteria.locationCode').selectedIndex = $('option-'+dest).index;
			}
		}	
	} else {
		$('searchCriteria.locationCode').selectedIndex = 0;
	}
}

function navLinkHandler(ev){
	// cancel the default click events for links
	stopDefaultAction(ev);

	// get the url of the link initiating the event
	var url = ev.currentTarget.href;
    var url_array = url.split("/");
    var len = url_array.length;
    var month = "";
    var destination = "";

    for (var i = 0; i< url_array.length; i++) {
        //if array item equals meta key "view" and appkey equals "wheres-hot"
        if ((url_array[i] == "view") && (url_array[len-1] == "wheres-hot")){
            //if next item in array is a month name assign value to month variable
            if (url_array[i+1]=="jan" || url_array[i+1]=="feb" || url_array[i+1]=="mar" || url_array[i+1]=="apr" || url_array[i+1]=="may" || url_array[i+1]=="jun" || url_array[i+1]=="jul" || url_array[i+1]=="aug" || url_array[i+1]=="sep" || url_array[i+1]=="oct" || url_array[i+1]=="nov" || url_array[i+1]=="dec"){
                month = url_array[i+1];
                //if next item in array is not the appkey ("where's-hot") assign the value to the destination variable
                if(url_array[i+2] != "wheres-hot"){
                    destination = url_array[i+2];
                }
            }else if (url_array[i+2] != "wheres-hot"){
                destination = url_array[i+1];
                month = url_array[i+2];
            }else{
                destination = url_array[i+1];
            }
        }
    }
	selectedMonth = month;
	//console.log('destination: ' + destination + ', month: ' + month)

    // get the month and destination info
    var newurl = '/_category/travel_wheresHot.adp?month=' + month;
	if (destination != "")
	{
		newurl += '&destination=' + destination;
		selectedDestination = destination;
	}
	if (selectedFlighttime != "")
	{
		newurl += '&flighttime=' + selectedFlighttime;
	}
	

	$('loading').style.display = 'block';
	// make an AJAX request to the url and once that's done, call updateContentBlock with the response
	makeRequest(newurl, updateContentBlock);
}

function updateContentBlock(url, str){
	// get the content div and set it's body to the response string
	if (useDummyContentPane)
	{
		document.getElementById('dummy').innerHTML = str;
	} else {
		document.getElementById('whats-hot-wrapper').innerHTML = str;
	}
	$('loading').style.display = 'none';
	
	init();
	document.getElementById('destinationList').focus();
}

var degreeElems;
var temperatureGraph =  new Object();
temperatureGraph.months = new Array();

function initTemperatureBar(){
	degreeElems = getElementsByClassName('degrees',$('temperature-graph'),'td');
	for(var i=0; i < degreeElems.length; i++) {
		var idx = i + 1;
		temperatureGraph.months[i].barElem = $('wh-month-bar'+idx);
	}
}

function initTemperatures(){
	degreeElems = getElementsByClassName('degrees',$('temperature-graph'),'td');
	for(var i=0; i < degreeElems.length; i++) {
		var idx = i + 1;
		//degreeElems[i].className = 'degrees degrees-1';
		if (!temperatureGraph.months[i]) {
			temperatureGraph.months[i] = new Object();
			temperatureGraph.months[i].oldTemp = 0.1;
		} else {
			temperatureGraph.months[i].oldTemp = temperatureGraph.months[i].temp;
		}
		temperatureGraph.months[i].barElem = $('wh-month-bar'+idx);
		temperatureGraph.months[i].barElem.style.height = (temperatureGraph.months[i].oldTemp/10) + 'em';

		if ($('wh-month-'+idx)) {
			temperatureGraph.months[i].temp = $('wh-month-'+idx).innerHTML;
		}
	}
}



function trackDestination(dest){
try{
	if(!dest){
		var selectElem = document.getElementById('destinationList');
		var selectedIndex = selectElem.selectedIndex;
		var selectedOption  = selectElem.options[selectedIndex]
		dest = selectedOption.value;
	}

	s_265.prop18=dest;
	var omniText = "Wheres Hot Now Chosen Destination"; 
	if (initCount == 0)
	{
		omniText = "Wheres Hot Now Displayed Destination"; 
	}

	s_265.tl(this,"o",omniText);
	s_gi(s_account);
}catch (e){}
return true;
}

function initCountryList(){
	// get a list of all nav links (which we're adding ajax to)
	// first parameter to getElementsByClassName is classname
	// second is the parent element to start searching from
	// third is the type of tag e.g. a (anchor tag)
	
	addEvent(document.getElementById('destinationList'),"change",countryListHandler,false );
	
}
function countryListHandler(ev){
	$('loading').style.display = 'block';
	var selectElem = ev.currentTarget;
	var selectedIndex = selectElem.selectedIndex;
	var selectedOption  = selectElem.options[selectedIndex]
	var destination = selectedOption.value;
	trackDestination(destination);
	var flighttime = document.getElementById('flighttimeList').value;
	selectedFlighttime = flighttime;
	
	document.getElementById('flighttime-month').value = selectedMonth;
	document.getElementById('flighttime-destination').value = destination;

	var newurl = '/_category/travel_wheresHot.adp?destination=' + destination;
	if (selectedMonth != "")
	{
		newurl += '&month=' + selectedMonth + '&flighttime=' + flighttime;
	}

	selectedDestination = destination;
	
	makeRequest(newurl, updateContentBlock);
}

function Animate(){
	this.cnt = 0;
	this.interval = 10;
	this.time = 50;
	this.elapsed = 0;

	this.init = function(){
		this.cnt = 0;
		this.elapsed = 0;
	}
	this.start = function(){
		
		this.init();		
		function fn(obj){return (function(){obj.run();});}
		var functRef = fn(this);		
		setTimeout(functRef, this.interval);
	}

	this.run = function(){
		
		this.cnt++;

		this.grow();	
		this.elapsed += this.interval;
		if (this.elapsed < this.time){
			function fn(obj){return (function(){obj.run();});}
			var functRef = fn(this);			
			setTimeout(functRef, this.interval);
		} 
	}

	this.grow = function(){
		var elems = temperatureGraph.months;
		for (var i = 0; i < elems.length; i++) {

			var changeTemp = elems[i].temp - elems[i].oldTemp;
			var block = changeTemp*(this.interval/this.time)/10;
			var elem = elems[i].barElem;

			var currHeight = 0;
			if (elem.style.height){
				currHeight = parseFloat(elem.style.height.substring(0,elem.style.height.length-2));	
			}

			var nextHeight = (block+currHeight);
			elem.style.height  = nextHeight + 'em';
			

			
		}
	}
	this.resetBars = function(){
		var degreeElems = getElementsByClassName('degrees',$('temperature-graph'),'td');
		for (var i = 0; i < degreeElems.length; i++) {
			degreeElems[i].className = 'degrees degrees-1';
			temperatureGraph.months[i].barElem.style.height = '';
		}
	}
}




var anim = new Animate();

