﻿var dictHP = {
  clicktoread : "Click to read the full story",
  fadeinterval : 60
};


var hpFlip = {
    current:null,
    next:null,
    hpf:[],
    dir:'forwards',
    fading:true, //smooth fading transitions
    automatic:true, //automatically fade
    controls:false, //make forward/backward controls
    mouseover:true, //override on link mouseover
    parentdiv:'homeContentMain', //the div with all the images in
    classname:'hpnewsimage', //the classname of all the images
    linkclass:'button', //the class for an inactive link
    linkactiveclass:'buttonactive', //class for an active link
    titlesdiv:'newsmenu', //the div with the links in
    restartIn:4000, // how long to wait for automatic restart
    interval:false, // internal: interval timer between transitions
    countdown:false, // internal: countdown timer to restart    
    progbar:null,
    progcount:0,
    cl:null,
    prephpimgs:function(){
        var hptf = endava.$(hpFlip.parentdiv); // was hardcoded
        if (hptf) {
            var d = hptf.getElementsByTagName('DIV');
            if (d) {
                for (var i=0; i<d.length; i++) {
                    if (d[i].className==hpFlip.classname) { // was hardcoded id.indexOf...
                        d[i].style.display = 'none';
                        d[i].xO = 0; //opacity
                        if (hpFlip.fading) hpFlip.setOpacity(d[i]);
                        hpFlip.hpf.push(d[i]);
                    }
                }
            }
            if (hpFlip.hpf.length>0) {
                hpFlip.hpf[0].style.display = 'block';
                hpFlip.hpf[0].xO = 1;
                if (hpFlip.fading) hpFlip.setOpacity(hpFlip.hpf[0]);
                hpFlip.current = 0;
                hpFlip.next = 1;
                hpFlip.makecontrols();
            }
            if (hpFlip.automatic) {
                hpFlip.interval = setInterval(hpFlip.progress,dictHP.fadeinterval);
            }
        }
    },
    restart:function(){
        hpFlip.dir = 'forwards';
        hpFlip.automatic=true;
        if (hpFlip.pause()) {
            if (hpFlip.countdown) clearInterval(hpFlip.countdown);
            hpFlip.countdown = setTimeout(hpFlip.restart,hpFlip.restartIn);
        } else {
            hpFlip.interval = setInterval(hpFlip.progress,dictHP.fadeinterval);
        }
    },
    pause:function(){
        var dd = hpFlip.hpf[hpFlip.current].getElementsByTagName('DIV')[0];
        if (dd && dd.id.indexOf('hpvid')>-1) {
             var vid = window['vid_'+dd.id].getState();
            if (vid > -1 && vid < 6){
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    },
    setOpacity:function(obj) {
	    if(obj.xO>.99) {
		    obj.xO = 1;
	    }
	    if(obj.xO<0) {
		    obj.xO = 0;
	    }
	    obj.style.opacity = obj.xO;
	    obj.style.filter = "alpha(opacity=" + (obj.xO*100) + ")";
	    if(obj.xO==1) {
		    return;
	    }
    },
    fade:function(){
		var cO = hpFlip.hpf[hpFlip.current].xO;
		var nO = hpFlip.hpf[hpFlip.next].xO;
		cO-=.1; 
		nO+=.1;
		hpFlip.hpf[hpFlip.next].style.display = "block";
		hpFlip.hpf[hpFlip.current].xO = cO;
		hpFlip.hpf[hpFlip.next].xO = nO;
		hpFlip.setOpacity(hpFlip.hpf[hpFlip.current]); 
		hpFlip.setOpacity(hpFlip.hpf[hpFlip.next]);
		if(cO<=0) {	
			hpFlip.hide(hpFlip.hpf[hpFlip.current]);
			hpFlip.show(hpFlip.hpf[hpFlip.next]);
			hpFlip.current = hpFlip.next;
			if (hpFlip.controls) {
			    endava.$('hpcontroltext').innerHTML = parseInt(1+hpFlip.current)+' of '+hpFlip.hpf.length;
			}
		} else {
			setTimeout(hpFlip.fade,50);
		}
    },
    hide:function(d){
        d.xO = 0;
        d.style.opacity = d.xO;
	    d.style.filter = "alpha(opacity=" + (d.xO*100) + ")";
	    d.style.display = 'none';
    },
    show:function(d){
        d.xO = 1;
        d.style.opacity = d.xO;
	    d.style.filter = "alpha(opacity=" + (d.xO*100) + ")";
        var dd = d.getElementsByTagName('DIV')[0];
        if (dd && dd.id.indexOf('hpvid')>-1) {
            window['vid_'+dd.id].unload(); // override auto-load of video object
        }
        d.style.display = 'block';
    },
    flip:function() {
        if (hpFlip.pause() && hpFlip.automatic) {
            if (hpFlip.countdown) clearInterval(hpFlip.countdown);
            hpFlip.countdown = setTimeout(hpFlip.restart,hpFlip.restartIn);
            if (hpFlip.interval) clearInterval(hpFlip.interval);
        } else {
            hpFlip.next = hpFlip.dir == 'forwards' ? hpFlip.current+1 : hpFlip.current-1;
            if (arguments.length>0) {
                hpFlip.next = parseInt(arguments[0]);
            } // override if a number is specified

            var dd = hpFlip.hpf[hpFlip.current].getElementsByTagName('DIV')[0];
            if (dd && dd.id.indexOf('hpvid')>-1) { //(notation: http://www.jibbering.com/faq/faq_notes/square_brackets.html)
                window['vid_'+dd.id].unload(); 
            }
            if (hpFlip.next<0) {
                hpFlip.next = hpFlip.hpf.length-1;
            } else if (hpFlip.next > hpFlip.hpf.length-1) {
                hpFlip.next = 0;
            }
            if (hpFlip.cl){//the caption divs.
                hpFlip.cl[hpFlip.current].className=hpFlip.linkclass;
		        hpFlip.cl[hpFlip.next].className=hpFlip.linkactiveclass;
            }
            if (hpFlip.fading) { 
                hpFlip.fade(hpFlip.current,hpFlip.next);
            } else {
                hpFlip.hide(hpFlip.hpf[hpFlip.current]);
                hpFlip.show(hpFlip.hpf[hpFlip.next]);
                hpFlip.current = hpFlip.next;
                if (hpFlip.controls) {
                    endava.$('hpcontroltext').innerHTML = parseInt(1+hpFlip.current)+' of '+hpFlip.hpf.length;
                }
            }
        }
    },
    progress:function() {
	    hpFlip.progcount = hpFlip.progcount +1
	    if (hpFlip.progcount > 85) { 
		    hpFlip.progcount=0; 
		    hpFlip.flip();
	    }
	    if (hpFlip.controls) {
	        hpFlip.progbar.style.width = hpFlip.progcount+'px' ;
	    }
    },
    makecontrols:function() {
    	var nt = endava.$(hpFlip.titlesdiv);
	    hpFlip.cl = nt.getElementsByTagName('A');
        for (var xx=0; xx<hpFlip.cl.length; xx++) {
	            hpFlip.cl[xx].className=hpFlip.linkclass;		
            }
        hpFlip.cl[0].className=hpFlip.linkactiveclass;
	    if (hpFlip.mouseover) {
	        for (var xx=0; xx<hpFlip.cl.length; xx++) {
	            hpFlip.cl[xx].id='newsbutton'+xx;
                hpFlip.cl[xx].onmouseover = function() {
                    hpFlip.fading = false;
                    hpFlip.automatic=false;
                    hpFlip.progcount=85;
                    if (hpFlip.interval) clearInterval(hpFlip.interval);
                    if (hpFlip.countdown) clearInterval(hpFlip.countdown);
                    hpFlip.flip(this.id.replace(/[^0-9]/g, ''));				
		            return false;
	            };
	            hpFlip.cl[xx].onmouseout = function() {
	                hpFlip.fading = true;
	                hpFlip.countdown = setTimeout(hpFlip.restart,hpFlip.restartIn);
	            };
            }
	    }
	    if (hpFlip.controls) {
	        var cts = document.createElement('div');
	        cts.id = 'hpcontrols';//for css?
	        nt.appendChild(cts);
	        hpFlip.progbar = document.createElement('span');
	        hpFlip.progbar.id = 'progbar';
	        hpFlip.progbar.innerHTML = '<!-- - -->'; // don't want a self-closed div
	        cts.appendChild(hpFlip.progbar);
            for (var xx=0; xx<hpFlip.cl.length; xx++) {
	            hpFlip.cl[xx].id='currentimagelink'+xx;
	            hpFlip.cl[xx].title=dictHP.clicktoread;
	            hpFlip.cl[xx].innerHTML += '<span>'+dictHP.clicktoread+'</span>';
            }
            var cttext = document.createElement('div');
	        cttext.id = 'hpcontroltext';
	        cttext.innerHTML = '1 of '+hpFlip.hpf.length;
            cts.appendChild(cttext);
            var ctLeft = document.createElement('a');
	        var ctRight = document.createElement('a');
	        ctLeft.href='#';
	        ctRight.href='#';
	        ctLeft.id='hpcontrol_left';//for css?
	        ctRight.id='hpcontrol_right';//for css?
	        cts.appendChild(ctLeft);
	        cts.appendChild(ctRight);	
            ctLeft.onclick = function() {
                hpFlip.automatic=false;
                hpFlip.progcount=85;
                hpFlip.progbar.style.width='85px';
                if (hpFlip.interval) clearInterval(hpFlip.interval);
                if (hpFlip.countdown) clearInterval(hpFlip.countdown);
                hpFlip.countdown = setTimeout(hpFlip.restart,hpFlip.restartIn);
                hpFlip.dir = 'backwards';
                hpFlip.flip();				
		        return false;
	        }
            ctRight.onclick = function() {
                hpFlip.automatic=false;
                hpFlip.progcount=85;
                hpFlip.progbar.style.width='85px';
                if (hpFlip.interval) clearInterval(hpFlip.interval); //stop auto for now
                if (hpFlip.countdown) clearInterval(hpFlip.countdown);
                hpFlip.countdown = setTimeout(hpFlip.restart,hpFlip.restartIn); //but start after a period of inactivity
                hpFlip.dir = 'forwards';
                hpFlip.flip();	
		        return false;
	        }
	    }
    }  
};

var endava = {
    $:function(e){
        return document.getElementById(e);
    },
    Load:function(f) { 
	    var o = window.onload; 
		    if (typeof window.onload != 'function') { 
			    window.onload = f; 
		    } else { 
			    window.onload = function() { 
				    if (o) { 
				    o(); 
			    } 
			    f(); 
		    } 
	    } 
    },
    DOMLoad:function(){var e=[],t,s,n,i,o,d=document,w=window,r='readyState',c='onreadystatechange',x=function(){n=1;clearInterval(t);while(i=e.shift())i();if(s)s[c]=''};return function(f){if(n)return f();if(!e[0]){d.addEventListener&&d.addEventListener("DOMContentLoaded",x,false);/*@cc_on@*//*@if(@_win32)d.write("<script id=__ie_onload defer src=//0><\/scr"+"ipt>");s=d.getElementById("__ie_onload");s[c]=function(){s[r]=="complete"&&x()};/*@end@*/if(/WebKit/i.test(navigator.userAgent))t=setInterval(function(){/loaded|complete/.test(d[r])&&x()},10);o=w.onload;w.onload=function(){x();o&&o()}}e.push(f)}}(),
    video:function(div) {
        if (arguments.length>1){
            flowplayer(div, "/styles/flowplayer/flowplayer-3.1.3.swf", {
                key: '@b1fce84f6f93e8f6d5d', 
                plugins: {  
                    controls: {
                        url: '/styles/flowplayer/flowplayer.controls-3.1.3.swf',
                        autoHide: 'always',
                        stop: true,
                        backgroundColor: 'transparent' 
                    },
                    rtmp: {
                        url: '/styles/flowplayer/flowplayer.rtmp-3.1.2.swf',
                        netConnectionUrl: 'rtmp://cp78683.live.edgefcs.net/live/',
                        subscribe: true
                    }
                }, 
                clip: {
                    provider: 'rtmp',
                    live: true
                }
            });
        } else {
            flowplayer(div, "/styles/flowplayer/flowplayer-3.1.3.swf", {
                key: '@b1fce84f6f93e8f6d5d', 
                plugins: {  
                    controls: {
                        url: '/styles/flowplayer/flowplayer.controls-3.1.3.swf',
                        autoHide: 'always',
                        stop: true,
                        backgroundColor: 'transparent' 
                    },
                    akamai: { 
                        url: '/styles/flowplayer/flowplayer.akamai-3.1.4-dev.swf'
                    },
                    rtmp: {
                        url: '/styles/flowplayer/flowplayer.rtmp-3.1.2.swf'
                    }
                }, 
                clip: {
                    provider: 'rtmp'
                }
            });
        }
    },
    downloadUrl:function(url, func) {
    /*
	    This function replicates the google map GDownloadURL functionality for ajax file download.
	    url    : Call url
	    func   : custom function which is used to process returned data
    */
	    var httpObj=null;     
	    if (window.XMLHttpRequest) { // proper browser
		    httpObj=new XMLHttpRequest();
	    }
	    else if (window.ActiveXObject) { // IE6 and older
		    httpObj=new ActiveXObject("Microsoft.XMLHTTP");
	    }
	    if (httpObj!=null){
		    httpObj.open('GET', url, true);
		    httpObj.onreadystatechange = function() {
			    if(httpObj.readyState == 4){ // 'loaded'
				    if (httpObj.status == 200) { // 'OK'
					    var contenttype = httpObj.getResponseHeader('Content-Type');
					    if (contenttype.indexOf('xml')>-1) {
						    func(httpObj.responseXML);
					    } else {
						    func(httpObj.responseText);
					    }
				    } else {
					    func('Error: '+httpObj.status);
			       }
			    }
		    };
		    httpObj.send(null);
	    } else {
		    // browser cannot do ajax.
	    }
    }
};

leaderboard = {
    init:function() {
        var div = endava.$('minilbtop');
        if (div) {
            var table = div.getElementsByTagName('TABLE')[0];
            var URL = table.getAttribute('summary');
            if (URL) {
                //leaderboard.fetch(URL);
                var mlb = setInterval('leaderboard.fetch(\''+URL+'\')',120000); // 2 minutes
            }
        }
    },
    fetch:function(url) {
        endava.downloadUrl(url, leaderboard.update);
    },
    update:function(xml) {
        var div = endava.$('minilbtop');
        if (div && typeof(xml)!='string') { // 'string' indicates an error message. 'object' is some xml
            var tbody = div.getElementsByTagName('TBODY')[0];
            if (tbody) {
                var r = tbody.rows;
                var i = xml.getElementsByTagName('item');
                if (i.length>4){
                    for (var x=0; x<5; x++) {
                        r[x].cells[0].innerHTML = i[x].getAttribute('position');
                        r[x].cells[1].innerHTML = '<img src="http://www.golfbox.dk/images/tournament/flags/'+
                            i[x].getAttribute('countryshort') + 
                            '.gif" alt="" />';
                        var l = i[x].getElementsByTagName('lastname')[0];
                        var p = i[x].getElementsByTagName('propername')[0];   
                        var ln = (l.textContent) ? l.textContent : l.text; //ie needs 'text' everything else needs 'textContent'.
                        var pn = (p.textContent) ? p.textContent : p.text;
                        r[x].cells[2].innerHTML = ln;
                        r[x].cells[2].title = pn;
                        r[x].cells[3].innerHTML = i[x].getAttribute('holesplayed');
                        r[x].cells[4].innerHTML = i[x].getAttribute('TotalScore');
                        var par = i[x].getAttribute('TotalScore').substring(0,1);
                        if (par =='+') {r[x].cells[4].className='over'}
                        else if (par =='-') {r[x].cells[4].className='under'}
                        else {r[x].cells[4].className='par'}
                    }
                }
            }
        }
    }
};

endava.DOMLoad(hpFlip.prephpimgs);
endava.DOMLoad(leaderboard.init);
