document.domain = 'instyle.com';

IS.Makeover = {
    locationparams : '',
    init : function() {
        //initialize the page's iframe ads
        var rnum = IS.Makeover.getRandomNumber();
        $('#adtopcontainer').html('<iframe src="/instyle/static/includes/ads/iframe_ad_728_90.html?zone=celeb/makeover/taaz/main&amp;ord='+rnum+'&amp;width=728&amp;height=90&amp;tile=1&amp;channel=taaz" width="728" height="90" frameborder="0" scrolling="no"></iframe>');
        $('#makeovercol2ad1 div.content').html('<iframe src="/instyle/static/includes/ads/iframe_ad_336_280.html?zone=celeb/makeover/taaz/main&amp;ord='+rnum+'&amp;width=336&amp;height=2800&amp;tile=2&amp;channel=taaz" width="336" height="280" frameborder="0" scrolling="no"></iframe>');
        //preload makeover tool
        IS.Makeover.tool();
        //check for passed in GET parameters, display makeover app if so
        if (String(document.location).split('?').length > 1) {
            this.locationparams = String(document.location).split('?')[1];
            IS.Makeover.showmakeovertool(this.locationparams);
        }
        //curtnav href update
        $('#curtnav-1 a').attr('href','#action=signin');
        $('#curtnav-2 a').attr('href','#action=register');
        $('#curtnav-3 a').attr('href','#action=help');
        //curtnav listeners
        $('#makoverheader div.curtnav a').click(function() {
            IS.Makeover.showmakeovertool($(this).attr('href').split('#')[1]);
            return false;
        });
        //carousel navigation listeners
        IS.Makeover.carousel();
        //main image link, carousel link, and footer link listeners
        $('#toutsection1 a, #carouselslides a').click(function() {
            //carousel links use the old "?" method of deep linking, so split on the "?" instead of the "#" like the main nav does
            IS.Makeover.showmakeovertool($(this).attr('href').split('?')[1]);
            return false;
        });
    },
    getRandomNumber : function() {
        return(Math.floor(Math.random()*1000000000000+1));
    },
    showmakeovertool : function(hash) {
        if(hash) { $('#makeovertool iframe').attr('src','http://makeover.instyle.com/' + '#' + hash); }
        if ($('#makeovertool').css('display') != 'block') {
            $('#makeoverstartbutton,#toutsection2').fadeOut(250);
            $("#makeovercol2").animate({"margin-top": "+=232px"}, 250, function() {
                $(this).css('height','310px');
                $('#makeovertool').fadeIn(250);
            });
        }
    },
    tool : function() {
        //QA URL: http://makeover.instyle.com/new/instyle2.html
        //PROD URL: http://makeover.instyle.com/
        $('#mainbody').append('<div id="makeovertool"><iframe width="993" height="656" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://makeover.instyle.com/"></iframe></div>');
        //makeover tool display listeners
        $('#makeoverstartbutton a, #makeovernav a, #toutsection2 h3 a').click(function() {
            var hash = $(this).attr('href').split('#');
            if (hash.length > 1) {
                hash = hash[1];
            } else {
                hash = '';
            }
            IS.Makeover.showmakeovertool(hash);
            return false;
        });
    },
    carousel : function() {
        var busy = false;
        var slidewidth = 134;
        $('#carouselslides').width($('#carouselslides div.slide').length * slidewidth);
        //carousel listeners
        $('#makeovercarouselprevious').click(function() {
            if (!busy) {
                busy = true;
                $('#carouselslides').css('left','-' + slidewidth + 'px');
                $('#carouselslides div.slide:last').clone().prependTo('#carouselslides');
                $('#carouselslides').animate({"left": '+=' + slidewidth + 'px'}, 500,'',function() {
                    $('#carouselslides div.slide:last').remove();
                    busy = false;
                });
            }
            return false;
        });
        $('#makeovercarouselnext').click(function() {
            if (!busy) {
                busy = true;
                $('#carouselslides div.slide:first').clone().appendTo('#carouselslides');
                $('#carouselslides').animate({"left": '-=' + slidewidth + 'px'}, 500,'',function() {
                    $('#carouselslides').css('left','0px');
                    $('#carouselslides div.slide:first').remove();
                    busy = false;
                });
            }
            return false;
        });
    },
    writeads : function(zone) {
        //set a new ord
        var rnum = IS.Makeover.getRandomNumber();
        //pass any parent parameters on to the child for ad ops testing
        var gup = location.href.split("?");
        var params = '';
        if (gup.length > 1) { params = '&' + gup[1]; }
        //rewrite the ads using the new zone
        $('#adtopcontainer').html('<iframe width="728" height="90" frameborder="0" scrolling="no" src="/instyle/static/includes/ads/iframe_ad_728_90.html?zone='+zone+'&ord='+rnum+'&width=728&height=90&tile=1&channel=taaz'+params+'"></iframe>');
        setTimeout(function() {
            $('#makeovercol2ad1').html('<div class="title"><span>Advertisement</span></div><div class="content"><iframe width="336" height="280" frameborder="0" scrolling="no" src="/instyle/static/includes/ads/iframe_ad_336_280.html?zone='+zone+'&ord='+rnum+'&width=336&height=280&tile=2&channel=taaz'+params+'"></iframe></div>');
        }, 1000);
    },
    writevideoiframe : function() {
        $('#makeovercol1').append('<iframe width="645" height="430" frameborder="0" scrolling="no" id="videoiframe" src="http://www.instyle.com/instyle/static/h/iframe/ads/hollywoodmakeover645x430.html" allowtransparency="true"></iframe>');
    },
    removevideoiframe : function() {
        $('#videoiframe').remove();
    }
}

function reloadAd(zone) { //reload ad iframes, called from Taaz Flash
    IS.Makeover.writeads(zone);
}
function reloadAds(zone) { //temporarily using both reloadAd and reloadAds for Taaz. TODO: remove one of these once we've confirmed which Taaz is really using -- Alex 8/30/2010
    IS.Makeover.writeads(zone);
}
function writevideoiframe(){ //write the video iframe overlay onto the page
    IS.Makeover.writevideoiframe();
}
function removevideoiframe(){ //removethe video iframe overlay from the page
    IS.Makeover.removevideoiframe();
}
function omniMakeover(omniStr) { //Makeover's Omniture function
    if(typeof(HHMakeover_tracker) == "function") HHMakeover_tracker(omniStr,s_account);
}

$(function() { IS.Makeover.init(); });
