IS.LOTD = {
    thispagecount : '',
    previouspage : '',
    nextpage : '',
    adcount : 1,
    init : function() {
        $('#leftcolumn a.lotdpopup').click(function() {
            window.open(
                $(this).attr('href'),
                null,
                'resizable=1,height=610,width=830,status=yes,toolbar=no,menubar=no,location=no,scrollbars=no'
            );
            return false;
        });
        if ($.gup("count") != undefined) {
            IS.LOTD.adcount = Number($.gup("count"));
        } else if (IS.LOTD.thispagecount == 2){
            IS.LOTD.adcount++;
        }
        if (IS.LOTD.thispagecount == '') {
            for(key in pagination) {
                if (IS.LOTD.pageid) {
                    if (key == IS.LOTD.pageid) {
                        IS.LOTD.thispagecount = pagination[key];
                        break;
                    }
                }
            }
        }
        for(key in pagination) {
            if (pagination[key] == IS.LOTD.thispagecount - 1) {
                IS.LOTD.previouspage = key;
            }
            if (pagination[key] == IS.LOTD.thispagecount + 1) {
                IS.LOTD.nextpage = key;
            }
            if (IS.LOTD.previouspage != '' && IS.LOTD.nextpage != '') {
                break;
            }
        }
        if (IS.LOTD.previouspage != '') {
            if ($('#adslide').length > 0) { //interstitial
                $('#adslide div.prev a').click(function() {
                    $(this).attr('href','/instyle/celebrities/lotdpopup/0,,' + IS.LOTD.previouspage + ',00.html?count=0');
                });
            } else {
                $('#slide li.prev a').click(function() {
                    IS.LOTD.adcount--;
                    $(this).attr('href','/instyle/celebrities/lotdpopup/0,,' + IS.LOTD.previouspage + ',00.html?count=' + IS.LOTD.adcount);
                });
            }
            
        }
        if (IS.LOTD.nextpage != '') {
            $('#slide li.next a').click(function() {
                if (IS.LOTD.adcount == 5) { //show an interstitial
                    $('#slide li.next a').attr('href','/instyle/celebrities/lotdpopup/adslide/0,,' + IS.LOTD.nextpage + ',00.html');
                } else { //show the next slide
                    IS.LOTD.adcount++;
                    $('#slide li.next a').attr('href','/instyle/celebrities/lotdpopup/0,,' + IS.LOTD.nextpage + ',00.html?count=' + IS.LOTD.adcount);
                }
            });
        }
    },
    pageof : function () {
        for(key in pagination) {
            if (IS.LOTD.pageid) {
                if (key == IS.LOTD.pageid) {
                    IS.LOTD.thispagecount = pagination[key];
                    break;
                }
            }
        }
        if (IS.LOTD.thispagecount != '') {
            $('#slide li.cnt').text(IS.LOTD.thispagecount + ' of ' + pagination.length);
        }
    }
}

/* http://www.yaduk.co.uk/2009/05/13/javascript-get-url-parameter-gup-jquery/ */
jQuery.extend({
    gup: function(param){
        var param = param.replace(/[[]/,"[").replace(/[]]/,"]"); 
        var regexS = "[?&]"+param+"=([^&#]*)";  
        var regex = new RegExp(regexS);  
        var results = regex.exec(window.location.href);  
        if(results) { 
            return results[1];
        }
    }
});

$(function() { IS.LOTD.init(); });

