/* Ajax navigation in pornstar, studio and site views*/
function goToPage(type, sort, pageOffset, category, scroll) {
	if ((category == undefined) || (category == "")) {
		category = "";
	}
	if (scroll == undefined) {
		scroll = false;
	}
	
	var view = "thumbs";
	if ($('view') != null) {
		view = $('view').value;
	}
	
	ajaxRequest.callUpdate('browse','ajaxContentId',{type:type, sort:sort, pageOffset:pageOffset, category:category, view:view}, 'get', true);
	
	if (scroll == true) {
		Effect.ScrollTo('ajaxContentId');
	}
}


/* Start pagination */
var troisPre = false;
var troisPost = false;
var troisPoint = false;

var pageBlockExecTime = new Array();
	
swapPageBlock = function(id,action){
	switch(id){
	
		case "pre":
			var box = "ctnPgnRight";
			if ($(box))
				$(box).style.visibility = 'hidden';				
			var box = "ctnPgnLeft";
			troisPoint=troisPre;
			if ( (action == 'over') && ($(box).style.visibility == 'visible') )	
				return;			
			break;
		
		case "post":
			var box = "ctnPgnLeft";
			if ($(box))
				$(box).style.visibility = 'hidden';
			var box = "ctnPgnRight";			
			troisPoint=troisPost;
			if ( (action == 'over') && ($(box).style.visibility == 'visible') )	
				return;				
			break;
		
		default:
		return;
		break;
		
	}
	
	if ( ($(box).style.visibility == 'visible') ){
		if ( troisPoint == false)
			$(box).style.visibility = 'hidden';
								
	
		
	}else{
	
		pageBlockExecTime[id] = Math.random(1,999);
		pageBlockExecTimeLocal = pageBlockExecTime[id];
		
		$(box).style.visibility = 'visible';
					
		setTimeout('if( ( troisPoint == false)&&("' + pageBlockExecTimeLocal + '"==pageBlockExecTime["' + id + '"]) ){ $("' + box + '").style.visibility = \'hidden\';  }', 2000); 
		
	}
	
};

awakePageBlock = function(id, idle){
	if( idle == undefined)
		idle = 1000;
	switch(id){
	
		case "pre":
			var box = "ctnPgnLeft";
			troisPoint=troisPre;
			break;
		
		case "post":
			var box = "ctnPgnRight";	
			troisPoint=troisPost;		
			break;
		
		default:
			return;
			break;
		
	}
	
		pageBlockExecTime[id] = Math.random(1,999);
		pageBlockExecTimeLocal = pageBlockExecTime[id];
	
	$(box).style.display = 'block';
			
	setTimeout('if( (troisPoint == false ) && ("' + pageBlockExecTimeLocal + '"==pageBlockExecTime["' + id + '"]) ){ $("' + box + '").style.visibility = \'hidden\';  }', idle); 
};
/* End pagination */

/*
 * Utility functions
 */
function checkEnter(e) {
	
	var characterCode;

	if (e.which || e.keyCode) {
		if ((e.which == 13) || (e.keyCode == 13)) {
			return true;
		}
	} 
	return false;
}
function refreshKey(){
	var x = Math.random();
	document.getElementById('keyImage').src = "/include/accessKey/"+x;
}
function selectUnselectAll(){
	if ($('all').checked) {
		check(true);
	}
	else {
		check(false);
	}
}
   
function check(param){
	$$('.checkboxSingle').each(function(obj) {
		obj.checked = param;		
	});
}

function emptyDiv(elemId) {
	$(elemId).innerHTML = "";
	$(elemId).style.display = "none";
}

/* Open url in a tab or new standard window */
function pop(url){		
	
	try{
		if(url==undefined){
			return false;
		}
	}catch(e){ 
		return false;
	}
	
	// Firefox2 (open popup in tabs)
	if(navigator.userAgent != null && navigator.userAgent.indexOf( "Firefox/" ) != -1   && navigator.userAgent.indexOf( "Firefox/1" ) == -1){
	
		var nnwin = window.open(url,'mywin' + Math.random(1000));

	} else {
		
		// All other traditional
		var nnwin = window.open(url,'mywin' + Math.round(Math.random(1000)*1000)); 
		nnwin.blur();
		
	}
	
	return false;
}
/* End of utility functions */

/*
 * Slide show TGPs pictures to preview video
 * Mathieu Imbert
 */
var videoPreviewArray = new Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12");
var arrayIdx = 0;
var previewStarted = false;
var videoPreviewInterval;
var videoRestartTimeout;
var currentImageObject = '';
var currentVnumImage = '';
var previewDomain = 'http://rpcache.rpcache.com/images/';
var previewSize = '165x124';
var videoPckId = 1;

function initVideoPreview(imgOb, vnum, pckId, size) {
	
	videoPckId = pckId;
	if (pckId == 4) {
		previewDomain = 'http://rpcache.videosz.com/images/';
		videoPreviewArray = new Array("1", "3", "5", "7", "9", "11");
	} else {
		previewDomain = 'http://rpcache.rpcache.com/images/';
		videoPreviewArray = new Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12");
	}
	
	if (size !== undefined) {
		previewSize = size;
	}
	
	if (previewStarted == false) {
		arrayIdx = 0;
		currentImageObject = imgOb;
		currentVnumImage = vnum;
		
		// show first image as soon as mouseover occur
		newUrl = previewDomain+currentVnumImage+"/crop/"+previewSize+"/nt/"+videoPreviewArray[arrayIdx]+".jpg";
		currentImageObject.setAttribute("src",newUrl);
		
		previewStarted = true;
		videoPreviewInterval = setInterval("previewVideo()", 700);
	}
}

function previewVideo() {
	
	arrayIdx = (arrayIdx + 1) % videoPreviewArray.length;
		
	newUrl = previewDomain+currentVnumImage+"/crop/"+previewSize+"/nt/"+videoPreviewArray[arrayIdx]+".jpg";
	currentImageObject.setAttribute("src",newUrl);
		
	if (arrayIdx == videoPreviewArray.length - 1) {
		clearInterval(videoPreviewInterval);
		previewStarted = false;
		videoRestartTimeout = setTimeout("initVideoPreview(currentImageObject, '"+currentVnumImage+"', videoPckId)", 700*3);
	}
}

function stopVideoPreview() {

	clearTimeout(videoRestartTimeout);
	clearInterval(videoPreviewInterval);
	currentImageObject.setAttribute("src",previewDomain+currentVnumImage+"/presentation/"+previewSize+"/nt/1.jpg");
	currentVnumImage = "";
	previewStarted = false;
}
/* End video preview */

/* Method for popup under current window (once per page) */
var popupUrl = "";
function checkTarget(e) {
	var e = e || window.event;

	if (!popupUrl.empty()) {
		popunder(popupUrl);
	}
}

function initPopunder(url) {

	popupUrl = url;
	
	_Top = self;
	
	if (top != self) {
		try {
			if (top.document.location.toString())
			_Top = top;
		} catch(err) { }
	}
	
	if ( document.attachEvent ) {
		document.attachEvent( 'onclick', checkTarget );
	} else if ( document.addEventListener ) {
		document.addEventListener( 'click', checkTarget, false );
	}
}

function popunder(url){
	var popURL = "about:blank";
	var popID = "ad_" + Math.floor(89999999*Math.random()+10000000);

	if (getCookie('popup') == "true") {
		return true;
	}

	var PopWin = _Top.window.open(popURL,popID,'toolbar=0,scrollbars=1,location=1,statusbar=1,menubar=0,resizable=1');
	if (PopWin) {
		setCookie('popup', 'true', 365);

		PopWin.blur();

		if (navigator.userAgent.toLowerCase().indexOf("applewebkit") > -1) {
			_Top.window.blur();
			_Top.window.focus();
		}

		PopWin.Init = function(e) {
			with (e) {
				Params = e.Params;
				Main = function() {
					if (typeof window.mozPaintCount != "undefined") {
						var x = window.open("about:blank");
						x.close();
					}
		
					var popURL = Params.PopURL;
					try {
						opener.window.focus();
					} catch (err) { }
		
					window.location = popURL;
				};
				Main();
			}
		};

		PopWin.Params = {
			PopURL: url
		};
		PopWin.Init(PopWin);
	}

	return PopWin;
}
/* End of popup method */

/* correctly handle PNG transparency in Win IE 5.5 & 6 */
function correctPNG() // 
{
   var arVersion = navigator.appVersion.split("MSIE");
   var version = parseFloat(arVersion[1]);
   if ((version >= 5.5) && (document.body.filters)) 
   {
      for(var i=0; i<document.images.length; i++)
      {
         var img = document.images[i];
         var imgName = img.src.toUpperCase();
         if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
         {
            var imgID = (img.id) ? "id='" + img.id + "' " : "";
            var imgClass = (img.className) ? "class='" + img.className + "' " : "";
            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
            var imgStyle = "display:inline-block;" + img.style.cssText;
            if (img.align == "left") imgStyle = "float:left;" + imgStyle;
            if (img.align == "right") imgStyle = "float:right;" + imgStyle;
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
            var strNewHTML = "<span " + imgID + imgClass + imgTitle;
            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";";
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader";
            + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>";
            img.outerHTML = strNewHTML;
            i = i-1;
         }
      }
   }    
}

/* Handle Cookies */
function getCookie(search) {
	var i, key, value;
	var cookies = document.cookie.split(";");

	for (i=0 ; i < cookies.length ; i++) {
		key 	= cookies[i].substr(0,cookies[i].indexOf("="));
		value	= cookies[i].substr(cookies[i].indexOf("=")+1);
		key 	= key.replace(/^\s+|\s+$/g,"");
		if (key == search) {
			return unescape(value);
		}
	}
}

function setCookie(key, value, exdays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + exdays);
	
	var c_value = escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString()) + "; path=/";
	document.cookie = key + "=" + c_value;
}
