var NS5=(navigator.userAgent.indexOf("Gecko")>=0);
if(NS5) document.onmousedown=function(e) {if(e.target.tagName!='A') return false;}
else document.onselectstart=function(){return false;}
document.oncontextmenu=function(){return false;}

var IsLogged=IsLocked=0;
var iPar,NSWindow=null;;
var PList=new Array();
var cooName='';

window.onload=function() {
    var f,t,aTags;
    if(window.parent==window) window.location.href=URL;
    else
    if(!document.referrer.length || !ref.test(document.referrer)) window.location.href=URL;
    else {
        aTags=document.getElementsByTagName('A');
        for(t=0; t<aTags.length; ++t) {
            if(Period==1 && t>1) aTags[t].innerHTML=myDayOfWeek(t)+aTags[t].innerHTML;
            aTags[t].onclick=LaunchPuzzle;
            if(COpt&4) PList[t]=new PResult(myDateStamp(t));
            }
        iPar=new WebPuzzleParameters(InterfaceID,CID,(COpt&2)?(POpt|8):POpt,PTime,SubType,'');
        if((COpt&4) && navigator.cookieEnabled){
            cooName="PM_"+InterfaceID.toString();
            if((str=Get_cookie(cooName))) SetPuzzleList(str);
            }
        }
    if(NS5) window.addEventListener('unload',function() {if(NSWindow && !NSWindow.closed) NSWindow.abandon();},true);        
    }

function PResult(d) {
    this.DStamp=d;
    this.Res='';
    this.toString=function() {return this.DStamp+':'+this.Res;}
    }
    
var WDays=Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
function myDayOfWeek(t) {
var day;
    day=new Date().getDay()-t;
    if(day<0) day+=7;
    return WDays[day];
    }

function myDateStamp(t) {
var d,day,dm;
    d=new Date();
    dm=d.getDate();
    if(Period==1) {if(t) d.setDate(dm-t);}
    else {
        day=d.getDay();
        if(day!=1) dm-=((day)?(day-1):6);
        dm-=(t*7);
        d.setDate(dm);
        }
    return d.getFullYear().toString()+(d.getMonth() + 1).toString()+d.getDate().toString();
    }

function WebPuzzleParameters(iid,cid,opt,tmo,sbt,u) {
    this.InterfaceID=iid;
    this.CustID=cid;
    this.DOffset=0;
    this.POpt=opt;
    this.PTime=tmo;
    this.SubType=sbt;
    this.Saved='0';
    this.User=u;
    }

function LaunchPuzzle(e) {
var s,sc,w,h,url;
    if(NS5) {e.preventDefault(); s=e.target;} else {event.returnValue=false; s=event.srcElement;}
    if(NS5 && NSWindow && !NSWindow.closed) return;
    while(s.tagName!='A') s=s.parentNode;
    if(!(POpt&4) || (IsLogged && !IsLocked)) {
        iPar.DOffset=parseInt(s.getAttribute("id"),10);
//        iPar.Saved=(s.parentNode.className.indexOf('saved')>=0)?'1':'0';
	    if((w=window.screen.width)>1000) w=1000;
	    if((h=window.screen.height)>800) h=720;
        url='SynPuzzle.htm';
        if(NS5) {
            if(!NSWindow || NSWindow.closed)
                try {
                    NSWindow=window.open(url,'ExtWindow','width='+w.toString()+',height='+h.toString()+',left=10,top=5,resizable=yes,scrollbars=no,status=no,dependent=yes,modal=yes');
                    }
                catch(ex) {NSWindow=null;}
            if(NSWindow) NSWindow.focus();
            else alert('In order to play our puzzles, please enable pop-ups for www.pmlsvc.com');
	        }
        else 
        if(showModalDialog) {
            if(navigator.userAgent.indexOf("MSIE 9")>=0) url='SynPuzzle_IE9.htm';
            var sFeatures="dialogHeight: " + h.toString() + "px; dialogWidth:"+w.toString()+"px; scroll:no; status:no; center: yes;"
            if((sc=showModalDialog(url,iPar,sFeatures)).length) ProcessPuzzleReturn(sc);
            }
        }
    s.blur();
    return false;
    }


function ProcessPuzzleReturn(r) {
var f,c,s,n,st,pID;
    c=parseInt(r.substr(0,1),10);
    if(!c) alert('The puzzle is currently anavailable.\nTry again later.');
    else
//    if(c==3) {
//        if(iPar.Saved=='0') {
//            f=document.getElementById(iPar.PuzzleID.toString());
//            f.parentNode.className="pzlitem saved";
//            f.parentNode.title='Saved';
//            }
//        }
//    else
    if(c!=5)
        if((f=document.getElementById(iPar.DOffset.toString())).parentNode.className.indexOf('solved')==-1) {
            s=r.split(':');
            switch(c) {
                case 1:
                    n=parseInt(s[1],10);
                    PList[iPar.DOffset].Res=s[0]+':'+s[1]+':'+s[2];
                    st='Time: '+Math.floor(n/60).toString()+'m '+(n%60)+'s'
                    n=parseInt(s[2],10);
                    if(n) st+=('&nbsp;&bull;&nbsp;Revealed: '+n.toString());
                    f.nextSibling.innerHTML=st;
                    f.parentNode.className="pzlitem solved";
                    f.parentNode.title='Solved';
                    break;
                case 2:
                    f.parentNode.className="pzlitem failed";
                    f.parentNode.title='Failed to solve';
                    PList[iPar.DOffset].Res=s[0]+'::';
                    break;
                case 4:
                    f.parentNode.className="pzlitem disqual";
                    f.parentNode.title='Disqualified';
                    PList[iPar.DOffset].Res=s[0]+'::';
                    break;
                }
            SaveResults();
            }
    }

function SetPuzzleList(str) {
var ss,i,j,f,s,n,st;
    if(!str.length) return;
    ss=str.split('|');
    for(i=0; i<ss.length; ++i) {
        s=ss[i].split(':');
        for(j=0; j<PList.length; ++j)
            if(s[0]==PList[j].DStamp) {
                PList[j].Res=s[1]+':'+s[2]+':'+s[3];
                break;
                }
        if(j==PList.length) continue;
        if((f=document.getElementById(j.toString()))){
            switch (s[1]) {
                case '1': 
                    n=parseInt(s[2],10);
                    st='Time: '+Math.floor(n/60).toString()+'m '+(n%60)+'s'
                    n=parseInt(s[3],10);
                    if(n) st+=('&nbsp;&bull;&nbsp;Revealed: '+n.toString());
                    f.nextSibling.innerHTML=st;
                    f.parentNode.className+=" solved";
                    f.parentNode.title='Solved';
                    break;
                case '2': 
                    f.parentNode.className+=" failed";
                    f.parentNode.title='Failed to solve';
                    break;
                case '4': 
                    f.parentNode.className+=" disqual";
                    f.parentNode.title='Disqualified';
                    break;
                }
            }
        }
    SaveResults();
    }
    
function SaveResults() {
var i,s='';
    if(!navigator.cookieEnabled) return;
    for(i=0; i<PList.length; ++i)
        if(PList[i].Res.length) s+=(((s.length)?'|':'')+PList[i].toString());
    if(s.length) Set_cookie(cooName,s);
    else         Delete_cookie(cooName);
    }

function Set_cookie(cookie_name,cookie_value) {
var eDate = new Date();
    eDate.setMonth(eDate.getMonth()+1);
    document.cookie=cookie_name+"="+cookie_value+"; expires=" + eDate.toGMTString() + ";path=/";
    }

function Get_cookie(cookie_name) {
var r;
    r=document.cookie.match ('(^|;) ?'+cookie_name+'=([^;]*)(;|$)');
    return(r)?r[2]:null;
    }

function Delete_cookie(cookie_name) {
var eDate=new Date();
    eDate.setTime(eDate.getTime()-1);
    document.cookie=cookie_name+="=; expires=" + eDate.toGMTString();
    }

