var hjFURL = window.location.href;
var ns6 = document.getElementById && !document.all;

pathArray = location.href.split('/');
//baseUrl = pathArray[0] + '/' + pathArray[1] + '/' + pathArray[2] + '/';
//Hack 
baseUrl = "http://www.golfchannel.com/";

function OpenOrRedirect(url) {
    if (window.parent.opener != null && typeof window.parent.opener.closed != 'unknown' && !window.parent.opener.closed) {
        window.parent.opener.location = url;
        if (window.parent.opener.focus) { window.parent.opener.focus(); }
        self.focus();
    } else {
        var blah = window.open(url);
        self.parent.opener = blah;
        self.focus();
    }
}

function getQueryArgs(global) {
    var args = {};
    var loc = window.location.href;
    var q = loc.indexOf("?");
    if (q == -1) return false;
    loc = loc.substring(q + 1);
    var pairs = loc.split("&");
    for (var i = 0; i < pairs.length; i++) {
        //if (global) eval(pairs[i]);
        var keyval = pairs[i].split("=");
        args[keyval[0]] = unescape(keyval[1]);
    }
    return args;
}

function setCookie(name, value, expires) {
    var curCookie = name + '=' + value +
	   '; path=/' +
	   ((expires != '') ? ';expires=' + expires : '');
    document.cookie = curCookie;
}

function getCookie(name) {
    var prefix = name + '=';
    var cookieStartIndex = document.cookie.indexOf(prefix);
    if (cookieStartIndex == -1)
        return null;
    var cookieEndIndex = document.cookie.indexOf(';', cookieStartIndex + prefix.length);
    if (cookieEndIndex == -1)
        cookieEndIndex = document.cookie.length;
    return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

function AdPopUp(popurl, params, unload) {
    if (unload == 'True')
        window.onunload = function () { window.open(popurl, 'ad_popup', params); }
    else
        winpops = window.open(popurl, 'ad_popup', params);
}

function AdPopUnder(popurl, params, unload) {
    if (unload == 'True') {
        window.onunload = function () {
            winpops = window.open(popurl, 'ad_popunder', params);
            winpops.blur();
            window.focus();
        }
    } else {
        winpops = window.open(popurl, 'ad_popunder', params);
        winpops.blur();
        window.focus();
    }
}

function AdFloater(popurl, params) {

    var objAd = document.createElement('div');
    objAd.id = 'ad_floater';
    objAd.style.top = '140px';
    objAd.style.left = '130px';
    objAd.style.display = 'block';
    objAd.style.position = 'absolute';
    objAd.style.border = '1px solid #0054E3';
    objAd.style.background = '#0054E3';
    objAd.align = 'right';
    //border: 1px solid #0054E3; background: #0054E3;position:absolute; top: 140px; left: 130px; z-index:100;    

    objAd.style.zIndex = 100001;

    document.getElementsByTagName('body')[0].appendChild(objAd);

    objAd.innerHTML = '<a href="#" onClick="document.getElementById(\'ad_floater\').style.display=\'none\'; return false;"><font color=white style="text-decoration: none;"><b>Close</b>&nbsp;</font><img src="Components/Design/img/global/btn/btn-close-pop.gif" align="absmiddle" vspace=1 border=0></a><br><iframe style="background-color: #FFFFFF;" src="' + popurl + '" scrolling="no" marginheight="0" marginwidth="0" hspace="0" vspace="0" ' + params + '></iframe>';

}

function openInterstitial(url) {

    if (window.tPageRefresh) {
        window.clearTimeout(window.tPageRefresh);
    }

    var objAd;
    var posx = 0;
    var posy = 0;

    var scrolly = (document.documentElement) ? document.documentElement.scrollTop : window.pageYOffset
    var scrollx = (document.documentElement) ? document.documentElement.scrollLeft : window.pageXOffset

    var myWidth = 0, myHeight = 0;

    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }

    posy = scrolly + ((myHeight - 608) / 2);
    posx = scrollx + ((myWidth - 570) / 2);



    objAd = document.createElement('div');
    objAd.id = "interstitialAdContainer";
    objAd.style.top = (posy <= 0) ? posy + 80 + 'px' : posy + 0 + 'px';
    objAd.style.left = posx + 'px';
    objAd.style.display = 'block';
    objAd.style.position = 'absolute';



    objAd.style.zIndex = 200000;

    document.getElementsByTagName("body")[0].appendChild(objAd);
    grayOut(true);

    objAd.innerHTML = '<iframe allowtransparency="true" background-color="transparent" id="frmPhoto" name="frmPhoto" style="z-index:200000;" src="' + url + '" width=570 height=620 scrolling=no frameborder=0 marginheight="0" marginwidth="0" hspace="0" vspace="0"></iframe>';

}

function closeInterstitial() {
    /*if (window.tPageRefreshTime) {
    tPageRefresh = setTimeout('refresh()', window.tPageRefreshTime);
    }*/
    var objAd = document.getElementById('interstitialAdContainer');
    objAd.style.display = 'none';
    objAd.parentNode.removeChild(objAd);
    grayOut(false);
}

var interstitialQueue;
function process() {
    var hjtot;
    var hj;
    var page;

    if (getQueryArgs(true)['page'] != null) {
        page = getQueryArgs(true)['page'];
    } else {
        page = '200001';
    }

    //if (getCookie('TGC_Machine') == null)
    //    skipHJ = true;

    hjtot = getCookie('hjtot');
    if (hjtot != null && hjtot.indexOf('3904') > 0) {
        setCookie('hjtot', 'ct=1&exp=');
    }

    if (typeof (skipHJ) != 'undefined' && skipHJ == false) {
        if (hjtot == null || (hjtot.substring(hjtot.indexOf('ct=') + 3, hjtot.indexOf('&')) * 1) < 8) {
            //loop through applicable campaigns
            for (var i = 0; i < HJC.length; i++) {
                //check campaign specific cookie
                hj = getCookie('hj' + HJC[i][0]);

                if (HJC[i][7] != '' && !eval(HJC[i][7])) continue;
                /// <reference path="../../../AdInterstitial/inter.aspx" />

                //if (true) {
                if (hj == null || (hj.substring(hj.indexOf('ct=') + 3, hj.indexOf('&')) * 1) < (HJC[i][1] * 1)) {
                    if (HJC[i][13] == 'True') {
                        if (HJC[i][12] != '' || HJC[i][6] != '') {
                            AdFloater(baseUrl + 'AdInterstitial/pop.aspx?c=' + HJC[i][0] + '&s=' + HJC[i][10] + '&p=' + page, HJC[i][4]);
                        } else {
                            AdFloater(HJC[i][5], HJC[i][4]);
                        }
                    } else if (HJC[i][3] == 'True') {
                        if (HJC[i][12] != '' || HJC[i][6] != '') {
                            AdPopUp(baseUrl + 'AdInterstitial/pop.aspx?c=' + HJC[i][0] + '&s=' + HJC[i][10] + '&p=' + page, HJC[i][4], HJC[i][14]);
                        } else {
                            AdPopUp(HJC[i][5], HJC[i][4], HJC[i][14]);
                        }
                    } else if (HJC[i][11] == 'True') {
                        if (HJC[i][12] != '' || HJC[i][6] != '') {
                            AdPopUnder(baseUrl + 'AdInterstitial/pop.aspx?c=' + HJC[i][0] + '&s=' + HJC[i][10] + '&p=' + page, HJC[i][4], HJC[i][14]);
                        } else {
                            AdPopUnder(HJC[i][5], HJC[i][4], HJC[i][14]);
                        }
                    } else {
                        //on unload or load?
                        if (HJC[i][14] == 'True') {
                            document.onclick = function (e) {
                                var cur = ns6 ? e.target : event.srcElement;
                                if (cur.tagName.toUpperCase() == 'B' || cur.tagName.toUpperCase() == 'FONT' || cur.tagName.toUpperCase() == 'IMG') cur = ns6 ? cur.parentNode : cur.parentElement;
                                //					               if (cur.tagName.toUpperCase() == 'A' || (cur.tagName.toUpperCase() == 'INPUT' && cur.type == "button")) {
                                if (cur.tagName.toUpperCase() == 'A') {
                                    // if link is a popup window or is executing js don't bind onClick
                                    if ((cur.href.indexOf("#") == 0 && cur.href.length == 1) || cur.href.length == cur.href.indexOf("#") + 1 || cur.href.indexOf("javascript") > -1) {
                                        hjFURL = "#";
                                    } else {
                                        hjFURL = cur.href;
                                    }
                                }
                                //strip out any ajax calls
                                //if (hjFURL.indexOf(baseUrl) > -1 && document.location.href != hjFURL && hjFURL.indexOf("/ajax/") == -1) {
                                    openInterstitial(baseUrl + 'AdInterstitial/inter.aspx?oas=&c=' + HJC[i][0] + '&s=' + HJC[i][10] + '&p=' + page + '&next_url=' + escape(hjFURL));
                                    return false;
                                //}
                            }

                            // store interstitial argument to be called later if needed
                            interstitialQueue = baseUrl + 'AdInterstitial/inter.aspx?oas=&c=' + HJC[i][0] + '&s=' + HJC[i][10] + '&p=' + page + '&next_url=';

                        } else
                        //$(window).load(function () { 
                            window.onload = openitup;
                        function openitup() {
                            openInterstitial(baseUrl + 'AdInterstitial/inter.aspx?oas=&c=' + HJC[i][0] + '&s=' + HJC[i][10] + '&p=' + page + '&next_url=' + escape(hjFURL) + '&onload=true');
                        }
                        //});

                        //document.location.href = '/AdInt/inter.aspx?c=' + HJC[i][0] + '&s=' + HJC[i][10] + '&p=' + page + '&next_url=' + escape(document.location);
                    }

                    //set site wide cookie
                    var ct = 1;
                    var dt = new Date();
                    if (dt.getYear() < '1900') {
                        dt = new Date(dt.getYear() + 1900, dt.getMonth(), dt.getDate(), 24, 0, 0);
                    } else {
                        dt = new Date(dt.getYear(), dt.getMonth(), dt.getDate(), 24, 0, 0);
                    }
                    if (hjtot != null) {
                        ct = (hjtot.substring(hjtot.indexOf('ct=') + 3, hjtot.indexOf('&')) * 1) + 1;
                        dt = new Date(hjtot.substring(hjtot.indexOf('&exp=') + 5));
                    }
                    setCookie('hjtot', 'ct=' + ct + '&exp=' + dt.toString(), dt.toGMTString());

                    //set campaign specific cookie
                    ct = 1;
                    var dat = new Date();

                    if (HJC[i][2] == '') {
                        dat = new Date(dat.getTime() + 86400000);
                    } else {
                        dat = new Date(dat.getTime() + ((HJC[i][2] * 1) * 60000));
                    }

                    if (hj != null) {
                        ct = (hj.substring(hj.indexOf('ct=') + 3, hj.indexOf('&')) * 1) + 1;
                        dt = new Date(hj.substring(hj.indexOf('&exp=') + 5));
                    }
                    setCookie('hj' + HJC[i][0], 'ct=' + ct + '&exp=' + dat.toString(), dat.toGMTString());

                    break;
                }
            }
        }
    }
}

function refresh() {
    var sURL = unescape(window.location);
    var vPos; var hPos;

    //if (sURL.indexOf('tgcx.mirror-image.net') > -1) sURL = sURL.replace('tgcx.mirror-image.net', 'www.thegolfchannel.com');
    //if (sURL.indexOf('www1.thegolfchannel.com') > -1) sURL = sURL.replace('www1.thegolfchannel.com', 'www.thegolfchannel.com');
    if (sURL.indexOf('v.pos') > -1) sURL = sURL.substring(0, sURL.indexOf('v.pos') - 1);

    if (document.documentElement && document.documentElement.scrollTop)
        vPos = document.documentElement.scrollTop;
    else if (document.body)
        vPos = document.body.scrollTop;
    else
        vPos = window.pageYOffset;

    if (document.documentElement && document.documentElement.scrollLeft)
        hPos = document.documentElement.scrollLeft;
    else if (document.body)
        hPos = document.body.scrollLeft;
    else
        hPos = window.pageXOffset;

    //var vPos = (document.all) ? document.body.scrollTop : window.pageYOffset;
    //var hPos = (document.all) ? document.body.scrollLeft : window.pageXOffset;
    //window.location.reload(true);
    if (vPos == 0 && hPos == 0) {
        window.location.replace(sURL);
    } else {
        window.location.replace((sURL.indexOf('?') > -1) ? sURL + '&v.pos=' + vPos + '&h.pos=' + hPos : sURL + '?v.pos=' + vPos + '&h.pos=' + hPos);
    }
}

function grayOut(vis, options) {
    // Pass true to gray out screen, false to ungray
    // options are optional.  This is a JSON object with the following (optional) properties
    // opacity:0-100         // Lower number = less grayout higher = more of a blackout 
    // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
    // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
    // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
    // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
    // in any order.  Pass only the properties you need to set.
    var options = options || {};
    var zindex = options.zindex || 100001;
    var opacity = options.opacity || 60;
    var opaque = (opacity / 100);
    var bgcolor = options.bgcolor || '#000000';
    var dark = document.getElementById('darkenScreenObject');
    var selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) {
        selects[i].style.visibility = "hidden";
    }
    if (!dark) {
        // The dark layer doesn't exist, it's never been created.  So we'll
        // create it here and apply some basic styles.
        // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
        var tbody = document.getElementsByTagName("body")[0];
        var selects = document.getElementsByTagName("select");
        for (i = 0; i != selects.length; i++) {
            selects[i].style.visibility = "hidden";
        }
        var tnode = document.createElement('div');           // Create the layer.
        tnode.style.position = 'absolute';                 // Position absolutely
        tnode.style.top = '0px';                           // In the top

        tnode.style.left = '0px';                          // Left corner of the page
        tnode.style.overflow = 'hidden';                   // Try to avoid making scroll bars            
        tnode.style.display = 'none';                      // Start out Hidden
        tnode.id = 'darkenScreenObject';                   // Name it so we can find it later

        tbody.appendChild(tnode);                            // Add it to the web page

        dark = document.getElementById('darkenScreenObject');  // Get the object.
    }
    if (vis) {
        // Calculate the page width and height 
        if (document.body && (document.body.scrollWidth || document.body.scrollHeight)) {
            var pageWidth = document.body.scrollWidth + 'px';
            var pageHeight = document.body.scrollHeight + 'px';
        } else if (document.body.offsetWidth) {
            var pageWidth = document.body.offsetWidth + 'px';
            var pageHeight = document.body.offsetHeight + 'px';
        } else {
            var pageWidth = '100%';
            var pageHeight = '100%';
        }
        //set the shader to cover the entire page and make it visible.
        dark.style.opacity = opaque;
        dark.style.MozOpacity = opaque;
        dark.style.filter = 'alpha(opacity=' + opacity + ')';
        dark.style.zIndex = zindex;
        dark.style.backgroundColor = bgcolor;
        dark.style.width = pageWidth;
        dark.style.height = pageHeight;
        dark.style.display = 'block';
    } else {
        dark.style.display = 'none';
        var selects = document.getElementsByTagName("select");
        for (i = 0; i != selects.length; i++) {
            selects[i].style.visibility = "visible";
        }
    }
}

//window.onload = process;
if (window.attachEvent) { window.attachEvent('onload', process); }
else if (window.addEventListener) { window.addEventListener('load', process, false); }
else { document.addEventListener('load', process, false); } 

