// JavaScript Document
var AjaxQueue = {
	batchSize: 1, //No.of simultaneous AJAX requests allowed, Default : 1
	urlQueue: [], //Request URLs will be pushed into this array
	elementsQueue: [], //Element IDs of elements to be updated on completion of a request ( as in Ajax.Updater )
	optionsQueue: [], //Request options will be pushed into this array
	setBatchSize: function(bSize){ //Method to set a different batch size. Recommended: Set batchSize before making requests
		this.batchSize = bSize;
	},
	push: function(url, options, elementID){ //Push the request in the queue. elementID is optional and required only for Ajax.Updater calls
		this.urlQueue.push(url);
		this.optionsQueue.push(options);
		if(elementID!=null){
			this.elementsQueue.push(elementID);
		} else {
			this.elementsQueue.push("NOTSPECIFIED");
		}

		this._processNext();
	},
	_processNext: function() { // Method for processing the requests in the queue. Private method. Don't call it explicitly
		if(Ajax.activeRequestCount < AjaxQueue.batchSize) // Check if the currently processing request count is less than batch size
		{
			if(AjaxQueue.elementsQueue.first()=="NOTSPECIFIED") { //Check if an elementID was specified
				// Call Ajax.Request if no ElementID specified
				//Call Ajax.Request on the first item in the queue and remove it from the queue
				new Ajax.Request(AjaxQueue.urlQueue.shift(), AjaxQueue.optionsQueue.shift()); 

				var junk = AjaxQueue.elementsQueue.shift();
			} else {
				// Call Ajax.Updater if an ElementID was specified.
				//Call Ajax.Updater on the first item in the queue and remove it from the queue
				new Ajax.Updater(AjaxQueue.elementsQueue.shift(), AjaxQueue.urlQueue.shift(), AjaxQueue.optionsQueue.shift());
			}
		}
	}
};

Ajax.Responders.register({
  //Call AjaxQueue._processNext on completion ( success / failure) of any AJAX call.
  onComplete: AjaxQueue._processNext
});

var url;
//url='http://www.designblox.com/clients/clevelandplus/';
url='http://' + location.hostname + '/site/';

function switchcollege(id) {
	
	if(id=='') return; 
	
	setCookie('collegeid',id);
	
	AjaxQueue.setBatchSize(1);

	AjaxQueue.push(url+"index.php?option=com_personalized&view=links&format=raw", {
		method:'get',
		parameters: {'collegeid': id},
		onSuccess: function(transport) {
		  var response = transport.responseText;
		  var data = response.evalJSON();
		  $('xmlfeed1_name').innerHTML = '<span>' + data.xmlfeed1_name + '</span>';
		  $('xmlfeed2_name').innerHTML = '<span>' + data.xmlfeed2_name + '</span>';
		  $('xmlfeed3_name').innerHTML = '<span>' + data.xmlfeed3_name + '</span>';
		  $('link1').innerHTML = '<a href="' + data.link1_url + '">' + data.link1_name + '</a>';
		  $('link2').innerHTML = '<a href="' + data.link2_url + '">' + data.link2_name + '</a>';
		  $('link3').innerHTML = '<a href="' + data.link3_url + '">' + data.link3_name + '</a>';
		  $('link4').innerHTML = '<a href="' + data.link4_url + '">' + data.link4_name + '</a>';
		  $('link5').innerHTML = '<a href="' + data.link5_url + '">' + data.link5_name + '</a>';
		  $('link6').innerHTML = '<a href="' + data.link6_url + '">' + data.link6_name + '</a>';
  		  $('school_logo').innerHTML = '<img src="' + data.logo + '" alt="' + data.college + '" />';
		}, 
		onfailure: function(){ alert('Something went wrong...') }
	});

    AjaxQueue.setBatchSize(1);
	
	AjaxQueue.push(url+"index.php?option=com_personalized&format=raw", {
		method:'get',
		parameters: {'collegeid': id},
		onSuccess: function(transport){
		  var response = transport.responseText;
		  var data = response.evalJSON();
		  var output1 = '';
		  var output2 = '';
		  var output3 = '';
   		  var forecast_high = '';
   		  var forecast_low = '';
          var condition_image = '';
		  var zipcode ='02215';
		  var fivedayforecastlink ='http://www.weather.com/weather/local/';

		  data.weather.each(function (element,index) {
			  if(element!=undefined) {
				  forecast_low = forecast_low + element.forecast_low;
  				  forecast_high = forecast_high + element.forecast_high;
                  condition_image = condition_image + element.condition_image;
				  zipcode = element.zipcode;
			  }
		  });
		  
		  forecast_high = (forecast_high=='') ? '' : forecast_high+'&deg;F' ;
  		  forecast_low = (forecast_low=='') ? '' : forecast_low+'&deg;F' ;
		  zipcode = (zipcode=='02215') ? '' : zipcode ;

          $('forecast_high').innerHTML=forecast_high;
		  $('forecast_low').innerHTML=forecast_low;
		  $('condition_image').style.background='url(' + condition_image + ') no-repeat scroll left top';
		  $('fivedayforecast').href=fivedayforecastlink + zipcode;
		  
		  //First Tab
		  output1 += '<ul>';
		  data.feed1.each(function (element,index) {
			  if(element!=undefined) {						
			  	output1 = output1 + '<li><a href="' + element.url + '" target="_blank">' + element.title +  '</a></li>';
			  }
		  });
		  output1 += '</ul>';
		  $('tab1').innerHTML=output1;
		  
		  //Second Tab
		  output2 = '';
		  output2 += '<ul>';
		  data.feed2.each(function (element,index) {
			  if(element!=undefined) {						
			  	output2 = output2 + '<li><a href="' + element.url + '" target="_blank">' + element.title +  '</a></li>';
			  }
		  });
		  output2 += '</ul>';
		  $('tab2').innerHTML=output2;

		  //Third Tab
		  output3 = '';
		  output3 += '<ul>';
		  data.feed3.each(function (element,index) {
			  if(element!=undefined) {						
			  	output3 = output3 + '<li><a href="' + element.url + '" target="_blank">' + element.title +  '</a></li>';
			  }
		  });
		  output3 += '</ul>';
		  $('tab3').innerHTML=output3;

		}, 
		onfailure: function() { alert('Something went wrong...') }
	});
}

/**
 * Function : dump()
 * Arguments: The data - array,hash(associative array),object
 *    The level - OPTIONAL
 * Returns  : The textual representation of the array.
 * This function was inspired by the print_r function of PHP.
 * This will accept some data as the argument and return a
 * text that will be a more readable version of the
 * array/hash/object that is given.
 * Docs: http://www.openjs.com/scripts/others/dump_function_php_print_r.php
 */
function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";
}

function setCookie(c_name,value,expiredays)
{
  var exdate=new Date();
  exdate.setDate(exdate.getDate()+expiredays);
  document.cookie=c_name+ "=" +escape(value)+
  ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function checkCookie()
{
  collegeid=getCookie('collegeid');
  if (collegeid!=null && collegeid!="")
    setCookie('collegeid',collegeid,365);
}
