﻿var root_domain = "ganges.com";

// checking mail
function VerifyEmail(values){
	return values.search(/^\s*[\w\~\-\.]+\@[\w\~\-]+(\.[\w\~\-]+)+\s*$/g)>=0;
}

function setCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/;domain=" + root_domain;
}

function getCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function delCookie(name) {
	setCookie(name,"",-1);
}

function signup(){
	var nowUrl = String(document.location);
	if(nowUrl.indexOf("/users/?referer=") == -1){
		document.location.href = "/users/?referer=" + encodeURIComponent(nowUrl) + "&mode=signup";
	}else{
		document.location.href = document.location.href;
	}
}

function login(){
	var nowUrl = String(document.location);
	if(nowUrl.indexOf("/users/?referer=") == -1){
		document.location.href = "/users/?referer=" + encodeURIComponent(nowUrl) + "&mode=login";
	}else{
		document.location.href = document.location.href;
	}
}

function setMature(){
	var nowUrl = String(document.location);
	document.location.href = "/users/setMature.html?referer=" + encodeURIComponent(nowUrl);
}

function FlashMedia(byPath, byWidth, byHeight) {
	document.write('<object width="' + byWidth + '" height="' + byHeight + '" type="application/x-mplayer2">');
	document.write('<param name="FileName" value="' + byPath + '" /><param name="ShowControls" value="1" /><param name="ShowDisplay" value="0" />');
	document.write('<param name="ShowStatusBar" value="0" /><param name="AUTOSTART" value="1" /></object>');
}



function headSearchCheck(){
	return false;
}


function searchChange(obj,u){
	var q = document.getElementById("q");
	obj.form.action = u;
	if (q.value != "")
	{
		obj.form.submit();	
	}
}

function qCheck(obj){
	var q = document.getElementById("q");
	if (q.value == "" | q.value == "Search Videos : " | q.value == "Search Videoseries : " | q.value == "Search Photos : " | q.value == "Search Photoseries : ") return false;
	obj.submit();
	obj.disabled = true;
	return false;
}


function urlReplace(qValue){
	var filterValue ;
	var regexp = /[\`\~\!\@\#\$\%\^\&\*\(\)\_\+\|\-\=\\\{\}\[\]\;\'\:\"\,\.\/\<\>\?]+/gi ;
	filterValue = qValue.replace(regexp," ");
	var regexp = /\s/gi ;
	filterValue	= filterValue.replace(regexp, "_")
	var regexp = /\_{2,}/gi ;
	filterValue	= filterValue.replace(regexp, "_")
	return filterValue;
}

var searchLoading = false;

function searchKeyChk(evt){
	var keyName = evt.keyCode? evt.keyCode : evt.charCode;
	keyName  = parseInt(keyName);
	if (keyName == 13) {
		searchExe();
	}
}

function searchExe(){
	var q = document.getElementById("q").value;
	var searchType = document.getElementsByName("searchType");
	var num = 0;
	for (var iNum=0 ; iNum<searchType.length ;iNum++ )
	{
		if(searchType[iNum].checked){
			num = iNum + 1;
			break;
		}
	}
	if (q != "")
	{
		switch (num)
		{
		case 2:
			var menuType = "VideoseriesSearch";
			break;
		case 3:
			var menuType = "PhotosSearch";
			break;
		case 4:
			var menuType = "PhotoseriesSearch";
			break;
		default:
			var menuType = "VideosSearch"
			break;
		}
		var searchUrl = "/" + urlReplace(q) + "_" + menuType + "/";
		if (searchLoading) return false;
		searchLoading = true;
		document.location.href = searchUrl ;
	}
}

function detailView(b){
	setCookie("detailview",b,365);
	document.location.reload();
}

function simpleView(b){
	setCookie("simpleview",b,365);
	document.location.reload();
}


function delContents(_m,_idx){
	switch (_m)
	{
		case "1":
			_msg = "Are you sure you want to delete this video? (This cannot be undone.)"
			_u = "/video/call_delete.html?idx=" + _idx;
			break;
		case "2":
			_msg = "Are you sure you want to delete this videoseries? (This cannot be undone.)"
			_u = "/series/call_delete.html?idx=" + _idx;
			break;
		case "3":
			_msg = "Are you sure you want to delete this photo? (This cannot be undone.)"
			_u = "/photo/call_delete.html?idx=" + _idx;
			break;
		case "4":
			_msg = "Are you sure you want to delete this photoseries? (This cannot be undone.)"
			_u = "/photoseries/call_delete.html?idx=" + _idx;
			break;
	}
	if (confirm(_msg)){
		new Ajax.Request(_u,{
		parameters:{
			idx:_idx
		},
		onSuccess:function(res){
			document.location.reload();
		},
		onFailure:function(res){
			alert(res.responseText);
		}
	}) ;
	}
}


function setFeatured(_b,_m,_idx){
	_u = "/common/setFeatured.html";
	new Ajax.Request(_u,{
		parameters:{
			b:_b,
			m:_m,
			idx:_idx
		},
		onSuccess:function(res){
			document.location.reload();
		},
		onFailure:function(res){
			//alert(res.responseText);
		}
	}) ;
}


function setSexy(_b,_m,_idx){
	_u = "/common/setSexy.html";
	new Ajax.Request(_u,{
		parameters:{
			b:_b,
			m:_m,
			idx:_idx
		},
		onSuccess:function(res){
			document.location.reload();
		},
		onFailure:function(res){
			//alert(res.responseText);
		}
	}) ;
}


function setHiddenBest(_b,_idx){
	_u = "/common/setHiddenBest.html";
	new Ajax.Request(_u,{
		parameters:{
			b:_b,
			idx:_idx
		},
		onSuccess:function(res){
			document.location.reload();
		},
		onFailure:function(res){
			//alert(res.responseText);
		}
	}) ;
}

function defaultPress(){
	delCookie("defaultView");
	setCookie("defaultView","1",999);	
}


function customizePress(){
	delCookie("defaultView");
	setCookie("defaultView","0",999);
	document.location.reload();
}

function isExistArray(arr,value){
	var arrLen = arr.length;
	for (var i=0; i<arrLen ; i++ ){
		if (arr[i] == value){
			return true;
		}
	}
	return false;
}


function setLang(lang){
	delCookie("lang");
	setCookie("lang",lang,999);
	document.location.reload();
}

function qfocus(){
	var q = document.getElementById("q");
	if (q)
	{
		q.focus();	
	}
}


function langInit(){
	var allowLang = new Array("jp","cn","es","fr","de","it","kr","pt","ru","tw");
	var myLang = getCookie("lang");
	var urlArr = document.location.toString().split("/");
	var firstDomain = urlArr[2].substring(0,urlArr[2].indexOf("."));
	if (isExistArray(allowLang,myLang) && (myLang != firstDomain) && (firstDomain == "www" || firstDomain == "ganges")){
		urlArr[2] = myLang + "." + root_domain ;
		document.location.href = urlArr.join("/");
	}
}
//langInit();


// Alpha Management
var APM = Class.create({
	initialize:function(){
		this.alphaTemp = 0;
		this.alphaIntervalId = 0;
		this.onEnd = function(){};
	},
	setAlpha:function(id,alpha) {
		$(id).setStyle({
			opacity:(alpha/100),
			MozOpacity:(alpha/100),
			KhtmlOpacity:(alpha/100),
			filter:"alpha(opacity=" + alpha + ")"
		});
	},
	downAlpha:function(id,endAlpha,speed){
		this.alphaTemp -= speed;
		if (this.alphaTemp <= endAlpha){
			this.setAlpha(id,endAlpha);
			clearInterval(this.alphaIntervalId);
			this.onEnd();
		}else{
			this.setAlpha(id,this.alphaTemp);
		}
	},
	upAlpha:function(id,endAlpha,speed){
		this.alphaTemp += speed;
		if (this.alphaTemp >= endAlpha){
			this.setAlpha(id,endAlpha);
			clearInterval(this.alphaIntervalId);
			this.onEnd();
		}else{
			this.setAlpha(id,this.alphaTemp);
		}
	},
	changeAlpha:function(id, endAlpha,speed,duration) {
		this.alphaTemp = $(id).getStyle("opacity")*100;
		if (isNaN(this.alphaTemp)) this.alphaTemp = 100;
		if (this.alphaTemp > endAlpha){
			this.alphaIntervalId = window.setInterval(function(){this.downAlpha(id,endAlpha,speed);}.bind(this),speed);
		}else{
			this.alphaIntervalId = window.setInterval(function(){this.upAlpha(id,endAlpha,speed);}.bind(this),speed);
		}
	}
});



var pMenu = {};
pMenu.mItem = [];
pMenu.mTime = [];
pMenu.mWait = 250;
pMenu.mSet = function(ul){
	if (document.getElementById) {
		ul = document.getElementById(ul).getElementsByTagName('ul');
		var i, j, e, a, f, b;
		var m = pMenu.mItem.length;
		for (i = 0; i < ul.length; i++) {
			if (e = ul[i].getAttribute('id')) {
				pMenu.mItem[m] = e;
				e = ul[i].parentNode;
				f = new Function('pMenu.mShow(\'' + pMenu.mItem[m] + '\');');
				b = new Function('pMenu.mBlur(\'' + pMenu.mItem[m] + '\');');
				e.onmouseover = f;
				e.onmouseout = b;
				a = e.getElementsByTagName('a');
				for (j = 0; j < a.length; j++) {
					a[j].onfocus = f;
					a[j].onblur = b;
				}
				m++;
			}
		}
	}
}



pMenu.mShow = function(id) {
	for (var i = 0; i < pMenu.mItem.length; i++) {
		if (document.getElementById(pMenu.mItem[i]).style.display != 'none') {
			if (pMenu.mItem[i] != id) mShow.mHide(pMenu.mItem[i]);
			else pMenu.mClear(pMenu.mItem[i]);
		}
	}
	document.getElementById(id).style.display = 'block';
}


pMenu.mHide = function(id) {
	pMenu.mClear(id);
	document.getElementById(id).style.display = 'none';
}


pMenu.mBlur = function(id) {
	pMenu.mTime[id] = setTimeout('pMenu.mHide(\'' + id + '\');', pMenu.mWait);
}


pMenu.mClear = function(id) {
	if (pMenu.mTime[id]) {
		clearTimeout(pMenu.mTime[id]);
		pMenu.mTime[id] = null;
	}
}