
/* ********* Tour Dates */
/* Sets the maximum number of tour dates to be shown with in the module.*/
var tourDateMaxItems = 3; 

/*This is the View All Link of the Tour Date module.*/
var tdViewAllLink="/tour-dates/"; 

/* Text that is displayed when no shows are available */
var tdNoShowsText="No shows scheduled. Check again later";

/* Value for the artist name */
var tdAppearingWithLabel = "Serena Ryder with ";

var monthNames = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun",
			"Jul", "Aug", "Sep", "Oct", "Nov", "Dec");


renderRSSModule = function(data,format, div) {
	
	if (div.attr("id") == "rss_content-382956") {
	
		div.empty();
		if ($("body").is("#home")) {
			/* This is tours module in the home page. In this module a maximum of 
		 * above set tour dates are displayed.
		 * The tdAllFlag is set to false, so that it displays only the set number of tour dates in the module.
		 */
			tdAllFlag = false;
		}
		else {
			/* This is tours module in the concerts landing page module. In this module 
		 * all the tour dates are displayed.The tdAllFlag is set to true, so that
		 * it displays all the tour dates.
		 */
			tdAllFlag = true;
		}
		tourDatesWrapper = tourDatesBuildStructure(tdAllFlag, data);
		div.append(tourDatesWrapper);
	} else if (div.attr("id") == "rss_content-399814") { 
			renderRSSModuleEOS(data, format, div);
			$("#rss_content-399814 h3 a").text("Follow [Artist Name]");
			$("#rss_content-399814 a").attr("target", "_new");
	}else if (div.attr("id") == "rss_content-422247") {
			renderRSSModuleEOS(data, format, div);			
		/* Empty the container div */
		//div.empty();
	
/*		fanClubBuilder(data);	*/
		
	}
	else {
		/* Default RSS Ingester module functionality */
		//if this isn't the cellstream RSS module then use the EOS default copied and pasted below
		renderRSSModuleEOS(data, format, div);
		
	}

}


renderRSSModuleEOS=function(c,a,b){jQuery.getFeed({xml:c,success:function(g){jQuery(b).append('<h3><a href="'+g.link+'">'+g.title+"</a></h3>");var e="";for(var d=0;d<g.items.length&&d<5;d++){var f=g.items[d];e+="<h4>";if(f.link.length>0){e+='<a href="'+f.link+'">'+f.title+"</a>"}else{e+=f.title}e+="</h4>";e+='<div class="updated">'+f.updated+"</div>";e+="<div>"+f.description+"</div>";if(f.image){e+='<div class="image"><img src="'+f.image+'"></div>'}}jQuery(b).append(e)}})};


function createTourDateUnit(title, venueName,venueCountry,venueCity,venueLocation,buyLink, appearingWith,dateFirstUnit){

	tourDatesUnit = document.createElement('div');
	$(tourDatesUnit).attr("class","tourDatesUnit");
	
	if(!(dateFirstUnit%2)){
		$(tourDatesUnit).addClass("oddEntry");
	}else 
		$(tourDatesUnit).addClass("evenEntry");
	
	//create tour date unit
	tdDateUnit = document.createElement('div');
	$(tdDateUnit).attr("class","tdDateField");
	var day;
	var dateInTitle = "" + title.match(/\d{1,2}\/\d{1,2}/);	
	var unFormatDay ="" +title.match(/- \w{3}/);	
	if(unFormatDay != null){
		day=unFormatDay.substring(2,5);
	}	
	var dateString = "";
	var monthString = "";
	if(dateInTitle != null ) {
		var dateArray = dateInTitle.split("\/");
		monthNumString = dateArray[0];		
		monthString = monthNames[dateArray[0]-1];
		dateString = dateArray[1];		
	}
	$(tdDateUnit).append('<div class="tdDay">'+ day +'</div><div class="tdDM">'+ dateInTitle +'</div>');
	
	//$(tdDateUnit).append('<div class="tdMonth">'+ monthString +'</div><div class="tdDD">'+ dateString +'</div>');
	
	//create tourVenueBuyWrapper	
	tourVenueBuyWrapper = document.createElement('div');
	$(tourVenueBuyWrapper).attr("class","tourVenueBuyWrapper");

	//create tour venue unit
	tdVenueField = document.createElement('div');
	$(tdVenueField).attr("class","tdVenueField");
	$(tdVenueField).append('<div class="tdVenue">' + $.trim(venueLocation) + ', ' + venueName + '</div>');
	
	if (appearingWith.replace(/\s/g,"") != ""){
		$(tdVenueField).append('<div class="tdArtistName">' + tdAppearingWithLabel + appearingWith +'</div>');
	}
	$(tourVenueBuyWrapper).append(tdVenueField);	
	
	//create purchase tickets unit
	if (buyLink != ""){
		tdBuyLinksField = document.createElement('div');
		$(tdBuyLinksField).attr("class","tdBuyLinksField");
		$(tdBuyLinksField).append('<div class="tdBuyLinks"><a href="'+ buyLink +'" target="_blank">Buy Tickets</a></div>');
		$(tourVenueBuyWrapper).append(tdBuyLinksField);			
		
		//Values for iggli variables
		var newDate = new Date();
		var yearString = newDate.getFullYear();
		invite_title = title;
		invite_location = venueName + $.trim(venueLocation);
		invite_date = monthNumString + '/' + dateString + '/' +yearString;
		
		onclickString = "populateInviteData('"+ invite_title + "','"+invite_location+"','"+invite_date +"');";
		$(tourVenueBuyWrapper).append('<div class="iggli-invite-button"><a class="iggli-button" onclick="' + onclickString + '">Invite Friends</a></div>');		
		$(tourVenueBuyWrapper).append('<div class="clear"></div>');
	}
	
	$(tourDatesUnit).append(tdDateUnit);
	$(tourDatesUnit).append(tourVenueBuyWrapper);
	$(tourDatesUnit).append('<div class="clear"></div>');
	return tourDatesUnit;
}

function tourDatesBuildStructure(tdAllFlag,data){
	tourDateCount = 0;
	var dateFirstUnit=0;
	var title;
	
	//Load into XML DOM
	xmlDom = getXMLDOM(data);
	tourDatesWrapper = document.createElement('div');
	$(tourDatesWrapper).attr("class","tourDatesWrapper");
	totalTourItems = ($(xmlDom).find('item').length);
	tourDateLimit = tdAllFlag ? totalTourItems : tourDateMaxItems;
	if ($(xmlDom).find('item').length <= 0){
		$(tourDatesWrapper).append('<span class="tdErrorText">'+ tdNoShowsText + '</span>');
	}else {
		if(!tdAllFlag && totalTourItems > tourDateLimit) {
			$(tourDatesWrapper).append('<div class="tdViewAll header"><a href="' + tdViewAllLink + '">VIEW MORE</a></div');
		}
	}

	$(xmlDom).find('item').each(function(){
		title = $(this).children("title").text();
		venueName = $(this).children("venueName").text();
		venueCountry = $(this).children("venueCountry").text();
		venueCity = $(this).children("venueCity").text();
		venueLocation = $(this).children("venueLocation").text();
		buyLink = $(this).children("buylink").text();
		appearingWith = $(this).children("appearingWith").text();
		tourDatesUnit = createTourDateUnit(title, venueName, venueCountry,venueCity,venueLocation,buyLink,appearingWith,dateFirstUnit);
		$(tourDatesWrapper).append(tourDatesUnit);
		tourDateCount++;
		dateFirstUnit++;
		if (tourDateCount == tourDateLimit){
			return false;
		}
	})

	if (!($(xmlDom).find('item').length <= 0)) {
		if(!tdAllFlag && totalTourItems > tourDateLimit) {
			$(tourDatesWrapper).append('<div class="tdViewAll"><a href="' + tdViewAllLink + '">VIEW MORE</a></div');
		}
	}
	return tourDatesWrapper;
}
function getXMLDOM(xml){
    if( window.ActiveXObject && window.GetObject ) {
        var dom = new ActiveXObject( 'Microsoft.XMLDOM' );
        dom.loadXML( xml );
        return dom;
    }
    if( window.DOMParser )
        return new DOMParser().parseFromString( xml, 'text/xml' );
    throw new Error( 'No XML parser available' );
}

/*
function fanClubBuilder(data){	
				
		

		xmlDom = getXMLDOM(data);


		totalItems = ($(xmlDom).find('item').length);

		
		$(xmlDom).find('item').each(function(){
			author= $(this).children("author").text();
			alert(author);
			});
		
		$('item', xmlDom).each(function(){
			author = $(this).children("author").text();
			

		});
			
}*/	


function writeToConsole(obj){
	if(window.console && window.console.log){
		switch(typeof obj){
			case 'string': case 'integer':
				window.console.log('Eos > '+obj); break;
			default:
				window.console.log(obj); break;
		}
	}
};
