// JavaScript Document
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_popupMsg(msg) { //v1.0
  confirm(msg);
}

function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_changeProp(objName,x,theProp,theValue) { //v6.0
  var obj = MM_findObj(objName);
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
    if (theValue == true || theValue == false)
      eval("obj."+theProp+"="+theValue);
    else eval("obj."+theProp+"='"+theValue+"'");
  }
}

//Free Hack - Start
function VerifySearchBarForm(){
  if($('SearchBarKeyword').value==''){
   alert('关键词不能为空');
   $('SearchBarKeyword').focus();
   	return false;
  }else{
	  var searchKeyword = $('SearchBarKeyword').value.replace(/\//g,'');
	  var searchUrl = $('SearchBarForm').action + '/q/' + encodeURIComponent( searchKeyword ) +'/';
	  window.location.href= searchUrl;
	  return false;
	  //$('SearchBarForm').submit();
	  //$('SearchBarKeyword').disabled = true;
  }
}
////from /user/js/login.js
//我的优盘登陆
function my(){
	new Ajax.Request(
      "/my/islogined/",
      {method: 'get',
      onSuccess:function(o){
		if(o.responseText=='0'){
			openLoginDialog(gotomy);
      	}else if(o.responseText=='1'){
			window.location='/my/';
      	}
      	}
      }
    );
}
//回调:跳转到我的优盘
function gotomy(){
	window.location="/my/";
}
//登陆
function login(callback,param){
	new Ajax.Request(
         "/user/loginCommit",
         {method: 'post',
          parameters: Form.serialize('form_login'),
          onSuccess: function(o){
          	if(o.responseText=='0'){
          		alert("用户名或密码不正确，请重试。");
          	}else{
			run_cpa('login');
          		if(callback==null){
          			document.location.reload();
          		}else{
          			callback(param);
          		};

          	};
          }
         }
    );
}

function logout(){
	run_cpa('logout');
	window.location.href='/user/logout';
}
function mynull()
{
    nova_update("welcome", "/index/status");
	Dialog.okCallback();
}
//回答问题
function q_answer(){
	    if($('answer').value.length==0){
	    	alert("答案不能为空");
	    	return;
	    }
		new Ajax.Request(
         "/user/resetpwd/step/2/",
         {method: 'post',
          parameters: Form.serialize('form_resetpwd2'),
          onSuccess: function(o){
          	if(o.responseText=='0'){
			alert('你的生日不对吧？');
          	}else{
					$('resetpwd_content').innerHTML='<p >&nbsp;</p>\
					    <p >&nbsp;</p>\
					        <p>请在你注册的邮箱里，按提示修改你的密码</p>';

          	};
          }
          }
	    );
}
//找回密码提交
function resetpwd(){
	    if($('user_name2').value.length==0){
	    	alert("用户名不能为空");
	    	return;
	    }
	    //Element.show('page_loading');
		new Ajax.Request(
         "/user/resetpwd/step/1/",
         {method: 'post',
          parameters: Form.serialize('form_resetpwd1'),
          onSuccess: function(o){
			//Element.hide('page_loading');
          	if(o.responseText=='0'){
		alert('用户名不存在');
        //  	}else if(o.responseText=='1'){
	//		$('resetpwd_content').innerHTML='<p >&nbsp;</p>\
	//		<p >&nbsp;</p>\
	//		<p>请在你注册的邮箱里，按提示修改你的密码</p>';
          	}else{
$('resetpwd_content').innerHTML='<form id="form_resetpwd2" name="resetpwd2" method="post" action="/user/resetpwd/step/2/" onSubmit="javascript:q_answer();return false;">\
        <table width="100%" border="0" cellpadding="0" cellspacing="0">\
      <tr>\
        <td align="center" style="padding-top:8px;">你的生日[格式0000-00-00]：</td>\
      </tr>\
      <tr>\
        <td align="center"><input type="text" name="answer" id="answer"  /></td>\
      </tr>\
    </table>\
    <p class="p_5" style="padding:5px;">\
      <input type="submit" name="submit" id="btn_reset_sure" class="button_sys" value="确　定"/></form>';
          	};
          }
          }
	    );
}

function change_bg(obj,x,theProp,theValue) {
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
    if (theValue == true || theValue == false)
      eval("obj."+theProp+"="+theValue);
    else eval("obj."+theProp+"='"+theValue+"'");
  }
}

//打开登陆窗口
//callback为回调函数：整个函数不能含有任何单引号
//param 回调函数的参数(目前仅支持一个)
function openLoginDialog(callback,param){
	if(callback!=null&&callback.toString().split(' ')[0]=='function'){
		callback = callback.toString().split(' ')[1];
		callback = callback.substring(0,callback.indexOf('('));
	}
	var content='<div style="width:204px; height:180px; border:2px solid #A8A8A8; background-color:#EDEFF0;overflow:hidden">\
  <table width="100%" height="22" border="0" cellpadding="0" cellspacing="0" class="bold" style="background:url(/index/img/login_bg.gif) repeat-x top;">\
    <tr>\
      <td align="center" style="padding-left:16px;">会员登录</td>\
      <td width="16" class="nounderline"><a href="javascript:Dialog.cancelCallback()">X</a></td>\
    </tr>\
  </table>\
  <form id="form_login" name="form_login" action="/user/loginCommit" method="POST" onsubmit=\"login('+callback+','+'\''+param+'\''+');return false;\">\
  <div style="width:190px; height:132px; margin:0 auto; text-align:center; background-color:#ffffff;">\
    <p class="p_5 m_t_5" style="padding:5px;margin-top:5px;">\
      <input name="user_name_login"  id="user_name_login" type="text" style=" background-image:url(/index/img/log_name.gif); background-repeat:no-repeat; background-position:left;" onFocus="change_bg(this,\'\',\'style.background\',\'none\',\'INPUT/TEXT\')" />\
    </p>\
    <p class="p_5" style="padding:5px;"><input name="passwd_login" id="passwd_login" type="password" style=" background-image: url(/index/img/log_password.gif); background-repeat:no-repeat; background-position:left;"  onFocus="change_bg(this,\'\',\'style.background\',\'none\',\'INPUT/TEXT\')"/></p>\
    <p>\
      <input name="forever" id="forever" value="1" checked="true" type="checkbox" class="no_border"/>\
      记住我</p>\
    <p class="p_5" style="padding:5px;">\
    <input type="submit" name="submit" value="登 录" class="button_sys_s bold"/>\
    </p>\
	<p class="c_gra2 t_r" style="padding-right:3px;"><a href="javascript:findpwd()">忘记密码</a></p>\
  </div>\
  </form>';
  var cncmax_user=Nova.Cookie.get('cncmax_user');
  if(cncmax_user)
  	content=content+'\
    <div style="text-align:center;">\
		<FORM name="cncLoginForm" METHOD=POST ACTION="http://www.cncmax.cn/cs/Satellite?c=Page&pagename=PCcncmax/Page/UserLogin">\
		<INPUT TYPE="hidden" name="accessReturnURL" value="http://yoqoo.cncmax.cn/cncmax/login_success">\
		<INPUT TYPE="hidden" name="errorReturnURL" value="http://yoqoo.cncmax.cn/cncmax/login_failure">\
		<a href="#" onclick="javascript:cncLoginForm.submit();">CNCMAX用户登录</a>\
		</FORM>\
	</div>';
	content=content+'\
  <table width="100%" border="0" cellpadding="0" cellspacing="0">\
    <tr>\
      <td align="center" style="color:red;font-weight:bold">新用户请 <a href="javascript:window.location=\'/user/signup\'" style="font-size:11pt;text-decoration:none">注册</a></td>\
    </tr>\
  </table>\
</div>';
	Dialog.custom(content,{windowParameters: {width:208, height:184}});
	$("user_name_login").value=' ';
	$("user_name_login").focus();
	$("user_name_login").value='';
}
//取回密码窗口
function findpwd(){
	//Dialog.cancelCallback();
	var content='<div style="width:204px; height:138px; border:2px solid #A8A8A8; background-color:#EDEFF0;overflow:hidden">\
  <table width="100%" height="22" border="0" cellpadding="0" cellspacing="0" class="bold" style="background:url(/index/img/login_bg.gif) repeat-x top;">\
    <tr>\
      <td align="center" style="padding-left:16px;">取回密码</td>\
      <td width="16" class="nounderline"><a href="javascript:Dialog.cancelCallback()">X</a></td>\
    </tr>\
  </table>\
  <div id="resetpwd_content" style="width:190px; height:92px; margin:0 auto; text-align:center; background-color:#ffffff;">\
    <form id="form_resetpwd1" name="resetpwd1" method="post" action="/user/resetpwd/step/1/" onSubmit="resetpwd();return false;">\
    <p class="p_5 c_service bold" style="padding:5px;margin-top:5px;">请输入会员名 :    </p>\
    <p>\
      <input name="user_name" id="user_name2" type="text" style=" background-image:url(/index/img/log_name.gif); background-repeat:no-repeat; background-position:left;" onclick="change_bg(this,\'\',\'style.background\',\'none\',\'INPUT/TEXT\')" />\
    </p>\
    <p class="p_5"  style="padding:5px;">\
      <input type="submit" name="submit" id="btn_reset_sure" class="button_sys" value="下一步"/>\
    </p>\
  </form></div>\
  <table width="100%" border="0" cellpadding="0" cellspacing="0" class="nounderline m_t_5">\
    <tr>\
      <td align="center"><a href="/user/signup/" class="c_red bold">[免费注册]</a></td>\
      <td width="80">&nbsp;</td>\
      <td align="center"><a href="javascript:openLoginDialog()" class="bold">登录</a></td>\
    </tr>\
  </table>\
</div>';
	Dialog.custom(content,{windowParameters: {width:208, height:142}});
	$("user_name2").value='  ';
	$("user_name2").focus();
	$("user_name2").value='';
}

window.nova_init_hook_login = function() {
    var saved = Nova.Cookie.get('k');
    var user = Nova.Cookie.get('u');
    if (!user && saved) {
        nova_update('welcome', '/index/status');
        return;
    }
    if (user) if (user.charAt(0)=='%') user = decodeURIComponent(user);
    var welcome = user ?
        '您好, <a href="/my/" style="color:#003399;text-decoration:underline;">'+user+'</a> &nbsp; &nbsp; <a href="javascript:logout();" target="_top">注销</a> &nbsp; &nbsp; <a href="/yoqoo/help/words.html" target="_top">帮助</a>' :
        '<a href="javascript:openLoginDialog('+callback+')" target="_top">登录</a> &nbsp; &nbsp; <a href="/user/signup" target="_blank">注册</a> &nbsp; &nbsp; <a href="/yoqoo/help/words.html" target="_top">帮助</a>';
    if ($("welcome")) Element.update($("welcome"), welcome);
}

var lastNavId = "s_nav_preload";
function highlight(module, search) {
    var style = {
        index:    "s_page_home",
        v:        "s_page_video",
        pk:       "s_page_lists",
        category: "s_page_class",
        club:     "s_page_club",
	bbs:      "s_page_club",
        top:      "s_page_bill",
        user:     "s_page_show",
        my:       "s_page_myshow"
    };
    var select = {
        v:        0,
        pk:       1,
        club:     2,
        bbs:      2,
        user:     3
    };
	
    var searchType = {
	0:	'video',
	1:	'pk',
	2:	'club',
	3:	'user'
    };
    
    if ($(lastNavId)) 
	    $(lastNavId).id = style[module] ? style[module] : style['index'];
    lastNavId = style[module] ? style[module] : style['index'];
    search = search ? search : module;
    search = select[search] ? searchType[select[search]] : searchType[0];
    if ($("moduleType")) 
	    $("moduleType").selectedIndex = select[search];
    $('SearchBarForm').action='/search/'+search;
}

window.nova_init_hook_nav = function() {
    var path = document.location.pathname.split('/');
    var module = path[1] ? path[1] : 'index';
    var url = document.location.href;
    if (module=="search"){
	    //keyword
	    var regrex = /(keyword|q)[\/|\=](.*)/;
	    var matches = url.match(regrex);
	    var keyword = matches[2] ? matches[2] : '';
	    if(keyword != ''){
		    var indexSlash = keyword.indexOf('/');
		    if( indexSlash== -1){
			    indexSlash = keyword.indexOf('&');
		    }
		    if(indexSlash != -1){
			    var keyword = keyword.substring(0,indexSlash);
		    }
	    }  
	    keyword = keyword.replace(/\+/g, ' ');
	    $('SearchBarKeyword').value=decodeURIComponent(keyword);
    }

    if ($('SearchBarKeyword').value.length) $('SearchBarKeyword').style.background='#FAFBFF';
    switch (module) {
        case 'v':
            for (var ii=2; ii<path.length; ii++) if (path[ii-1]=='module') module=path[ii];
            if (!module) module='v';
            var m1, m2;
            m1 = url.replace(/^.*moduleType=([A-Za-z]+).*$/g, "$1");
            m2 = url.replace(/^.*module=([A-Za-z]+).*$/g, "$1");
            module = (m1!=url ? m1 : (m2!=url ? m2 : module)).toLowerCase();
            break;
        case 'tag':
            if(-1!=document.location.pathname.indexOf('/ot/2')) module = "club";
            else if(-1!=document.location.pathname.indexOf('/ot/3')) module = "pk";
            else  module='v';
            break;
        case 'category':
            if(-1!=document.location.pathname.indexOf('/club/')) module = "club";
            else if(-1!=document.location.pathname.indexOf('/show/ot/3')) module = "pk";
            break;
    }
    var search = module=="search" ? path[2] : module;
    switch (module) {
        case 'top':
            search='video';
            var temp=1;
            for (var ii=2; ii<path.length; ii++) if (path[ii-1]=='type') temp=path[ii];
            if (temp==2||(temp>10&&temp<20)) search = "pk";
            else if (temp==3||(temp>20&&temp<30)) search = "club";
            else if (temp==4||(temp>30&&temp<40)) search = "user";
            break;
        case 'my':
            if('club'==path[2]) search = "club";
            else if('friend'==path[2]) search = "user";
            else if('pk'==path[2]) search = "pk";
            break;
    }
    if('search'==module) { module=search;}
    if('video'==module) module='v';
    highlight(module, search);
    loadAds(module);
}

function loadAds(module) {
    /**cpa**/
    Nova.addScript("http://adr.yoqoo.com/cpa_yoqoo.js");
    setTimeout("run_cpa('view');", 500);

    /* Google */
    Nova.addScript("http://www.google-analytics.com/urchin.js");
    setTimeout("run_google();", 500);
    /* Allyes */
    var c = {
        index:    "1866",
        v:        "1867",
        pk:       "1868",
        category: "1869",
        club:     "1880",
        top:      "1871",
        user:     "1872",
        my:       "1873"
    };
    var code = c[module] ? c[module] : c["index"];
    Nova.addScript("http://smarttrade.allyes.com/main/adftrack?db=smarttrade&point="+code+"&username=&order=&cash=&js=on&cache="+Nova.uid('YoQooRND')+"&memo=");

}

function run_cpa(action) {
    if (!window.cpa_log) {
    	setTimeout("run_cpa('"+action+"');", 500);
        return;
    }
        var uid = Nova.Cookie.get('uid');
	if(!action){
		action = 'view';
	}
	cpa_log(action,uid);
}

function run_google() {
    if (!window.urchinTracker) {
        setTimeout(run_google, 500);
        return;
    }
    _uacct = "UA-455269-1";
    urchinTracker();
}

function sendPkLink(pkId){
	var url = '/my/sendlink?obj=pk&url='+encodeURI('http://www.yoqoo.com/pk/pk/id/'+pkId);
	MM_openBrWindow(url,'','scrollbars=yes,width=650,height=350,resizable=yes');
}
function sendClubLink(clubId){
	var url = '/my/sendlink?obj=club&url='+encodeURI('http://www.yoqoo.com/club/id/'+clubId);
	MM_openBrWindow(url,'','scrollbars=yes,width=650,height=350,resizable=yes');
}
function sendPkVideoLink(pkId,vid,fileId){
	var url = '/my/sendlink?obj=v&vid='+vid+'&fid='+fileId+'&url='+encodeURI('http://www.yoqoo.com/v/show/module/pk/pkid/'+pkId+'/id/'+vid);
	MM_openBrWindow(url,'','scrollbars=yes,width=650,height=350,resizable=yes');
}
function sendVideoLink(videoId,fileId){
	var url = '/my/sendlink?obj=v&vid='+videoId+'&fid='+fileId+'&url='+encodeURI('http://www.yoqoo.com/v/show/id/'+videoId);
	MM_openBrWindow(url,'','scrollbars=yes,width=650,height=350,resizable=yes');
}

