var Plavix = Plavix || {};
var count = 0;

Plavix.Utils = Plavix.Utils || {};
Plavix.Utils.noPrint = function(id_value) {
    if ($('#' + id_value + ':checked').attr('checked')) {
        $('#group_' + id_value).removeClass("noPrint");
    }
    else {
        $('#group_' + id_value).addClass("noPrint");
    }
}
Plavix.Utils.Text = function() {

    //Private variables
    var that = this;
    var size = 13;
    var Hsize = 18;
    var COOKIE_NAME = 'text_size';
    var COOKIE_LH = 'line_height';

    //Public methods
    this.setText = function() {
        if (jQuery.cookie) {
            if ($.cookie(COOKIE_NAME)) {
                var mainContP = $('.subPage #mainContent p');
                var mainContLi = $('.subPage #mainContent li');
                var mainContStrong = $('.subPage #mainContent strong');
                var mainContH = $('.subPage #mainContent h3');
                var textSizeP = $('.subPage #contentCol #mainContent .textResize p');
                var xlineHeight;

                if ($.cookie(COOKIE_NAME)) {
                    var csize = $.cookie(COOKIE_NAME) - 13;
                    mainContP.css('font-size', $.cookie(COOKIE_NAME) + "px");
                    mainContLi.css('font-size', $.cookie(COOKIE_NAME) + "px");
                    mainContStrong.css('font-size', $.cookie(COOKIE_NAME) + "px");
                    var hcsize = csize + 18;
                    mainContH.css('font-size', hcsize + "px");
                }
                //alert(clineHeight);
                if ($.cookie(COOKIE_LH)) {
                    clineHeight = $.cookie(COOKIE_LH);
                    mainContP.css('line-height', clineHeight + 'px');
                    mainContLi.css('line-height', clineHeight + 'px');
                }
                textSizeP.css('font-size', '9px');
                textSizeP.css('line-height', '18px');
                $('.subPage #contentCol #mainContent p.note').css('font-size', '11px');
            }
            else {
                //No cookie exists, create new cookie
                that.createCookie('13');
            }
        }
    };
    
    

    this.textSize = function(value) {
    
    //to enable the text resizing in two clicks
        count++;
        if (count == 3) {
            count = 0;
            return;
        }
        var mainContP = $('.subPage #mainContent p');
        var mainContLi = $('.subPage #mainContent li');
        var mainContStrong = $('.subPage #mainContent strong');
        var mainContH = $('.subPage #mainContent h3');
        var textSizeP = $('.subPage #contentCol #mainContent .textResize p');
        var nlineHeight = "";
        size = parseInt(mainContP.css("font-size").replace("px", ""), 10);
        if (mainContH.css("font-size") !== undefined) {
            Hsize = parseInt(mainContH.css("font-size").replace("px", ""), 10);
        }
        var lineHeight = parseInt(mainContP.css("line-height").replace("px", ""), 10);

        if (size == 13 && value == "less") {
            //alert("test");
            //do nothing
        } else if (size > 13 && value == "less") {
            //alert(size);
            textSizeP.css('font-size', '9px');
            //console.log(size);
            mainContP.css('font-size', size - 1 + "px");
            mainContLi.css('font-size', size - 1 + "px");
            mainContStrong.css('font-size', size - 1 + "px");
            mainContH.css('font-size', Hsize - 1 + "px");
            nlineHeight = lineHeight - 1 + "px";
            mainContP.css('line-height', nlineHeight);
            mainContLi.css('line-height', nlineHeight);
            textSizeP.css('font-size', '9px');
            textSizeP.css('line-height', '18px');
            $('.subPage #contentCol #mainContent p.note').css('font-size', '11px');
            $.cookie(COOKIE_NAME, size - 1, { path: '/', expires: 365 });
            $.cookie(COOKIE_LH, lineHeight - 1, { path: '/', expires: 365 });

        } else if ((size <= 13 || size < 16) && value == "more") {
            mainContP.css('font-size', size + 1 + "px");
            mainContLi.css('font-size', size + 1 + "px");
            mainContStrong.css('font-size', size + 1 + "px");
            mainContH.css('font-size', Hsize + 1 + "px");
            nlineHeight = lineHeight + 1 + "px";
            mainContP.css('line-height', nlineHeight);
            mainContLi.css('line-height', nlineHeight);
            textSizeP.css('font-size', '9px');
            textSizeP.css('line-height', '18px');
            $('.subPage #contentCol #mainContent p.note').css('font-size', '11px');
            $.cookie(COOKIE_NAME, size + 1, { path: '/', expires: 365 });
            $.cookie(COOKIE_LH, lineHeight + 1, { path: '/', expires: 365 });
        }

    };

    this.textBind = function() {
        $('.less').click(function() {
            that.textSize('less');
        });

        $('.more').click(function() {
            that.textSize('more');
        });
    };

    this.createCookie = function(which) {
        $.cookie(COOKIE_NAME, which, { path: '/', expires: 365 });
    };
}; //End Plavix.Utils.Text Module


Plavix.Utils.checkAll = function() {
    $("#checkboxall").click(function() {
        var checked_status = this.checked;
        $("input[@type=checkbox]").each(function() {
            this.checked = checked_status;
        });
        Plavix.Utils.noPrint('q0');
        Plavix.Utils.noPrint('q1');
        Plavix.Utils.noPrint('q2');
        Plavix.Utils.noPrint('q3');
        Plavix.Utils.noPrint('q4');
        Plavix.Utils.noPrint('q5');
        //Plavix.Utils.noPrint('q6');
        Plavix.Utils.noPrint('q7');
        Plavix.Utils.noPrint('q8');
        Plavix.Utils.noPrint('q9');
        Plavix.Utils.noPrint('q10');
        Plavix.Utils.noPrint('q11');
        Plavix.Utils.noPrint('q12');
        Plavix.Utils.noPrint('q13');
        Plavix.Utils.noPrint('q14');
        Plavix.Utils.noPrint('q15');
        Plavix.Utils.noPrint('q16');
    });
};

Plavix.Utils.toggleCheckBoxes = function(state) {
    if (state == 'on') {
        $("input[@type=checkbox]").each(function() {
            this.style.display = '';
        });
    }
    else { //off
        $("input[@type=checkbox]").each(function() {
            this.style.display = 'none';
        });
    }
};

Plavix.Utils.print = function() {
    window.print();
};

Plavix.Utils.listPrint = function() {
    $("#listPrint").click(function() {
        /* doesn't work
        $("input[@type=checkbox]").each(function() {
            if (!$(this).is(":checked")) {
                $(this).parent("li").addClass("noPrint");
            }
        });*/
        javascript:window.print();
    });
};

Plavix.Utils.moveObject = function(toplink, div) {
    var width = 532;
    if (navigator) {
        if (navigator.appVersion.indexOf('MSIE 6') != -1) {
            
            
            document.getElementById('interstitial2').style.left = 484;
            document.getElementById('interstitial3').style.left = 484;
            document.getElementById('interstitial_aha').style.left = 484;
        }
       
    }
    document.getElementById('interstitial2').style.marginLeft = "-" + width / 2 + "px";
    document.getElementById('interstitial3').style.marginLeft = "-" + width / 2 + "px";
    document.getElementById('interstitial_aha').style.marginLeft = "-" + width / 2 + "px";
    var interstitialClass = (toplink) ? "toplink" : "btmlink";
    document.getElementById('interstitial2').className = interstitialClass;
    document.getElementById('interstitial3').className = interstitialClass;
    document.getElementById('interstitial_aha').className = interstitialClass;
};
Plavix.Utils.position = function(value) {
    if (navigator) {
        if (navigator.appVersion.indexOf('MSIE 6') != -1) {
            //IE6 hack - reset the popup
            document.getElementById('interstitial2').style.marginTop = "-" + value + "px";
            document.getElementById('interstitial3').style.marginTop = "-" + value + "px";
            document.getElementById('interstitial_aha').style.marginTop = "-" + value + "px";
                }
    }

}
Plavix.Utils.toggleInterstitial = function(top, url, type, e) {
    var toplink = (top == "top") ? 1 : 0;
    toplink = (top == "aa") ? 2 : toplink;

    /*window.onscroll = function() { document.getElementById('interstitial2').style.top = document.body.scrollTop; };
    document.getElementById('interstitial2').style.display = "block";
    document.getElementById('interstitial2').style.top = document.body.scrollTop;*/

    var interstitial; /*= (type == "hcp") ? document.getElementById('interstitial3') : document.getElementById('interstitial2');*/
    if (type == "hcp") {
        interstitial = document.getElementById('interstitial3');
    }
    else if (type == "aha") {
        if (url != 'close') {
            var pageTracker = _gat._getTracker("UA-1173604-3");
            pageTracker._setDomainName('none');
            pageTracker._setAllowLinker('true');
            pageTracker._trackPageview('/plavix/squibb_sanofipp_page');
        }
        interstitial = document.getElementById('interstitial_aha');
    }
    else {
        var pageTracker = _gat._getTracker("UA-1173604-3");
        pageTracker._setDomainName('none');
        pageTracker._setAllowLinker('true');
        pageTracker._trackPageview('/plavix/squibb_sanofipp_page');
        interstitial = document.getElementById('interstitial2');
    }
    if (url == 'close') {
        interstitial.style.display = "none";
        return false;
    }
    else if (url == 'downloadHeart') {
        $('#downloadHeart').show();
    }
    else {
        Plavix.Utils.moveObject(toplink, interstitial);
        if ($('#abiVideo')) {
            $('#abiVideo').hide();
        }
        if ($('#moaVideo')) {
            //call flash to reset the scrubber
            try {
                document.getElementById('moa_video').closeVideoPlayer();
            }
            catch (exception) { }
        }
        if ($('#downloadHeart')) {
            $('#downloadHeart').hide();
        }
        if ($('#downloadStroke')) {
            $('#downloadStroke').hide();
        }
        if ($('#downloadPAD')) {
            $('#downloadPAD').hide();
        }
        if ($('#downloadHRG')) {
            $('#downloadHRG').hide();
        }
        if ($('#downloadSRG')) {
            $('#downloadSRG').hide();
        }
        if (type != "hcp") {
            document.getElementById('go').href = url;
            $('#interstitial3').hide();
            $('#interstitial_aha').hide();
        }
        if (type == '') {

            if ($('#interstitial2')) {
                $('#interstitial2').hide();
            }
            $('#interstitial_aha').hide();
            $('#interstitial3').hide();
        }



        /* for aha*/
        else if (type == "aha") {
            document.getElementById('go1').href = url;
            document.getElementById('go1').onclick = function() {
                if ($('#interstitial_aha')) {
                    $('#interstitial_aha').hide();
                }
                else if ($('#interstitial2')) {
                    $('#interstitial2').hide();
                }
            };
            $('#interstitial_aha').hide();
            $('#interstitial2').hide();
        }
        /* for aha*/
        else if (type == "hcp") {

            document.getElementById('go2').href = url;
            document.getElementById('go2').onclick = function() {
                if ($('#interstitial3')) {
                    $('#interstitial3').hide();
                }
                if ($('#interstitial_aha')) {
                    $('#interstitial_aha').hide();
                }
            };
            $('#interstitial2').hide();
            $('#interstitial_aha').hide();

        }
        else {

            document.getElementById('go').href = url;
            document.getElementById('go').onclick = function() {
                if ($('#interstitial2')) {
                    $('#interstitial2').hide();
                }
                if ($('#interstitial_aha')) {
                    $('#interstitial_aha').hide();
                }
            };
            $('#interstitial2').hide();
            $('#interstitial_aha').hide();

        }

        interstitial.style.display = "block";
    }
    return false;
};

Plavix.Utils.toggleDownload = function(top, url, e) {
    $(".download").hide();
    var download;
    var toplink = (top == "top") ? 1 : 0;
    if (url == "downloadHeart") {
        download = document.getElementById('downloadHeart');
    }
    else if (url == "downloadStroke") {
        download = document.getElementById('downloadStroke');
    }
    else if (url == "downloadPAD") {
        download = document.getElementById('downloadPAD');
    }
    else if (url == "downloadHRG") {
        download = document.getElementById('downloadHRG');
    }
    else if (url == "downloadSRG") {
        download = document.getElementById('downloadSRG');
    }
    
    if (url == 'close') {

    //    $('#interstitial3').hide();
        //download.style.display = "none";
        $(".download").hide();
        return false;
    } else {
        Plavix.Utils.moveObject(toplink, download);
        if ($('#abiVideo')) {
            $('#abiVideo').hide();
        }
        if ($('#moaVideo')) {
            $('#moaVideo').hide();
        }
        if ($('#downloadHeart')) {
            $('#downloadHeart').hide();
        }
        if ($('#downloadStroke')) {
            $('#downloadStroke').hide();
        }
        if ($('#downloadPAD')) {
            $('#downloadPAD').hide();
        }
        if ($('#downloadHRG')) {
            $('#downloadHRG').hide();
        }
        if ($('#downloadSRG')) {
            $('#downloadSRG').hide();
        }
        if ($('#interstitial2')) {
            $('#interstitial2').hide();
        }
        if ($('#interstitial3')) {
            $('#interstitial3').hide();
        }
        if ($('#interstitial_aha')) {
            $('#interstitial_aha').hide();
        }
        download.style.display = "block";
    }

    return false;
};

Plavix.Utils.toggleMOA = function() {

    if (navigator) {
        if (navigator.appVersion.indexOf('MSIE 6') != -1) {
            Plavix.Utils.toggleCheckBoxes('on');
        }
    }
    document.getElementById('moaVideo').style.left = "-10000px";
    if (PlavixVideo) {
        PlavixVideo.counter++;
        PlavixVideo.videoState = "pause";
    }
};

//Plavix VideoPlayer Module
Plavix.VideoPlayer = function() {
    this.counter = 0;
    this.videoState = "pause";
};

Plavix.VideoPlayer.prototype = {

    displaySWF: function() {
        $('#moaVideo').show();
    },

    displayPlaySWF: function() {
        if (navigator.appVersion.indexOf('MSIE 6') != -1) {
            document.getElementById('moaVideo').style.left = "0px";
        }
        else {
            document.getElementById('moaVideo').style.left = "38%";
        }

        document.getElementById("moa_video").playVideoPlayer();
    },

    stopHideSWF: function() {
        document.getElementById("moa_video").closeVideoPlayer();
        document.getElementById('moaVideo').style.left = "-10000px";
    },

    hide: function() {
        $('#moaVideo').hide();
        this.videoState = "pause";
    },

    displayPlayHideSWF: function() {
        if ($('#downloadHeart')) {
            $('#downloadHeart').hide();
        }

        if ($('#downloadStroke')) {
            $('#downloadStroke').hide();
        }

        if ($('#downloadPAD')) {
            $('#downloadPAD').hide();
        }

        if ($('#downloadHRG')) {
            $('#downloadHRG').hide();
        }

        if ($('#downloadSRG')) {
            $('#downloadSRG').hide();
        }

        if ($('#interstitial2')) {
            $('#interstitial2').hide();
        }
        
        if ($('#interstitial_aha')) {
            $('#interstitial_aha').hide();
        }

        if (this.counter === 0) {
            if (navigator) {
                if (navigator.appVersion.indexOf('MSIE 6') != -1) {
                    Plavix.Utils.toggleCheckBoxes('off');
                }
            }
            this.displaySWF();
            this.counter++;
            this.videoState = "play";
        }
        else {
            if (this.videoState == "play") {
                this.stopHideSWF();
                this.videoState = "pause";
            }
            else {
                this.displayPlaySWF();
                this.videoState = "play";
            }
        }
    },

    playFLV: function(id) {

        var vidThumb = document.getElementById('thumb' + id);
        
        if (vidThumb.src.indexOf('_active') == -1) {
            vidThumb.src = vidThumb.src.replace('.jpg', '_active.jpg');
        }
        var flvArray = ["heartAttack.flv", "father_daughterNew.flv", "stroke.flv"];
        //var flvArray = ["coming_soon.flv", "coming_soon.flv", "coming_soon.flv"];
        var titleArray = ["Heart Attack (caused by a <br />clot) Video", "P.A.D. Video: Father/Daughter", "Stroke Video"];
        $(".vidTitle").empty();
        $(".vidTitle").append(titleArray[id]);
        var playerDiv = $('#player');
        playerDiv.innerHTML = "<div id=\"flvHolder\"></div>";
        var flashvars = { flvPath: flvArray[id] };
        var params = { wmode: "transparent" };
        var attributes = {};
        swfobject.embedSWF('../flash/videos.swf', 'flvHolder', '334', '290', '8.0.0', '../flash/expressInstall.swf', flashvars, params, attributes);
    }
};



Plavix.Utils.showAnnouncement = function() {

    var articles = $("#newsHeadlines .article"); //get all the article DOM elements

    //get the last news article the user saw from their cookies
    var lastHeadline = $.cookie("plavixHeadline");
    var currentHeadline = 0; //init variable

    //if first time visitor, they wont have that cookie
    if (lastHeadline == null) {
        currentHeadline = 0;
    }
    //otherwise set the next article in sequence
    else {
        currentHeadline = parseInt(lastHeadline);
        //if at the end of the sequence, return to the beginning
        if (currentHeadline == articles.length - 1) {
            currentHeadline = 0;
        }
        else {
            currentHeadline = currentHeadline + 1;
        }
    }

    //show an article
    articles.eq(currentHeadline).show();

    //save to cookie when leaving the page
    $(window).unbind("unload").unload(function() {
        $.cookie("plavixHeadline", currentHeadline, { expires: 10 });
    });

};


Plavix.Utils.cycleAnnouncements = function() {
    //-- Rotating version of showAnnouncement


    //config
    var speed = 10000; //set speed in milliseconds you want article to display for
    var clearHistory = 10; //set how many days you want the browser to remember the news story cycle position

    var timer;
    var articles = $("#newsHeadlines .article"); //get all the article DOM elements

    //get the last news article the user saw from their cookies
    var lastHeadline = $.cookie("plavixHeadline");
    var currentHeadline = 0; //init variable

    //if first time visitor, they wont have that cookie
    if (lastHeadline == null) {
        currentHeadline = 0;
    }
    //otherwise set the next article in sequence
    else {
        currentHeadline = parseInt(lastHeadline);
        setHeadline();
    }

    //show the first article
    articles.eq(currentHeadline).fadeIn("normal", function() {
        setHistory();
        //Start the timer when it finishs fading in
        timer = setInterval(cycle, speed);
    });

    function cycle() {
        //fade out the current headline
        articles.eq(currentHeadline).fadeOut("normal", function() {
            //when finished, set the next headline and fade it in.
            setHeadline();
            articles.eq(currentHeadline).fadeIn("normal", function() {
                //when complete set reset the page unload event to write the cookie
                setHistory();
            });
        });
    }

    function setHeadline() {
        //gets the next headline in sequence
        //if at the end of the sequence, return to the beginning
        if (currentHeadline == articles.length - 1) {
            currentHeadline = 0;
        }
        else {
            currentHeadline = currentHeadline + 1;
        }
    }

    function setHistory() {
        //remembers the last viewed article
        //sets the event when you leave the page to write a cookie for the last article seen
        $(window).unbind("unload").unload(function() {
            $.cookie("plavixHeadline", currentHeadline, { expires: 10 });
        });
    }

};



var PlavixText; //Define Global variable
$(document).ready(function() {
    PlavixText = new Plavix.Utils.Text();
    PlavixText.textBind();
    Plavix.Utils.checkAll();
    Plavix.Utils.listPrint();
    PlavixText.setText();
});

function validateSearch(searchbox) {
    //var val = document.forms[0].rslt_search.value;
    var val = document.getElementById(searchbox).value;
    //var val=searchbox.value
    if (val == "" || val == null) {
        alert("Please type the word or words you wish to search for in the search box.");
    }
    else {
        location.href = "/searchresults.aspx?q=" + val;
    }
}
