var timeout; // for timeout
var selectedImage;
var selectedTabDesc;
var imgList; //List to preload Images	
var Title = new Array();
var listName = new Array();

$(document).ready(function (){ 
	
	//Preload Images
	$('[category]').each(function(){
  		imgList = this.getAttribute("hoversrc")+","+ this.getAttribute("selectedsrc")+"," + this.getAttribute("offsrc");
  		//alert(imgList);
  		$.preloadImages(imgList);
  	});	
	// Access the getlistings file for categories when a particular tab is clicked.
	// Get listings for love tab which shd appear by default when user loads the page
	//GetRss($("#rsscat").val()); // Function to retrieve Rss Psychic tip of day		
	//GetListings($('#love-tab').attr("category")); // Get listings for love tab which shd appear by default when user loads the page
	//selectedImage = $('#love-tab')[0]; 
	
	if (($("#sTab").val() == "c1") || ($("#sTab").val() == "")){
		selectedImage = $("#love-tab")[0]; 
		selectedImage.src = $("#love-tab")[0].getAttribute('selectedsrc');
		$("#tab-desc")[0].src = $("#tab-desc")[0].getAttribute('lovesrc');
		GetListings($('#love-tab').attr("category")); 
		}
		else if ($("#sTab").val() == "c2"){
		selectedImage = $("#psychic-tab")[0]; 
		selectedImage.src = $("#psychic-tab")[0].getAttribute('selectedsrc');
		$("#tab-desc")[0].src = $("#tab-desc")[0].getAttribute('psychicsrc');
		GetListings($('#psychic-tab').attr("category")); 
		}
		else if ($("#sTab").val() == "c3"){
		selectedImage = $("#tarot-tab")[0]; 
		selectedImage.src = $("#tarot-tab")[0].getAttribute('selectedsrc');
		$("#tab-desc")[0].src = $("#tab-desc")[0].getAttribute('tarotsrc');
		GetListings($('#tarot-tab').attr("category")); 
		}
		else if ($("#sTab").val() == "c4"){
		selectedImage = $("#spirit-tab")[0]; 
		selectedImage.src = $("#spirit-tab")[0].getAttribute('selectedsrc');
		$("#tab-desc")[0].src = $("#tab-desc")[0].getAttribute('spiritsrc');
		GetListings($('#spirit-tab').attr("category")); 
		}
		else if ($("#sTab").val() == "c5"){
		selectedImage = $("#astro-tab")[0]; 
		selectedImage.src = $("#astro-tab")[0].getAttribute('selectedsrc');
		$("#tab-desc")[0].src = $("#tab-desc")[0].getAttribute('astrosrc');
		GetListings($('#astro-tab').attr("category")); 
		}
		else if ($("#sTab").val() != "c4"){
					if ($("#sTab").val() != "c5") {
		 			if ($("#sTab").val() != "c3") {
						if ($("#sTab").val() != "c2"){
							if($("#sTab").val() != "c1"){
							 if($("#sTab").val() != ""){
			
							selectedImage = $("#love-tab")[0]; 
							selectedImage.src = $("#love-tab")[0].getAttribute('selectedsrc');
							$("#tab-desc")[0].src = $("#tab-desc")[0].getAttribute('lovesrc');
							GetListings($('#love-tab').attr("category")); 
							}}}}}}
	
	$('[category]').click(function() { // bind click event to the love tab
		//alert("tab selected, cat is "+ $(this).attr("category"));//debugging code
		clearTimeout(timeout);   
		GetListings($(this).attr("category"));
		SetTabDesc($(this).attr("category"));
		return false;							
	});
	
	SetImageBehaviors(); 
	
});//close doc ready
			
function GetListings(category){

	$.ajax({
	url: '/documents/special_offers/getlistings.asp?tid=' + $("#tid").val() + '&cat=' + category + '&tlc=' + $("#tlc").val(),
	type: 'GET',
	dataType: 'xml',
	timeout: 5000,
	error: function(){
	getfListings();
	},
	success: function(xml){
		
	$('Listing',xml).each(function(i) {
		var count = jXML.getCount(xml,{"Listing":"Listing"});
      	//alert( count["Listing"]);
		if (count["Listing"] < 3) { getfListings();}
		//$("#fragment-2-container").empty();
		$("#fragment-1").show();
        listingTitle = $(this).find('title').text();
		Title[i] = listingTitle;
		
		listingImg = $(this).find('thumbNailImage').find('url').text();
		listingRate = $(this).find('rating').find('starsImage').find('url').text();
		listingName = $(this).find('memberName').text();
		listName[i] = listingName;
		listingmemUrl = $(this).find("memberUrl").text();
		listingUrl = $(this).find("expandedListingUrl").text();
		listingextn = $(this).find('phoneNumber').find('extension').text();		
		listingphno = $(this).find('phoneNumber').find('vanityNumber').text();	
		fullsizeImg = $(this).find('fullSizeImage').find('url').text();
			
		// the following statement is the clone of the div	
     	newE1 = $("#list-template").clone().fadeIn("slow");
       	newE1.find('#listingname-ed-832').html(listName[i].slice(0,20));
		newE1.find('#listingdesc-ed-832').html(Title[i].slice(0,50)+'...');
		//newE1.find('#listingdesc-ed-832').html(listingTitle);
		newE1.find('#divfakelisting-img').html('<img src="'+listingImg+'" />');
		newE1.find('#divfakelisting-img-full').html('<img src="'+fullsizeImg+'" />');
		newE1.find('#listingprofile-ed-832').html('<a href="'+listingUrl+'" target="_blank">'+"My Profile"+'</a>');
		newE1.find('#morelink-85359').html('<a href="'+listingUrl+'" target="_blank">'+"More >>"+'</a>');
		newE1.find('#starimage-ed-832').html('<img src="'+listingRate+'" width="60" height="13" alt="" border="0" />');
		newE1.find('#ext-ed-832').html('ext. '+listingextn);	
		newE1.find('#ph-ed-832').html(listingphno);
			
		$("#cell"+i).empty(); // This empties the current cells and appends the dom with the newly created elements when user switches to another category
		newE1.appendTo("#cell"+i);
		});	//close each listing	
	}//close success
	});//close ajax function
	
	var delay = function() 
	{
	//alert("refreshing "+ this.category);
	GetListings(category);
	}	

	timeout = setTimeout(delay, 30000);
}//close GetListings function
//The getfListings functions is to get dummy listings when server is down or if the listings returned is less than the required number of listings.
function getfListings() {
	$('#fragment-1').empty();
	$('#fragment-2-container').fadeIn("slow");
	$('#fragment-2-container').show();
	//SetImageBehaviors();
}
// The getCount function is to get the count of total listings in the xml
var jXML = {
    getCount: function(xml,nodes) {
        var response = {};
        for (var node in nodes) {
            response[node] = $(nodes[node],xml).length;
        }
        return response;
    }}

function SetImageBehaviors() {
		$('[category]').hover(SetHoverImage, RestoreImage);
		$('[category]').click(SetClickImage);
			if (selectedImage != null)
				$(selectedImage).unbind();	
		$('[category]').bind("click", (function() { // bind click event to the love tab
			//alert("love tab selected, cat is "+ $('#'+this.id).attr("category"));
			clearTimeout(timeout);   
			GetListings($('#'+this.id).attr("category"));
			SetTabDesc($('#'+this.id).attr("category"));
			return false;							
		}));
	}
		
function SetHoverImage() {
			this.src = 'http://i.keen.com/' + this.getAttribute('hoversrc');
			}
		
		function RestoreImage() {
			this.src = 'http://i.keen.com/' + this.getAttribute('offsrc');
		}
		
		function SetClickImage() {
			this.src = 'http://i.keen.com/' + this.getAttribute('selectedsrc');
			if (selectedImage != null)
				selectedImage.src = 'http://i.keen.com/' + selectedImage.getAttribute('offsrc');
			selectedImage = this;
			SetImageBehaviors();
		}
		
jQuery.preloadImages = function()
{
	for(var i = 0; i<arguments.length; i++)
	{
		jQuery("<img>").attr("src", arguments[i]);
	}
}
function SetTabDesc(category){
switch (category)
{
case "635":
  $("#tab-desc")[0].src = 'http://i.keen.com/' + $("#tab-desc")[0].getAttribute('lovesrc');
	break;
case "195":
  $("#tab-desc")[0].src = 'http://i.keen.com/' + $("#tab-desc")[0].getAttribute('psychicsrc');
break;
case "588":
  $("#tab-desc")[0].src = 'http://i.keen.com/' + $("#tab-desc")[0].getAttribute('tarotsrc');
break;
case "200":
  $("#tab-desc")[0].src = 'http://i.keen.com/' + $("#tab-desc")[0].getAttribute('spiritsrc');
break;
case "196":
  $("#tab-desc")[0].src = 'http://i.keen.com/' + $("#tab-desc")[0].getAttribute('astrosrc');
break;
}
}



