/* ==============================
	SOCIAL LABS V1.1
	Last updated: Jan 18, 2012

	regular item
		GS:TWMT:PP12:PP05:
	cross-sell item
		GS:TWMT:PP12:PP05::S:PP03
	cross-sell with iprofile:
		GS:TWMT:PP12:PP05:BTEST:S:PP03
	
	language options:
		en-US
		fr-CA
		es-LA

============================== */

/* ==============================
	variables (custom code)
============================== */
	// Set client name
	var slClient = document.title.split(" | ")[0];
	
	// If true, add group desc name to event in FB (ie: Stanford Football: vs Nebraska)
	var slShowGroupDesc = false;
	
	// If true, always use the default/client logo for all events (usually false for college)
	var slUseDefaultLogo = false;
	
	// If true, we create a facebook event (not instant, every half hour or so).
	var slCreateEvent = true;
	
	// template on displayeventinfo
	var slTemplate = "chocolate";
	
	// regional settings
	var slLanguage = "en-US";
	var slTimezone = "America/Los_Angeles";
	var slCurrency = "USD";
	
/* ==============================
	variables (do not change)
============================== */
	var slSize = new Object;
	slSize.mint = "size_257x86";
	slSize.chocolate = "size_600x79";
	
	var slLayout = slSize.mint;
	if(slTemplate=="chocolate") {
		slLayout = slSize.chocolate;
	}

	var slDesc = ""; // for testing
	
/* ==============================
	functions
============================== */
// You MUST be using printLogo functions
	function socialLabs_getLogo(name, group) {
		if(typeof(printLogo)!="function") return '';
		
		if(slUseDefaultLogo) {
			name = '';
			group = 'facebook';
		}
		var imgSrc = printLogo(name, group);
		imgSrc = imgSrc.slice(imgSrc.indexOf("\"")+1, imgSrc.length);
		imgSrc = imgSrc.slice(0, imgSrc.indexOf("\""));
		return(location.host + imgSrc);
	}

// strip html coding from strings
	function socialLabs_stripHTML(string) {
		string = string.replace(/<br>/ig, " ");
		return string.replace(/(<([^>]+)>)/ig, "");
	}

// returns 24-hour format time (ie: 23:00)
	function socialLabs_timeFormat(time) {
		if(time == "") return "";
		
		var h = time.getHours();
		var m = time.getMinutes();
		return (twoForm(h) +":"+ twoForm(m));
	}

// returns date format "yyyy-mm-dd" 
	function socialLabs_dateFormat(time) {
		if(typeof(time)=="undefined" || time == "") return "";
		
		var formatedTime = time.getFullYear() + '-' + (time.getMonth()+1) + '-' + time.getDate();
		return formatedTime;
	}

// get group desc
	function socialLabs_getGroupDesc(groupCode) {
		if(
			!slShowGroupDesc ||
			typeof(fullGroupList)=="undefined" ||
			groupCode=="" ||
			groupCode.indexOf("RSF:")==0
		) {
			return '';
		}
		
		for(var a=0; a<fullGroupList.length; a++) {
			if(fullGroupList[a].code==groupCode) {
				return fullGroupList[a].description;
			}
		}
		return '';
	}
	
	function socialLabs_getEventID(eo) {
		// set event ID without iprofile portion (event code sample: GS:HUSKIES:F09:F07:ADAM)
		var eventID = eo.eventCode
		// remove appended chars from cross-sell events
		if(eo.groupCode.indexOf("RSF:")==0) eventID = eventID.slice(0, eventID.lastIndexOf(":S:"));
		// remove iprofile chars
		eventID = eventID.slice(0, eventID.lastIndexOf(":")+1);
		
		return eventID;
	}
	
	function socialLabs_getEventURL(eID) {
		// set event url
		var eURL = "http://" + location.host + "/cgi-bin/ncommerce3/SEGetEventInfo?ticketCode="+ eID +"&linkID="+ linkID;
		// for student events, links go straight to student login page
		if(common.isStudent) {
			eURL = "http://" + location.host + "/cgi-bin/ncommerce3/EVExecMacro?linkID="+ linkID +"&evm=myac&msgCode=32000&shopperContext=ST";
		}
		
		return eURL;
	}

// create rondavu mo
	function socialLabs_createEvent(eventObj, facID, facName) {
		if(eventObj.eventCode.indexOf("PR:")==0) return false; // disable for events in promo
		
		var eventID = socialLabs_getEventID(eventObj);
		var groupDesc = socialLabs_getGroupDesc(eventObj.groupCode);
		var eventURL = socialLabs_getEventURL(eventID);
		var eventLogoFile = socialLabs_getLogo(eventObj.description, eventObj.groupCode);
		// tester: document.write(eventLogoFile);
		
		// set unique ID and event desc for sociable labs
		var rondavuID = linkID+ ":" +eventID;
		var rondavuDesc = socialLabs_stripHTML(eventObj.description); // custom code (description only)
		slDesc = rondavuDesc; // for testing purposes
		
		var slEventData = {
			id: [{
				id: rondavuID,
				type: "event"
			}],
			name: [{
				name: rondavuDesc,
				language: slLanguage,
				primary: true
			}],
			primary_tag: "event_" + eventID,
			tags: ["client_"+linkID, "venue_"+facID, "group_"+eventObj.groupCode, "event"],
			url: {
				detail: eventURL,
				picture: {
					primary: (eventLogoFile!='' ? 'http://'+eventLogoFile : ''),
					primary_secure: (eventLogoFile!='' ? 'https://'+eventLogoFile : '')
				}
			},
			product_date: [{
				start: {
					date: socialLabs_dateFormat(eventObj.eventTime), // date format is yyyy-mm-dd
					time: socialLabs_timeFormat(eventObj.eventTime), // 24 hour clock
					timezone: slTimezone
				}
			}]
		};
		
		if(facID!="" && facName!="") {
			slEventData.location = {
				id: facID,
				name: facName
			};
		}
		
		if(slCreateEvent) {
			slEventData.facebook = {
				// event_id: "35044734096", // id of the facebook event if it already exists. 
				create_event: true
			};
		}
		
		return slEventData;
	}

// return seat string
	function socialLabs_getSeatString(li) {
		if(
			li.level=="" ||
			li.section=="" ||
			li.row=="" ||
			li.seatFrom==""
		) return '';
		
		var seatString = [];
		if(li.level!="") seatString.push(li.level);
		if(li.section!="") seatString.push("Section " + li.section);
		if(li.row!="") seatString.push("Row " + li.row);
		if(li.seatFrom!="") seatString.push("Seat " + li.seatFrom + (li.seatTo!=li.seatFrom ? '-'+li.seatTo : ''));
		return seatString.join(", ");
	}
	
// get seat data for orderok
	function socialLabs_createSLSeat(itemObj) {
		var itemID = "";
		var itemCost = 0;
		var itemSeats = new Array();
		
		if(itemObj.type=="SE") {
			if(itemObj.eventInfo.eventCode.indexOf("PR:")==0) return false; // disable for events in promo
			
			itemID = socialLabs_getEventID(itemObj.eventInfo);
			for(var a=0; a<itemObj.reservationList.length; a++) {
				for(var b=0; b<itemObj.reservationList[a].lineItemList.length; b++) {
					itemCost += Number(itemObj.reservationList[a].lineItemList[b].cost);
					for(var c=0; c<itemObj.reservationList[a].lineItemList[b].seatList.length; c++) {
						var t_seats = socialLabs_getSeatString(itemObj.reservationList[a].lineItemList[b].seatList[c]);
						if(t_seats!='') itemSeats.push( t_seats );
					}
				}
			}
		} else if(itemObj.type=="ST") {
			if(itemObj.ticketInfo.eventCode.indexOf("PR:")==0) return false; // disable for events in promo
			
			itemID = socialLabs_getEventID(itemObj.ticketInfo);
			for(var a=0; a<itemObj.priceTypeQtyList.length; a++) {
				itemCost += Number(itemObj.priceTypeQtyList[a].cost);
			}
			if(itemObj.ticketInfo.allocEnabled=="1" && itemObj.ticketInfo.sameSeats) {
				for(var a=0; a<itemObj.eventList[0].seatList.length; a++) {
					var t_seats = socialLabs_getSeatString(itemObj.eventList[0].seatList[a]);
					if(t_seats!='') itemSeats.push( t_seats );
				}
			}
		}
		if(itemID==false) return false;
		
		var theItem = {
			id: {
				id: linkID + ":" + itemID,
				type:"event"
			},
			price: {
				actual_paid: amountFormat(itemCost),
				currency:"USD"
			},
			seat: itemSeats.join("; ")
		};
		return theItem;
	}

/* ==============================
	action
============================== *
/* DISPLAYEVENTINFO */
	if(
		typeof(common)!="undefined" &&
		typeof(common.pageName)!="undefined" &&
		common.pageName=="DisplayEventInfo.html" &&
		typeof(eventInfo)!="undefined" &&
		eventInfo.eventCode.indexOf("PR:")!=0
	) {
		window.RondavuData = {
			config: {
				version: "1.1"
			},
			page: {
				disable: false,
				is_homepage: false,
				language: slLanguage,
				primary: true,
				tags: ["client_"+linkID, "layout_top", slLayout]
			},
			primary_mo: socialLabs_createEvent(eventInfo, facility.number, facility.description)
		};
		
		var slScriptUrl = 'static.rondavu.com/C/Paciolan_PROD/rondavu.js';
		if(location.host=="ev4.evenue.net") {
			slScriptUrl = 'qa.rondavu.com/C/Paciolan_Dev/rondavu.js';
		}
		(function(){
			var s = document.createElement('script');
			s.async = true;
			s.src = ('https:' == window.location.protocol ? 'https://s3.amazonaws.com/' : 'http://')
			+
			slScriptUrl;
			document.getElementsByTagName('head')[0].appendChild(s);
		})();
	}

/* ORDEROK */
	if(
		typeof(order)!="undefined" &&
		typeof(order.number)!="undefined" &&
		typeof(cartCharges)!="undefined"
	) {
		var slEventList = [];
		
		for(var i=0; i<itemList_.length; i++){
			if(itemList_[i].type=="SE") {
				slEventList.push( socialLabs_createEvent(itemList_[i].eventInfo, '', itemList_[i].eventInfo.facility) );
			} else if(itemList_[i].type=="ST") {
				slEventList.push( socialLabs_createEvent(itemList_[i].ticketInfo, '', itemList_[i].ticketInfo.facility) );
			}
		}
		
		if(slEventList.length>0) {
			window.RondavuData = {
				config: {
					version: "1.2" // <-- new version
				},
				page: {
					disable: false,
					is_homepage: false,
					language: slLanguage,
					primary: true,
					tags: ["client_"+linkID, "layout_top", slSize.chocolate]
				},
				primary_mo: slEventList[0]
			};
			
			var slScriptUrl = 'static.rondavu.com/C/Paciolan_PROD/rondavu.js';
			if(location.host=="ev4.evenue.net") {
				slScriptUrl = 'qa.rondavu.com/C/Paciolan_Dev/rondavu.js';
			}
			(function(){
				var s = document.createElement('script');
				s.async = true;
				s.src = ('https:' == window.location.protocol ? 'https://s3.amazonaws.com/' : 'http://')
				+
				slScriptUrl;
				document.getElementsByTagName('head')[0].appendChild(s);
			})();
		}
	}
