﻿//v1.0
// Porned Javascript Tools
// Copyright 2009 Love Taco Inc.  All rights reserved.
// http://www.porned.com/

var isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;

var maxVideoIndexInPlaylist = 0;
var currentVideoIndexInPlaylist = 0;
var currentPlaylistGuid = "";

// Make a call into the video player to get the total time the video was watched
function SaveVideoStatistics() {
	// Get stats from video player
	// dynamically create a div layer we will use to submit the stat
	try {
		var videoObj = null;
		videoObj = document.getElementById('VideoPlayer');
		if (videoObj != null) {
			var lengthSeconds = videoObj.GetTimeWatched();
			var videoid = videoObj.GetVideoId();
			var statname = "videolengthwatched";
			AjaxPost('http://www.porned.com/api/AddStat.aspx', 'name=' + escape(statname) + '&value=' + escape(lengthSeconds) + '&videoId=' + escape(videoid));
		}
	} catch (err) {
		// simply ignore any errors for now
	}
}

function AjaxPost(strURL,strData) {
	var xmlHttpReq = false;
	var self = this;
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	self.xmlHttpReq.open('POST', strURL, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	self.xmlHttpReq.onreadystatechange = function() {
		if (self.xmlHttpReq.readyState == 4) {
			//updatepage(self.xmlHttpReq.responseText);
		}
	}
	self.xmlHttpReq.send(strData);
}



// Show icon indicating user was paid for the video
function PayoutSuccessful() {
	var divObj = null;
	divObj = document.getElementById('payoutIcon');
	if (divObj != null) {
		divObj.style.visibility = 'visible';
	}
	// Update the RaysManager flash with the payout value
	var raysObj = null;
	raysObj = document.getElementById('RaysManager');
	if (raysObj != null) {
		raysObj.UpdateRaysPayout("0.35");
	}
}

// Popup for Free Rays info
function PopupFreeRays() {
	window.open("/freerays.aspx", "FreeRays", "width=984,height=360,status=0,toolbar=0,menubar=0,scrollbars=0,resizable=0");
}

// For playlist functionality, if a playlist is being run, a call to NextVideo will begin the next video.
function NextVideo() {
	// if no playlist exists (max video index 0), or its the last item in the playlist then ignore the request.
	if (currentVideoIndexInPlaylist < maxVideoIndexInPlaylist) {
		location.href = "/playlist/" + currentPlaylistGuid + "/" + (currentVideoIndexInPlaylist + 1);
	} else {
		// last item, or no playlist, ignore NextVideo request.
	}
}

function TogglePassport() {
	var divObj = null;
	divObj=document.getElementById('passportInfo');
	if (divObj != null) {
		if (divObj.style.visibility == 'visible') {
			divObj.style.visibility = 'hidden';
			divObj.style.position = 'absolute';
		} else {
			divObj.style.visibility = 'visible';
			divObj.style.position = 'relative';
		}
	}
}

function SetRating(rating,txt,rateTextId) {
	var ratingval = parseInt(rating);
	for (var i = 1; i <= 10; i+=2) {
		var divObject = null;
		if (document.getElementById) {
			divObject = document.getElementById("rate_" + i);
			if (divObject != null) {
				if (ratingval > i) {
					divObject.className = "FullStar";
				}
				else if (ratingval > i - 1) {
					divObject.className = "HalfStar";
				}
				else {
					divObject.className = "EmptyStar";
				}
			}
		}
	}
	var divText = null;
	if (document.getElementById) {
		divText = document.getElementById(rateTextId);
		if (divText != null && (txt == null || txt.length == 0)) {
			switch (ratingval) {
				case 0:
				case 1:
				case 2:
					divText.innerHTML = "Lame";
					break;
				case 3:
				case 4:
					divText.innerHTML = "Seen better";
					break;
				case 5:
				case 6:
					divText.innerHTML = "Ok";
					break;
				case 7:
				case 8:
					divText.innerHTML = "Good";
					break;
				case 9:
				case 10:
					divText.innerHTML = "Awesome!";
					break;
				default:
					divText.innerHTML = "Error";
			}
		} else if (divText != null && txt != null)
			divText.innerHTML = txt;
	}
	
}

function ChangeClass(objid, classname) {
	var divObject = null;
	if (document.getElementById) {
		divObject = document.getElementById(objid);
	}
	if(divObject != null)
		divObject.className = classname;
}

function showUserMenu() {
	var divObject = null;
	if (document.getElementById) {
		divObject = document.getElementById("usermenu");
	}
	if (divObject != null)
		divObject.style.visibility = 'visible';
}
function hideUserMenu() {
	var divObject = null;
	if (document.getElementById) {
		divObject = document.getElementById("usermenu");
	}
	if (divObject != null)
		divObject.style.visibility = 'hidden';
}
function toggleUserMenu() {
	var divObject = null;
	if (document.getElementById) {
		divObject = document.getElementById("usermenu");
	}
	if (divObject != null) {
		if (divObject.style.visibility == 'hidden')
			divObject.style.visibility = 'visible';
		else
			divObject.style.visibility = 'hidden';
	}
}

function ShowEmbedCode(divName, left, top) {
	var divObject = null;
	if(document.getElementById) {
		divObject = document.getElementById(divName);
	}
	if (divObject != null) {
		if (divObject.style.visibility == 'hidden')
			divObject.style.visibility = 'visible';
		else
			divObject.style.visibility = 'hidden';
		divObject.style.left = left;
		divObject.style.top = top;
	}
	return false;
}

// add a javascript function to the onLoad
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}
// add a javascript function to the onUnload
function addUnloadEvent(func) {
	var oldonunload = window.onunload;
	if (typeof window.onunload != 'function') {
		window.onunload = func;
	} else {
		window.onunload = function() {
			oldonunload();
			func();
		}
	}
}

function CreateBookmark() {
	var title = "Porned.com";
	var url = "http://www.porned.com/b";
	if (window.sidebar) {
		// Mozilla Firefox bookmark
		window.sidebar.addPanel(title, url, "");
	} else if (window.external) {
		// IE Favorite
		window.external.AddFavorite(url, title);
	} else if (window.opera && window.print) {
		// Opera Hotlist
		var elem = document.createElement('a');
		elem.setAttribute('href', url);
		elem.setAttribute('title', title);
		elem.setAttribute('rel', 'sidebar');
		elem.click();
	} else {
		alert("Your browser does not support automatic Bookmarking.\nPlease manually bookmark the page using your web browser.");
	}
}
