
/* Sets the maximum number of tour dates to be shown with in the module.*/
var tourDateMaxItems = 5; 

/*This is the View All Link of the Tour Date module.*/
var tdViewAllLink="/events/"; 

/* Text that is displayed when no shows are available */
var tdNoShowsText="There are no upcoming events scheduled at this time. Please check back soon.";


var monthNames = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun",
			"Jul", "Aug", "Sep", "Oct", "Nov", "Dec");


/* Twitter text */
var followTwitterText = "Follow [Band Name] on Twitter";

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-413300") {		
		if ($("#cellstream").length > 0	){
				/* Cellstream details page: Div id of Cellstream Details section */
				div.empty();
				// ciscoeos.com?someparam=name&otherparam=8&id=6
				var selectedID = $.urlParam('id');		
				var isInputParam=false;			
				
				
				/* If the param - id is not null */
				if (selectedID != 0) {			
					isInputParam=true;	
					csDetailsWrapper=csDetailsBuildStructure(data,selectedID,isInputParam);
					div.append(csDetailsWrapper);		
				}		
				else {				
					/* Param id is blank */
					isInputParam=false;	
					csDetailsWrapper=csDetailsBuildStructure(data,selectedID,isInputParam);
					div.append(csDetailsWrapper);
				}			
		}
		else{
			/* Cell stream rss module */
			div.empty();
			cellStreamWrapper=cellstreamBuildStructureBlastmob(data);
			div.append(cellStreamWrapper);
			cellStreamOptions.pageNumber = 0;
			registerPreviousNextEvents();
			/* Setting the previous and next buttons */
			/* Display the first page */
			showCSPage(0);
			//registerHover(0);
		}	
	}
	else if (div.attr("id") == "rss_content-399814") { 
			renderRSSModuleEOS(data, format, div);
			titleDiv = $("#rss_content-399814 h3");
			$("#rss_content-399814").append($("#rss_content-399814 h3"));
			$("#rss_content-399814 h3 a").text(followTwitterText);
			$("#rss_content-399814 a").attr("target", "_new");
	}  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==0)){
		$(tourDatesUnit).addClass("first");
	}
	if(!(dateFirstUnit%2)){
		$(tourDatesUnit).addClass("oddEntry");
	}	
	
	//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("\/");		
		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">'+ 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);
		$(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;
	
	//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 ($(xmlDom).find('item').length == 1 && ($(xmlDom).find('item').children("title").text()=="Sorry, no tour events found for seanpaul. ")) {
			$(tourDatesWrapper).append('<span class="tdErrorText">'+ tdNoShowsText + '</span>');
	}
	else {
		if (!tdAllFlag && totalTourItems > tourDateLimit) {
			$(tourDatesWrapper).append('<div class="tdViewAll header"><a href="' + tdViewAllLink + '">View All</a></div');
		}
	}
	if (!($(xmlDom).find('item').length == 1 && ($(xmlDom).find('item').children("title").text()=="Sorry, no tour events found for seanpaul. "))){
		$(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 || ($(xmlDom).find('item').length == 1 && ($(xmlDom).find('item').children("title").text()=="Sorry, no tour events found for seanpaul. ")) )) {
		if(!tdAllFlag && totalTourItems > tourDateLimit) {
			$(tourDatesWrapper).append('<div class="tdViewAll"><a href="' + tdViewAllLink + '">View All</a></div');
		}
	}
	return tourDatesWrapper;
}


/* This function is used to build CellStream- Details module */
function csDetailsBuildStructure(data,selectedID,isInputParam){

	if (isInputParam == true) {
		var isPhotoAvl=false;	

		data = data.replace(/image>/g, 'image-url>');
		xmlDom = getXMLDOM(data);			
		// Search for post id in the feed and adding up the components if present
		$(xmlDom).find("post").filter("[id^='" + selectedID + "']").each(function(){
			isPhotoAvl = true;				
			csDetailsWrapper = document.createElement('div');
			$(csDetailsWrapper).attr("id", "csPhotosViewWrapper");
			cellStreamElement = document.createElement('div');
			$(cellStreamElement).attr("id", "cellStreamPhotosViewer");
			csPage = csCount % cellStreamOptions.pageSize;
			
			if (csPage == 0) {
				csCurrentPageElement = document.createElement('div');
				$(csCurrentPageElement).attr("id", "csPage" + csCount / cellStreamOptions.pageSize);
				$(csCurrentPageElement).attr("class", "csPage");
			}						
			author = $(this).children("author").text();
			authorFirstName = $(this).children("authorFirstName").text();
			date = $(this).children("date").text();
			text = $(this).children("text").text();
			image = $(this).children("image-url").text();

			video = $(this).children("video");			
			/* Generate the cell element using the data and add to Cellstream Slider */
			nodeHtml = getSelectedPhoto(author, authorFirstName, date, text, image, video, csCount, csPage);
			$(csCurrentPageElement).append(nodeHtml);
			$(cellStreamElement).append(csCurrentPageElement)
			csCount++;			
		});
		
		/* If id is not available or Junk value */
		if (isPhotoAvl == false) {
			csDetailsWrapper = document.createElement('div');
			$(csDetailsWrapper).attr("id", "csPhotosNotAvl");
			cellStreamElement = document.createElement('div');
			$(cellStreamElement).attr("id", "cellStreamPhotosNotAvl");			
			$(cellStreamElement).append('<div class="noImageText">Image not Found</div>');			
		}		
		
		$(csDetailsWrapper).append(cellStreamElement);		
		
	}else{
		/* If id is Blank */
		//Image Not found	
		csDetailsWrapper = document.createElement('div');
		$(csDetailsWrapper).attr("id","csPhotosNotAvl");
		cellStreamElement = document.createElement('div');
		$(cellStreamElement).attr("id","cellStreamPhotosNotAvl");
		csCurrentPageElement = document.createElement('div');							
							
		/* Add the text content */
		$(csCurrentPageElement).append('<div class="noImageText">Image not Found</div>');								
		$(cellStreamElement).append(csCurrentPageElement);	
		$(csDetailsWrapper).append(cellStreamElement);					
	}
	
	return csDetailsWrapper;							
}



function getSelectedPhoto(author, authorFirstName, date, text, image, video, i, csPage){

	/* Create the Cell */
	var cellElement = document.createElement('div');
	
	/* Create the Cell content */
	var contentElement = document.createElement('div');
	$(contentElement).attr("class","csPVContent");
	/* Add the image if available */
	if (image != ""){
		image = getNetopsImageUrl(image,'600x450');
		$(contentElement).append('<div class="image"><img src="' + image + '"/></div>');				
	}
	
	/* Add the text content */
	$(contentElement).append('<div class="text">'+text+'</div>');
	
	/* Create the Wrapper */
	var wrapperElement = document.createElement('div');
	$(wrapperElement).attr("class","csWrapper");	
	dateString = simpleDate(date);
	$(wrapperElement).append('<div class="timestamp">' + dateString + '</div>');
	$(wrapperElement).append('<div class="user">' + author + '</div>');
	$(wrapperElement).append('<div class="clear"></div>');
	
	/* Adding all of them to the cell element */
	$(cellElement).attr("id", "csCell" + i);
	$(cellElement).attr("class", "csCell");
	$(cellElement).addClass("csCell" + csPage);
	$(cellElement).append(contentElement);
	$(cellElement).append(wrapperElement);
	
	return cellElement;	
}





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;
		}
	}
};

$.urlParam = function(name){
	var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
	if (results == null) {
		return 0;
	}
	else {
		return results[1];
	}
}


