function changeLink(temp){
	for(var i=1;i<4;i++){
		if(temp == i){
			document.getElementById("headmenu"+i).className = "active";   
		}else{
			document.getElementById("headmenu"+i).className = ""; 
		}
	}
}

function changeMenuLink(temp){
	for(var i=1;i<7;i++){
		if(temp == i){
			document.getElementById("menu"+i).className = "active";   
		}else{
			document.getElementById("menu"+i).className = ""; 
		}
	}
}

function changeOnDemandMenuLink(temp){
	for(var i=1;i<5;i++){
		if(temp == i){
			document.getElementById("ondemandmenu"+i).className = "active";   
		}else{
			document.getElementById("ondemandmenu"+i).className = ""; 
		}
	}
}

function changeSortByLink(temp,temp2){
	var lis = new Array('viewBy_name','viewBy_date');
	var lis2 = new Array('sortBy_A','sortBy_B','sortBy_C','sortBy_D','sortBy_E','sortBy_F','sortBy_G','sortBy_H','sortBy_I','sortBy_J','sortBy_K','sortBy_L',
			'sortBy_M','sortBy_N','sortBy_O','sortBy_P','sortBy_Q','sortBy_R','sortBy_S','sortBy_T','sortBy_U','sortBy_V','sortBy_W','sortBy_X','sortBy_Y','sortBy_Z',
			'sortBy_A1','sortBy_B1','sortBy_C1','sortBy_D1','sortBy_E1','sortBy_F1','sortBy_G1','sortBy_H1','sortBy_I1','sortBy_J1','sortBy_K1','sortBy_L1',
			'sortBy_M1','sortBy_N1','sortBy_O1','sortBy_P1','sortBy_Q1','sortBy_R1','sortBy_S1','sortBy_T1','sortBy_U1','sortBy_V1','sortBy_W1','sortBy_X1','sortBy_Y1','sortBy_Z1');
	if(temp == "viewBy_null" || temp == "viewBy_" || temp == "viewBy_rating" || temp == "viewBy_movies"|| temp == "viewBy_scenes"|| temp == "viewBy_exclusive"|| temp == "viewBy_name2"){
		temp = 'viewBy_name';
	}
	if(temp2 == "sortBy_null" || temp2 == "sortBy_" ){
		temp2 = 'sortBy_A';
	}
	for(var i=0; i < lis.length; i++){
		if (lis[i].indexOf(temp,0) >= 0){
			document.getElementById(lis[i]).className = "active";
		} else {
			document.getElementById(lis[i]).className = "";
		}
	}
	for(var j=0; j< lis2.length; j++){
		if (lis2[j].indexOf(temp2,0) >= 0){
			document.getElementById(lis2[j]).className = "active";
		} else {
			document.getElementById(lis2[j]).className = "";
		}
	}
}

function changeCommonSortByLink(temp,temp2){
	var lis = new Array('viewBy_movie','viewBy_clip');
	var lis2 = new Array('sortBy_date','sortBy_name','sortBy_rating');
	if(temp == "viewBy_null" || temp == "viewBy_" ){
		temp = 'viewBy_movie';
	}
	if(temp2 == "sortBy_null" || temp2 == "sortBy_" ){
		temp2 = 'sortBy_date';
	}
	for(var i=0; i < lis.length; i++){
		if (lis[i].indexOf(temp,0) >= 0){
			document.getElementById(lis[i]).className = "active";
		} else {
			document.getElementById(lis[i]).className = "";
		}
	}
	for(var j=0; j< lis2.length; j++){
		if (lis2[j].indexOf(temp2,0) >= 0){
			document.getElementById(lis2[j]).className = "active";
		} else {
			document.getElementById(lis2[j]).className = "";
		}
	}
}


function changePageLink(temp,temp2){
	if(document.getElementById(temp)){
		document.getElementById(temp).className = "active";  
	}
	if(document.getElementById(temp2)){
		document.getElementById(temp2).className = "active";  
	}
}


function checkField(){   
    var usernameValue = document.getElementById("username").value;   
    var passwordValue = document.getElementById("password").value; 
    usernameValue = usernameValue.replace(/\s/gi,"");
    passwordValue = passwordValue.replace(/\s/gi,"");
    if(usernameValue !== "" && passwordValue !== ""){
    	return true;
    }else if(usernameValue == "" && passwordValue == ""){
        document.getElementById("error_username").innerHTML = "username can't be null !";
        document.getElementById("error_password").innerHTML = "password can't be null !";
        return false;   
    }else if(usernameValue !== "" && passwordValue == ""){
    	document.getElementById("password").focus();     
    	document.getElementById("error_username").innerHTML = "";
    	document.getElementById("error_login").innerHTML = "";
    	document.getElementById("error_password").innerHTML = "password can't be null !";
    	return false;
    }else if(passwordValue !== "" && usernameValue == ""){
    	document.getElementById("username").focus();
    	document.getElementById("error_password").innerHTML = "";
    	document.getElementById("error_login").innerHTML = "";
    	document.getElementById("error_username").innerHTML = "username can't be null !";
    	return false;   
    }
}


function ratingStarMouseMove(num) {
	for ( var i = 1; i < 5; i++) {
		var img = document.getElementById('rating' + i);
		if (img) {
			if (i <= num) {
				img.src = "../images/star-full.gif";
			} else {
				img.src = "../images/star.gif";
			}
		}
	}
}

function ratingStarMouseMoveIterator(id,num) {
	for ( var i = 1; i < 5; i++) {
		var img = document.getElementById('scene' + id + 'rating' + i);
		if (img) {
			if (i <= num) {
				img.src = "../images/star-full.gif";
			} else {
				img.src = "../images/star.gif";
			}
		}
	}
}

function ratingStarMouseOut(){
	for ( var i = 1; i < 5; i++) {
		var img = document.getElementById('rating' + i);
		if (img) {
			img.src = "../images/star.gif";
		}
	}
}

function ratingStarMouseOutIterator(id){
	for ( var i = 1; i < 5; i++) {
		var img = document.getElementById('scene' + id + 'rating' + i);
		if (img) {
			img.src = "../images/star.gif";
		}
	}
}



function ratingActorClick(userId,num,actorId){
	if (userId == 0) {
		alert("You have to be logged in to rating !")
		return;
	}else{
		RatingAction.addRatingActor(userId,num,actorId,function(callBack){
			alert(callBack);}
		);
	}
}

function ratingMovieClick(userId,num,movieId){
	if (userId == 0) {
		alert("You have to be logged in to rating !")
		return;
	}else{
		var url = "../ajax/RatingMovie";
		var params = {
			userId : userId,
			num : num,
			movieId : movieId
		};
		jQuery.post(url, params, callbackFun, 'json');
	}
}

function callbackFun(data) {
	alert(data);
}


function ratingSceneClick(userId,num,sceneId){
	if (userId == 0) {
		alert("You have to be logged in to rating !")
		return;
	}else{
		RatingAction.addRatingScene(userId,num,sceneId,function(callBack){
			alert(callBack);}
		);
	}
}

function ratingClipClick(userId,num,clipId){
	if (userId == 0) {
		alert("You have to be logged in to rating !")
		return;
	}else{
		RatingAction.addRatingClip(userId,num,clipId,function(callBack){
			alert(callBack);}
		);
	}
}

function loadImage(elem,url){
	elem.src = url;
}

/**
 * Enlarge the Cover
 * @param {} src
 */
function enlarge(elemId){
	var src = jQuery('#' + elemId).attr('cover');
	window.open(src, '', 'left=50,top=50,width=900,height=600,scrollbars=1,status=no,toolbar=no,location=no,menu=no,resizable=1');
}

/**
 * Send the SIGN ME UP
 */
function sendSignMeUp(){
	var reg=/^[a-zA-Z]+\w*([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
	var emailTemp = jQuery("#Email_Address").val().replace(/\s/gi,"");
	if(!reg.test(emailTemp)){
		alert("Invalid email address");
		return;
	}else{
		var url = '../ajax/nl';
		var params = {};
		params['lid'] = 900039135;
		params['fid'] = 900057583;
		params['f#31'] = 'TEXT:Email%20Address';
		params['f:31'] = emailTemp;
		params['url'] = 'http://www.TitanMen.com/help/faq_mailinglist.asp?confirm=1';
		
		jQuery.ajax({
		    url: url,
		    type: 'POST',
		    dataType: 'json',
		    timeout: 20000,
		    data: params,
		    error: function(data){
		        alert('Sorry, we are experiencing networking difficulties, please try again later.');
		    },
		    success: function(data){
		       onSendSignMeUpResponse(data);
		    }
		});
	}
}

function onSendSignMeUpResponse(data){
	//bugs?
	/*
	var elem = '<div class="smu-just">'
		+ '<a href="#" style="float: right;" class="modelClose">close</a>'
		+ '<h4>One more step is needed!</h4> '
		+ '<p>You will receive a confirmation email from:<br><span class="smu-red"><strong>titanmen@topica.email-publisher.com.</strong></span></p>'
		+ '<p>You must click the confirmation link within the email to complete your subscription.</p>'
		+ '<p><strong>Your confirmation email may arrive in your "Spam" or "Junk E-mail" folder. Please check that folder.</strong></p>'
  		+ '</div>';
  		
  	jQuery.modal(elem,{closeClass:'modelClose'});
  	*/
	
  	jQuery('#nlPopup').modal({closeClass:'modelClose'});
}

var NavigationTree = function(){
	this.categoryCollapse = false;
	this.studioCollapse = false;
	this.directorCollapse = false;
}

function onStudioClick(){
	var e = jQuery('#ttSTD');
	if(NavigationTree.studioCollapse == true){
		expand('ttSTD');
	}else{
		collapse('ttSTD');
	}
}

function onCategoryClick(){
	var e = jQuery('#ttCTG');
	if(NavigationTree.categoryCollapse == true){
		expand('ttCTG');
	}else{
		collapse('ttCTG');
	}
}

function onDirectorClick(){
	var e = jQuery('#ttDRC');
	if(NavigationTree.directorCollapse == true){
		expand('ttDRC');
	}else{
		collapse('ttDRC');
	}
}

/**
 * 
 * @param {} gId
 */
function expand(gId){
	var stdElem = jQuery('#' + gId);
	
	//stdElem.attr('collapse',false);
	stdElem.css('background','url(../images/unbullet.gif) no-repeat 143px 2px');
	
	/*
	if(gId == 'ttSTD'){
		jQuery('li.studioClass').each(function(){
			jQuery(this).css('display','');
		});
	}else if(gId == 'ttCTG'){
		jQuery('li.categoryClass').each(function(){
			jQuery(this).css('display','');
		});
	}else if(gId == 'ttDRC'){
		jQuery('li.directorClass').each(function(){
			jQuery(this).css('display','');
		});
	}else{
		//np
	}
	*/
	
	var e = null;
	if(gId == 'ttSTD'){
		NavigationTree.studioCollapse = false;
		e = jQuery('li.studioClass');
	}else if(gId == 'ttCTG'){
		NavigationTree.categoryCollapse = false;
		e = jQuery('li.categoryClass');
	}else if(gId == 'ttDRC'){
		NavigationTree.directorCollapse = false;
		e = jQuery('li.directorClass');
	}else{
		//np
	}
	if(e != null){
		e.each(function(){
			jQuery(this).show();
		});
	}
}

function collapse(gId){
	var stdElem = jQuery('#' + gId);
	//stdElem.attr('collapse',true);
	stdElem.css('background','url(../images/bullet.gif) no-repeat 143px 2px');
	
	/*
	if(gId == 'ttSTD'){
		jQuery('li.studioClass').each(function(){
			jQuery(this).css('display','none');
		});
	}else if(gId == 'ttCTG'){
		jQuery('li.categoryClass').each(function(){
			jQuery(this).css('display','none');
		});
	}else if(gId == 'ttDRC'){
		jQuery('li.directorClass').each(function(){
			jQuery(this).css('display','none');
		});
	}else{
		//np
	}
	*/
	
	var e = null;
	if(gId == 'ttSTD'){
		NavigationTree.studioCollapse = true;
		e = jQuery('li.studioClass');
	}else if(gId == 'ttCTG'){
		NavigationTree.categoryCollapse = true;
		e = jQuery('li.categoryClass');
	}else if(gId == 'ttDRC'){
		NavigationTree.directorCollapse = true;
		e = jQuery('li.directorClass');
	}else{
		//np
	}
	if(e != null){
		e.each(function(){
			jQuery(this).hide();
		});
	}
}

function initCollapseOrExpand(){
	expand('ttSTD');
	expand('ttCTG');
	expand('ttDRC');
}


/** movie Description Expand & Collapse */
function exco(elem){
	var e = jQuery(elem);
	if(e.attr('collapse') == true || e.attr('collapse') == "true"){
		
		jQuery('p.desc').each(function(){
			jQuery(this).css("height",'auto');		
		});
		e.css('background','url(../images/desc-up.gif) no-repeat 0 0');
		e.attr('collapse',false);
		return;
	}else{
		jQuery('p.desc').each(function(){
			jQuery(this).css("height",'130px');		
		});
		e.css('background','url(../images/desc-down.gif) no-repeat 0 0');
		e.attr('collapse',true);
		return;
	}
}

//INIT THE LEFT-NAVIGATION
function initLeftNavigation(){
	var stdElem = jQuery('#ttSTD');
	var ctgElem = jQuery('#ttCTG');
	var drcElem = jQuery('#ttDRC');
	if(stdElem != null && ctgElem != null && drcElem != null){
		stdElem.click(function(){
			onStudioClick();
		});
		ctgElem.click(function(){
			onCategoryClick();
		});
		drcElem.click(function(){
			onDirectorClick();
		})
		initCollapseOrExpand();
	}
}


jQuery(document).ready(function(){
	//INIT THE LEFT-NAVIGATION
	initLeftNavigation();
});

