var YCM = YAHOO.util.Connect;
var Dom = YAHOO.util.Dom;

function setRadioPlay(play){
    YAHOO.util.Cookie.set("radioPlay", play,{
        expires: new Date("January 12, 2025"),
        path:"/"
    });
}

function getRadioPlay(){
    return YAHOO.util.Cookie.get("radioPlay");
}

function radioReady(){
    if (getRadioPlay() == "true" || getRadioPlay() == "" || getRadioPlay() == null){
        document.getElementById("radioplaceholder").startPlay();
    }
}

function heartbeat(){
    var postData = '';
    var callback = {
        success : function(o) {
            checkRequestLogout(o.responseText);

        
            setTimeout(function(){
                heartbeat();
            }, 3*60*1000);
        },
        failure : function(o) {
            if (o.status == 503){
                window.location.href = "/";
            }else{
                setTimeout(function(){
                    heartbeat();
                }, 3*60*1000);
            }
        }
    }
    YAHOO.util.Connect.asyncRequest('POST', "heartbeat.do?decorator=blank&confirm=true",
            callback, postData);
}
    
function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
      var pair = vars[i].split("=");
      if (pair[0] == variable) {
        return pair[1];
      }
    }
  } 

function changeLang(lang){
    var newLang;
	if (lang.value == null){
	    newLang = lang;
	}else{
	    newLang = lang.value;  
	}
    if (window.location.pathname.toString().indexOf(".do") > -1){
        if (window.location.search==""){
            window.location.href=location+"?request_locale="+newLang;
        }else{
          if (window.location.search.toString().indexOf("request_locale") > -1){
        	  var search = window.location.search.toString();
        	  if (search.lastIndexOf("#") > -1){
        		   search = search.subStr(0, search.lastIndexOf("#")-1);   
              }
        	  var newsearch = search.replace('request_locale='+currentLocale, 'request_locale='+newLang);
        	  var href = window.location.href.toString().substring(0,window.location.href.toString().indexOf("?")); 
        	  window.location.href=href+newsearch;
          }else{
        	  window.location.href=location+"&request_locale="+newLang;
          }
        }
    }
}

function closeAdv(){
    try{
		  var div = document.getElementById("googleSquareAdv");
		  var table =  document.getElementById("googleAdvPlaceHolder");
		  div.style.display = "none";
		  document.body.removeChild(div);
    }catch (e){
    }
  
}

function printMe(){
	var printLocation = "";
	if (window.location.search==""){
	   printLocation = location+"?decorator=print&confirm=true";
	}else{
	   printLocation = location+"&decorator=print&confirm=true";;
	}
	
    window.open(printLocation,'_print','scrollbars=1,toolbar=0,resizable=1,status=1,menubar=1');
}


var passwordDefaultText = "";
function passwordFocus(){
    document.getElementById("login_password_id").style.display = "none";
    document.getElementById("login_password").style.display = "inline";
    document.getElementById("login_password").focus();
    document.getElementById("login_password").onblur = function(){
       if (this.value == ""){
            document.getElementById("login_password_id").style.display = "inline";
            document.getElementById("login_password").style.display = "none";
       }
    }
    

/*
	passwordDefaultText = document.getElementById("login_password").value;
	document.getElementById("login_password").value = "";
	document.getElementById("login_password").className = "text";
	document.getElementById("login_password").type = "password";
*/	
}
var loginDefaultText = "";
function loginFocus(){
	if (document.getElementById("login_loginName").value != ""){
	    loginDefaultText = document.getElementById("login_loginName").value;
		document.getElementById("login_loginName").value = "";
		document.getElementById("login_loginName").onblur = function(){
	       if (this.value == ""){
	           this.value = loginDefaultText;
	       }
		}
	}
}

function checkRequestLogout(checktext){
    try{
	    if (checktext.indexOf("####logout####")>-1){
	        var message="";
	        if (checktext.indexOf("####duplogin 1####")>-1){
	            message="100";    
	        }
	        
	        if (message != ''){
	            top.location.href="../home/index.do?e=10&c="+message;
	        }else{
	            top.location.href="../home/index.do";
	        }
	    }
	}catch (e){
	}
    
}

function loginAction(){

	var login = document.getElementById("login_loginName").value;
	var password = document.getElementById("login_password").value;
	var postData = 'loginName='+login+"&password="+password;
	var callback = {
		success : function(o) {
			try{
				
				var loginJson = YAHOO.lang.JSON.parse(o.responseText);
				if (loginJson.status == "0"){
				    if (window.location.href.indexOf("register.do")>-1){
				        window.location.href = "../home/index.do";
				    }else{
				        var newRedirect = "";
				        if (window.location.href.toString().indexOf("e=10")){
				            newRedirect = window.location.href.toString().replace("e=10","");
				        }
				    
                        if (newRedirect.indexOf("?") > -1){
                            window.location.href = newRedirect+"&lm=true";
                        }else{
                            window.location.href = newRedirect+"?lm=true";
                        }
				        
				    }

				}else{
//				    alert(bundle.loginincorrect);
					document.getElementById("login_password").value="";
					if (currentLocale.indexOf("zh")>-1){
						document.getElementById("login_password").className = "password-zh";
					}else{
						document.getElementById("login_password").className = "password-en";
					}

				    if (document.body.className.indexOf("yui-skin-sam") == -1){
				        document.body.className = document.body.className + " yui-skin-sam";
				    }

					var loginErrorDialog = 
					    new YAHOO.widget.SimpleDialog("loginErrorDialog", 
					             { width: "300px",
					               fixedcenter: true,
					               modal:true,
					               visible: false,
					               draggable: false,
					               close: true,
					               text: "<b>"+bundle.loginerror+"</b>",
					               icon: YAHOO.widget.SimpleDialog.ICON_WARN,
					               constraintoviewport: true,
					               buttons: [ { text:bundle.ok, handler:function(){this.hide();}, isDefault:true } ]
					             } );
					loginErrorDialog.setHeader(bundle.loginerror);
				    loginErrorDialog.render(document.body);
				    loginErrorDialog.show();
				}
			}catch (e){
			}
		},
		failure : function(o) {
		}
	}
	
	
//	YAHOO.util.Connect.transport("../jscripts/connection.swf");
//	YCM.asyncRequest('POST', "http://member.ma288.com/has/home/login.do",
//			callback, postData);	
    YCM.asyncRequest('POST', "/has/home/login.do",
            callback, postData);
}

function showLoginMessage(){
    if (document.body.className.indexOf("yui-skin-sam") == -1){
        document.body.className = document.body.className + " yui-skin-sam";
    }
    
        var callback = {
        success : function(o) {
            try{
/*			    var loginMessage = new YAHOO.widget.Panel("loginMessage", {
			          width:"320px", visible:true, x:660, y:150,draggable:false
			          } );
			//    loginMessage.setHeader("hello");
			    loginMessage.setBody(o.responseText);
			    loginMessage.render(document.body);
			    loginMessage.show();

                setTimeout(function(){
                    if (loginMessage != null){
                        loginMessage.destroy();
                    }
                }, 20*1000);*/
                
                    var loginErrorDialog = 
                        new YAHOO.widget.SimpleDialog("loginErrorDialog", 
                                 { width: "400px",
                                   fixedcenter: true,
                                   modal:true,
                                   visible: false,
                                   draggable: false,
                                   close: true,
                                   text: o.responseText,
                                   icon: YAHOO.widget.SimpleDialog.ICON_INFO,
                                   constraintoviewport: true,
                                   buttons: [ { text:bundle.ok, handler:function(){this.hide();}, isDefault:true } ]
                                 } );
                    loginErrorDialog.setHeader(bundle.loginsuccess);
                    loginErrorDialog.render(document.body);
                    loginErrorDialog.show();
                    
                    setTimeout(function(){
	                    if (loginErrorDialog != null){
	                        loginErrorDialog.hide();
	                    }
                    }, 20*1000);                
                
            }catch (e){
            }
        },
        failure : function(o) {
        }
    }
    
    YAHOO.util.Connect.asyncRequest('POST', "getMembershipStatus.do", callback, "decorator=blank&confirm=true");    
}

function showErrorMessage(errorMsg){
    if (errorMsg == "10"){
        var errorMessage = bundle.duplogin;

        if (document.body.className.indexOf("yui-skin-sam") == -1){
            document.body.className = document.body.className + " yui-skin-sam";
        }

        var errorDialog = 
            new YAHOO.widget.SimpleDialog("errorDialog", 
                     { width: "300px",
                       fixedcenter: true,
                       modal:true,
                       visible: false,
                       draggable: false,
                       close: true,
                       text: "<b>"+errorMessage+"</b>",
                       icon: YAHOO.widget.SimpleDialog.ICON_WARN,
                       constraintoviewport: true,
                       buttons: [ { text:bundle.ok, handler:function(){this.hide();}, isDefault:true } ]
                     } );
        errorDialog.setHeader(errorMessage);
        errorDialog.render(document.body);
        errorDialog.show();


    }
}

function showMemRechargeLog(memId){
    if (document.body.className.indexOf("yui-skin-sam") == -1){
        document.body.className = document.body.className + " yui-skin-sam";
    }
    
        var callback = {
        success : function(o) {
            try{
                    var loginErrorDialog = 
                        new YAHOO.widget.SimpleDialog("loginErrorDialog", 
                                 { width: "500px",
                                   fixedcenter: true,
                                   modal:true,
                                   visible: false,
                                   draggable: false,
                                   close: true,
                                   text: o.responseText,
                                   icon: YAHOO.widget.SimpleDialog.ICON_INFO,
                                   constraintoviewport: true,
                                   buttons: [ { text:bundle.ok, handler:function(){this.hide();}, isDefault:true } ]
                                 } );
                    loginErrorDialog.setHeader(bundle.rechargelog);
                    loginErrorDialog.render(document.body);
                    loginErrorDialog.show();
                    
//                    setTimeout(function(){
//	                    if (loginErrorDialog != null){
//	                        loginErrorDialog.hide();
//	                    }
//                    }, 20*1000);                
                
            }catch (e){
            }
        },
        failure : function(o) {
        }
    }
    
    YAHOO.util.Connect.asyncRequest('POST', "getMemRechargeLog.do?sonId="+memId, callback, "decorator=blank&confirm=true");    
}

function compareFinishingTime(raceNo, raceDate, raceId){
    if (pmem == 1){
        window.open('showRaceCardSpeeding.do?ordering=o.finishingTime&raceId='+raceId+'&raceDate='+raceDate,'BestTime','scrollbars=1,toolbar=0,resizable=1,status=1,menubar=1');
    }else{
        window.location.href = "../home/register.do";
    }
    
}

function openDefaultSpeeding(){
    if (pmem == 1){
        window.open('../raceCard/showRaceCardSpeeding.do', '_speedrating', 'width=1024, height=800,scrollbars=1,toolbar=0,resizable=1,status=1,menubar=1');
    }else{
        window.location.href = "../home/register.do";
    }
}

function openRaceCardPdf(date){
    window.open('http://www.hkjc.com/images/PDF/'+date+'_starter_all.pdf', '_pdfracecard', 'width=700, height=600');
}

function openOtherPayment(){
    window.open('../info/otherPayment.do?decorator=empty&confirm=true', '_otherpayment', 'width=500, height=400,scrollbars=1,toolbar=0,resizable=1,status=1,menubar=1')
}


var Menu = new function(){
    var currentSubmenu = null;

    function pageWidth() {return window.innerWidth != null? window.innerWidth: document.body != null? document.body.clientWidth:null;}

	this.changeMenu = function (o, submenu){
	    if (currentSubmenu != null){
	       currentSubmenu.style.display = "none";
	    }
	    
	    var submenu = document.getElementById(submenu);
        currentSubmenu = submenu;
        if (submenu != null){
	        if (Dom.getRegion(o).left > pageWidth()*2/3){
	            submenu.style.left = Dom.getRegion(o).right-200+"px";
	        }else{
	            submenu.style.left = Dom.getRegion(o).left+"px";
	        }     
	        submenu.style.marginTop = 34+"px";
//	        submenu.style.marginTop = Dom.getRegion(o).bottom+"px";
	        submenu.style.zIndex = 1000;
	        if (submenu != null){
	            submenu.style.display = "block";
	        }else{
	            document.getElementById("homeSubmenu").style.display = "block";
	        }
	        var nodes = YAHOO.util.Selector.query('#'+submenu.id + " li");
	        for (var i = 0; i < nodes.length; i ++){
	            nodes[i].onmouseover = function(){
	                submenu.style.display = "block";
	            }
	        }
	        var nodes = YAHOO.util.Selector.query('#'+submenu.id + " a");
	        for (var i = 0; i < nodes.length; i ++){
	            nodes[i].onmouseover = function(){
	                submenu.style.display = "block";
	            }
	            nodes[i].onclick = function(){
                    submenu.style.display = "block";
                }
	        }
	
	        submenu.onmouseout = function(){
	           submenu.style.display = "none";
	        }
        }
	    
	}

    this.hideAllSubmenu = function(){
	    var nodes = YAHOO.util.Selector.query("div.submenu-new");
	    for (var i = 0; i < nodes.length; i ++){
	        nodes[i].style.display = "none";
	    }

    }
 
} 

function checkBrowser(){
    if (YAHOO.env.ua.ie ==6 || YAHOO.env.ua.ie ==7) {
        Dom.get("browsernotsupported").style.display = "block";
    } 
}

YAHOO.util.Event.onDOMReady(function(){
    checkBrowser();
    YAHOO.util.Event.addListener(document, "click", function(){
        Menu.hideAllSubmenu();
    });
});


removeFromArray = function(array, from, to) {
	  var rest = array.slice((to || from) + 1 || array.length);
	  array.length = from < 0 ? array.length + from : from;
	  return array.push.apply(array, rest);
	};