if (typeof SOAP == "undefined" || !SOAP) {
    var SOAP = {};
}

hostnm = document.location.protocol + '//' + document.location.host;
CDNRoot = '';
if (document.location.host.toLowerCase() == 'dev.soapnet.go.com') {
	CDNRoot = 'http://dev.media.soapnet.go.com/soapnet';
} else if (document.location.host.toLowerCase() == 'preview.soapnet.go.com') {
	CDNRoot = 'http://preview.media.soapnet.go.com/soapnet';
} else {
	CDNRoot = 'http://a.media.soapnet.go.com/media';
}
today = new Date();
ts = Date.parse(today.toString());

// used validation of many forms to see if a field is empty
function isBlank(val){
	if(val==null){return true;}
	for(var i=0;i<val.length;i++) {
		if ((val.charAt(i)!=' ')&&(val.charAt(i)!="\t")&&(val.charAt(i)!="\n")&&(val.charAt(i)!="\r")){
			return false;
		}
	}
	return true;
};


// generates random ids for the video player
var randid;
var isFirst = true;
function generateId() {		
	if (isFirst) {		
		isFirst = false;
		randid = Math.round(Math.random() * 1000);
	}
	 return randid;
}

//add events
Event.onReady(function() {

    var showsNavLink = $('showsNavLink');
    if (showsNavLink) {
        showsNavLink.observe('click', toggleShowListNav);
    };

    var extrasNavLink = $('extrasNavLink');
    if (extrasNavLink) {
        extrasNavLink.observe('click', toggleExtrasNav);
    };

});

function toggleShowListNav() {
	showList = $('showListWrapper');
	if (showList.getStyle('visibility') == 'hidden') {
		displayShowListNav();
	} else {
		hideShowListNav();
	}
}

function toggleExtrasNav() {
	extrasDiv = $('extrasWrapper');
	if (extrasDiv.getStyle('visibility') == 'hidden') {
		showExtrasNav();
	} else {
		hideExtrasNav();
	}
}

function displayShowListNav() {
	showsLink = $('showsNavLink');
	showList = $('showListWrapper');
	extrasDiv = $('extrasWrapper');
	if (extrasDiv.getStyle('visibility') == 'visible') {
		extrasDiv.setStyle({visibility: 'hidden'});
	}
	pos = Position.positionedOffset(showsLink);
	showList.setStyle({ left: (pos[0] - 83) + 'px' });
	showList.setStyle({ top: (pos[1] + 34) + 'px' });
	showList.setStyle({ visibility: 'visible' });
}

function hideShowListNav() {
	showList = $('showListWrapper');
	showList.setStyle({ visibility: 'hidden' });	
}

function showExtrasNav() {
	extrasLink = $('extrasNavLink');
	extrasDiv = $('extrasWrapper');
	showList = $('showListWrapper');
	if (showList.getStyle('visibility') == 'visible') {
		showList.setStyle({ visibility: 'hidden' });
	}
	pos = Position.positionedOffset(extrasLink);
	extrasLeft = pos[0] - 36;
	extrasTop = pos[1] + 34;
	extrasDiv.setStyle({ left: extrasLeft + 'px' });
	extrasDiv.setStyle({ top: extrasTop + 'px' });
	extrasDiv.setStyle({ visibility: 'visible' });
}

function hideExtrasNav() {
	extrasDiv = $('extrasWrapper');
	extrasDiv.setStyle({visibility: 'hidden'});
}

function writeFlash(flcontent) {	
	document.write(flcontent);	
}

function showPreroll(showAd, leaveBehind, adImage, adURL) {
	if (showAd) {
		$('adDiv240x30_video').setStyle({ display: 'block' });
		$('adDiv240x30_video').innerHTML = '<a href="' + adURL + '" target="_blank"><img src="' + adImage + '" /></a>'
	} 
	
	if (!showAd) {
		$('adDiv240x30_video').innerHTML = ''
		$('adDiv240x30_video').setStyle({ display: 'none' });		
	}
};

/* Video Analytics tracking Class */

if (typeof abcdm == "undefined" || !abcdm) {
    var abcdm = {};
}

abcdm.analytics = {

  omnTrackMedia: function(iAction, sEvent, sPlayer, sName, sAssetType) {

		/*	
		iAction: 0, On Start
			 1, On Auto Start
			 2, On Complete
			 3, On Ad Start
			 4, On Ad Complete
			 5, Interactions (full screen / fast forward / pause / etc)
		sEvent: Name of event, pause, play, ad name, etc
		sPlayer (optional): The name of the player being used, ex: "abcfd:2.3:full_ep"
		sName (optional): The name of the item as shown in the report, ex "7th_heaven_s2_e5"
		sAssetType (optional): vid (default), gam, aud, pod, dld.	
		*/
				
		s_omni.Media.trackVars="prop1,prop3,prop4,eVar2,events";

		switch(iAction)
		{			
			case 0:	
				var actionArray = sName.split(':');
				var vidArray = actionArray[0].split('/');
				var vidVertical = vidArray[0];
				var vidID = vidArray[1];
				var vidSegment = actionArray[1];
				//Asset Type (video) and ID (included with the video's vertical)
				s_omni.eVar2 = s_omni.prop3 = "vid:" + vidVertical + ":video_" + vidID;
				s_omni.events = "event1";
				s_omni.eVar13 = "ui-regular:" + vidSegment;
				s_omni.prop4 = "vid";
				s_omni.linkTrackVars="prop1,prop3,prop4,eVar2,eVar13,events";
				s_omni.linkTrackEvents = s_omni.events;
				s_omni.Media.trackEvents="event11";
				s_omni.tl(this, 'o', "VideoStart");
				s_omni.Media.open('video_' + vidID, '300', 'SOAPnet Video Player');
				s_omni.Media.play('video_' + vidID, 0);
				break;
				
			case 1:	
				var actionArray = sName.split(':');
				var vidArray = actionArray[0].split('/');
				var vidVertical = vidArray[0];
				var vidID = vidArray[1];
				var vidSegment = actionArray[1];
				//Asset Type (video) and ID (included with the video's vertical)
				s_omni.eVar2 = s_omni.prop3 = "vid:" + vidVertical + ":video_" + vidID;
				s_omni.events = "event1";
				s_omni.eVar13 = "autostart:" + vidSegment;
				s_omni.prop4 = "vid";
				s_omni.linkTrackVars="prop1,prop3,prop4,eVar2,eVar13,events";
				s_omni.linkTrackEvents = s_omni.events;
				s_omni.Media.trackEvents="event11";
				s_omni.tl(this, 'o', "VideoStart");
				s_omni.Media.open('video_' + vidID, '300', 'SOAPnet Video Player');
				s_omni.Media.play('video_' + vidID, 0);
				break;
	
			case 2:	
				var actionArray = sName.split(':');
				var vidArray = actionArray[0].split('/');
				var vidVertical = vidArray[0];
				var vidID = vidArray[1];				
				//Asset Type (video) and ID (included with the video's vertical)
				s_omni.eVar2 = s_omni.prop3 = "vid:" + vidVertical + ":video_" + vidID;
				s_omni.events = "event11";
				s_omni.prop4 = "vid";
				s_omni.linkTrackEvents = s_omni.events;
				s_omni.Media.trackEvents="event11";
				s_omni.Media.stop('video_' + vidID, '300');
				s_omni.Media.close('video_' + vidID);
				break;
	
			/* NOT TRACKING ADS OR EVENT CLICKS YET
			case 3:		
			case 4:	
			*/		
	
			case 5:	
				var vidAction = sEvent;
				var actionArray = sName.split(':');
				var vidArray = actionArray[0].split('/');	
				var vidVertical = vidArray[0];
				var vidID = vidArray[1];
				//Asset Type (video) and ID (included with the video's vertical)
				s_omni.eVar2 = s_omni.prop3 = "vid:" + vidVertical + ":video_" + vidID;
				s_omni.events = "event40";
				s_omni.products="vid;vid:" + vidAction + ";;;event40=1";
				s_omni.linkTrackVars="prop1,prop3,prop4,products,events";
				s_omni.linkTrackEvents = "event40"; 
				s_omni.tl(this, 'o', vidAction);
				break;
		}
		
	}
};

function writeActorCarousel(flcontent, showid, lc_flashVars) {
	document.write(flcontent);
	var actorcarousel = { movie:CDNRoot + "/swf/animation_module.swf", wmode:"transparent", width:"464", height:"278", majorversion:"9", build:"0", flashvars:"lc_randStr=" + lc_flashVars + "&dataPath=" + hostnm + "/soapnet/_lib/writeShowActorXML/path-showID_" + showid};
	UFO.create(actorcarousel, "flashActorCarousel");
}

// posting comments
function postComment(formName) {
	entry = $(formName).serialize(true);	
	if (isBlank(entry['userComment'])) {
		alert("Please enter a comment.");
	} else {
		$(formName).setStyle({ visibility: 'hidden' });
		$('postCommentButt').setStyle({ visibility: 'hidden' });
		s_omni.prop9 = "commentpost";
		s_omni.tl(this ,'o', 'Comment Posted');
		var url = '/soapnet/_lib/postComment';
		var pars = entry;
		document.forms[formName].reset();
		var myAjax = new Ajax.Updater(
				'commentsTop',
				url,
				{
				method: 'post',
				parameters: pars,
				onComplete: timeoutReloadPage
				});
	}	
}

function timeoutReloadPage() {
	setTimeout( "reloadPage()", 4000 );
}

function reloadPage() {
	window.location.reload();
}

// reporting comments
function reportComment(username, commentID, comment, contentTag) {
	rURL = hostnm + '/soapnet/_lib/reportComment?username=' + username + '&commentID=' + commentID + '&comment=' + comment + '&contentTag=' + contentTag;
	reportWin = window.open(rURL, 'reportWindow', 'width=400,height=100,titlebar=no,toolbar=no,status=no,scrollbars=no,menubar=no,location=no');	
}


// search results script
function resultsToggle(tabName) {
	resultDiv = tabName + "Results";
	divArray = $$('.resultList');  
  //toggle the divs containing the items to show the one clicked
  for (var i = 0; i < divArray.length; i++) {
		if (divArray[i].id == resultDiv) {
			divArray[i].setStyle({ display: 'block' });
		} else {
			divArray[i].setStyle({ display: 'none' });
    }
  }	
}

// reporting profiles
function reportUser(username) {
	rURL = hostnm + '/soapnet/_lib/reportUser?username=' + username;
	reportWin = window.open(rURL, 'reportWindow', 'width=400,height=150,titlebar=no,toolbar=no,status=no,scrollbars=no,menubar=no,location=no');	
}

//toggle viewing all favorites on profile pages
function toggleFaves(divtype) {
	if (divtype == 'all') {
		$('smallFaves').setStyle({ display: 'none' });
		$('allFaves').setStyle({ display: 'block' });
	}
	if (divtype == 'small') {
		$('allFaves').setStyle({ display: 'none' });
		$('smallFaves').setStyle({ display: 'block' });
	}
}

//uploading avatars
function pickAvatar(imgchoice) {
	avForm = $('updateAvatar');
	if (imgchoice != 'file') {		
	  avForm.mediaFile.value = '';
		avForm.defaultPathToPreviewValue.value = 'http://a.media.soapnet.go.com/media/images/profile/avatars/avatar_' + imgchoice + '.jpg';
		avForm.setStyle({ display: 'none' });
		$('waitDiv').setStyle({ display: 'block' });
		avForm.submit();
	} else {		
		if (isBlank(avForm.mediaFile.value)) {
			alert('Please select a file for upload.');
			avForm.mediaFile.focus();
		} else {
		  avForm.defaultPathToPreviewValue.value = '';
			avForm.setStyle({ display: 'none' });
			$('waitDiv').setStyle({ display: 'block' });
			avForm.submit();
		}		
	}
}

function openEmailThis(cid) {
	eURL = hostnm + '/soapnet/sendToFriend?contentID=' + cid;
	emailWin = window.open(eURL, 'emailWindow', 'width=400,height=550,titlebar=no,toolbar=no,status=no,scrollbars=no,menubar=no,location=no');
}

function refreshAd(adsize) {
	var adDiv;
	// we need to append categories and subcategories onto ad call urls for targeting.
	var catStr = '';
	var locStr = document.location.pathname.toString();
	var catLoc = locStr.indexOf('/category');
	if (catLoc != -1) {
		catStr = locStr.substr(catLoc)
	}
	if (adsize == '300') {
		adDiv = 'adDiv300x250';	
	}
	
	cacheTime = new Date();
	cacheBust = Date.parse(cacheTime.toString());
	var url = '/soapnet/_lib/adIframe/path-adsize_' + adsize + catStr + '?' + cacheBust;
	var myAjax = new Ajax.Updater(
			adDiv,
			url,
			{
			method: 'get',
			evalScripts: true
			});
}

function galleryHit(vertical, gallery_id, picture_num) {
	galNum = gallery_id.replace("gallery", "gallery_");
	s_omni.pageName="sop:" + vertical + ":" + galNum + ":" + picture_num;
	//Clear Previous Events, Event 3 is added automatically
	s_omni.events = ""
	s_omni.t();
	cacheTime = new Date();
	cacheBust = Date.parse(cacheTime.toString());
	// calling blank xml file for MM/Comscore counting
	var myAjax = new Ajax.Request('/soapnet/feeds/galleryHit?ts=' + cacheBust, {
		  method: 'get'
		});
}

function tabToggle(tabName, divToChangeClass, pageNameForOmni) {
	var tabDiv = tabName + "Tab";
	//Grab the class name to modify
	var activityName = $w($(tabDiv).className) //"actorsTab"
	activityName = activityName[0].gsub("Inactive", "");
	activityName = activityName.gsub("Active", "");
	//Grab all the tabs that aren't the one we want
	var arrTabSiblings = $(tabDiv).siblings();
	for (var i = 0; i < arrTabSiblings.length; i++) {
		arrTabSiblings[i].removeClassName(activityName + "Active")
		arrTabSiblings[i].addClassName(activityName + "Inactive")
	}	
	$(tabDiv).addClassName(activityName + "Active")
	$(tabDiv).removeClassName(activityName + "Inactive")
	
	//Grab the children of the div container for hiding and showing
	var divArray = $(divToChangeClass).childElements();
	var itemDiv = tabName + "Items"
	//toggle the divs containing the items to show the one clicked
  	for (var i = 0; i < divArray.length; i++) {
		if (divArray[i].id == itemDiv) {
			divArray[i].show();
		} else {
			divArray[i].hide();
		}
	}
	//record a page hit for the call
	if (pageNameForOmni != null && pageNameForOmni != ""){
		var intCutOff = s_omni.pageName.indexOf("_")
		if(intCutOff > 0){
			s_omni.pageName = s_omni.pageName.slice(0, intCutOff)	
		}
		s_omni.pageName = s_omni.pageName + "_" +pageNameForOmni
		//Clear Previous Events, Event 3 is added automatically
		s_omni.events = ""
		s_omni.t();
	}
}