$(document).ready(function () {

    // set body id and class
    var idarr = window.location.pathname.split("/");
    var id = idarr[idarr.length - 1];
    if (id == '')
        id = idarr[idarr.length - 2];
    var sec = idarr[idarr.length - 2];
    //20101117-KY Due to changes in folder/sitemap structure 
    //get rid of the .aspx page extension so css assignments work properly
    id = id.split(".")[0];

    var pathname = window.location.pathname;
    var bodyClass = window.location.pathname.split("/")[2];
    var selectedURI = window.location.pathname;
    if (selectedURI == '/default.aspx') selectedURI = '/';

    // set id
    $("body").attr("id", id);
    if (($("body").attr("id") == "") || ($("body").attr("id") == "default")) {
        $("body").attr("id", "home");
    }
    // set classes
    $("body").addClass(bodyClass);
    if ($("body").attr("class") == "") {
        $("body").addClass("home");
    }
    var isHCP = (sec == 'hcp' ? true : false);
    if (isHCP) {
        $("body").addClass("hcp");
        $('.notice').text('FOR U.S. HEALTHCARE PROFESSIONALS ONLY').css('margin-left', '-35px');
    } else {
        $("body").addClass("consumer");
    }
    $("a[href='index.aspx']").click(function () {
        $("body").removeClass("hcp");
        $("body").addClass("consumer");
        deleteCookie('hcp');
        //window.location('/');
        return true;
    });
    $("ul.consumer a[href='/hcp/']").click(function () {
        var cookie = getCookie("hcp");
        if (!cookie) {
            $("div[id='overlay']").fadeIn('fast');
            $("div.accept_hcp").fadeIn('fast');
            $("div.accept_hcp button.no").click(function () {
                $("div[id='overlay']").fadeOut('fast');
                $("div.accept_hcp").fadeOut('fast');
            });
            $("div.accept_hcp button.yes").click(function () {
                location.replace("/hcp/");
            });
            return false;
        }
        return true;
    });
    // add class to anchors
    /*
    $("ul>li").each(function() {
    var cur = $(this),
    i = $("a", cur), 
    href = i.attr("href");
    $(this).find("a").each(function(i, element) {
    if (href == "/") {
    $(element).addClass("home");
    } else {
    $(element).addClass(href.substring(1,href.length));
    }
    });
    });
    */
    $("div#header ul[class] a").each(function () {
        var href = $(this).attr("href");
        var nav = href.split('/');

        if (nav[nav.length - 1] == '')
            aClass = nav[nav.length - 2];
        else
            aClass = nav[nav.length - 1].split('.')[0];

        if (href == "/") {
            $(this).addClass("home");
        } else {
            $(this).addClass(aClass);
        }
    });

    $("button").each(function () {
        var val = $(this).attr("value");
        $(this).addClass(val);
    });
    // set selected elements
    //$('li a[href$="' + id + '.aspx"]').parent('li').addClass('selected');
    $('li a[href="' + selectedURI + '"]').parent('li').addClass('selected');
    // smooth scroll to top of page
    $("div#content p a[href='#']").click(function () {
        $('html, body').animate({ scrollTop: 0 }, 200);
        return false;
    });

    $('div#header div#salink').attr('title', 'sanofi-aventis');
    $('div#header div#salink').click(function () {
        window.open('http://www.sanofi-aventis.us/');
    });
    // tiny tabs
    var tab = 0;
    tab = $.query.get('tab');

    if (tab) {
        $("div#tabs>ul li:eq(" + tab + ")").addClass("selected");
        $("div#tabs>div").hide();
        $("div#tabs>div#tab" + (tab + 1).toString() + "").fadeIn('fast');
        //$("div#tabs>ul li a#" + tab).parents("li").addClass("selected");
    } else {
        $("div#tabs>ul li:first-child").addClass("selected");
    }


    $("div#tabs>ul a").click(function () {
        $("div#tabs>ul li").removeClass("selected");
        $(this).parent("li").addClass("selected");
        var id = $(this).attr("href");


        $("div#tabs>div").hide();
        $("div#tabs>div" + id).fadeIn('fast');
        return false;
    });
    $("div#content").append('<br clear="both" />');
    //$("div#content span").append('<br clear="both" />');
    // external links
    $("a[href^='http']:not(a[href*='sanofi'])").click(function () {
        var cookie = getCookie('extLink');
        if (!cookie) {
            $("div[id='overlay']").fadeIn('fast');
            $("[class*='confirm_link']").fadeIn('fast');
            var externalUrl = this.href;
            $("[class*='confirm_link'][button[id='continue']").click(function () {
                //$("[class*='confirm_link'][button[class='continue']").click(function(){
                window.open(externalUrl);
            });
            return false;
        }
        return true;
    });
    // draggable modal window
    $("div.modal").draggable();
    $('div.modal button[id="cancel"]').click(function () {
        $("div.modal").fadeOut('fast');
        $("div#overlay").hide();
    });
    $("div.modal button.cancel").click(function () {
        $(this).parents("div.modal").fadeOut('fast');
        $("#overlay").hide();
    });
    // UV meter - header
    $("div#UVindex button").click(function () {
        var loc = $('div#UVindex input').val();
        if (loc.length > 0 && loc.indexOf('Enter your ') == -1) {
            FillUVIData4Header(loc);
            $("div#UVindex div.search").hide();
            $("div#UVindex div.result").fadeIn();
        }
        return false;
    });

    $('div#UVindex input').keyup(function (event) {
        if (event.keyCode == '13') {
            $('div#UVindex button').trigger('click');
        }
    });

    $('ol.swatch li').wrapInner("<span/>");
    $("ol.swatch li.now").live("click", function () {

        var myClass = $(this).attr("class");
        $(this).toggleClass("selected");
        $("div.result dl").addClass(myClass);
        $("div.result dl").animate({
            "height": "toggle",
            "opacity": "toggle"
        }, {
            duration: 100,
            specialEasing: {
                height: 'linear'
            }
        });
    });
    $("div.result dl").click(function () {
        $(this).hide();
        $("ol.swatch li.now").toggleClass("selected");
    });

    $('div.result a[href="#search"]').click(function () {
        $(this).parents("div.result").hide();
        $("div#UVindex div.search").fadeIn();
        $('div#UVindex input').trigger('select');
        return false;
    });
    // UV meter - header

    /*
    $("button[value='UV']").click(function() {
    var loc = $('div#tab1 input').val();
    if (loc.length > 0 && loc.indexOf('Enter your ') == -1) {
    FillUVIData4SunCare(loc);
    $(this).parents("form").hide();
    $("div#UVresult").fadeIn();
    }

    return false;
    });
    */

    $("button#ie6UV").click(function () {
        var loc = $('div#tab1 input').val();
        if (loc.length > 0 && loc.indexOf('Enter your ') == -1) {
            FillUVIData4SunCare(loc);
            $(this).parents("form").hide();
            $("div#UVresult").fadeIn();
        }

        return false;
    });

    $('div#tab1 input').keyup(function (event) {
        if (event.keyCode == '13') {
            $('button#ie6UV').trigger('click');
        }
    });

    $('h2 a[href="#UVindex"]').click(function () {
        $(this).parents("div#UVresult").hide();
        $("form[name='UVindex']").fadeIn();
        $('div#tab1 input').trigger('select');
        return false;
    });
    // accordion
    $('ol.accordion li:first-child').addClass("selected");
    $('ol.accordion h2').click(function () {
        $('ol.accordion li').removeClass("selected");
        $(this).parent("li").addClass("selected");
        $('ol.accordion span').hide();
        //$(this).siblings("span").toggle("slow");
        $(this).siblings("span").animate({
            "height": "toggle",
            "opacity": "toggle"
        }, "slow");
    });
    // share

    $('div.share button.open').click(function () {
        $(this).fadeOut();
        $('div', $(this).siblings("form")).show();

        $(this).parent("div").animate({
            height: "520px"
        }, {
            duration: 500,
            specialEasing: {
                //height: 'linear',
                height: 'swing'
            }
        });
        return false;
    });

    $('div.share button.send').click(function () {
        var template = $(this).parents().hasClass('rebate') ? 'rebate' : 'aks';
        var send_name = $('input#' + template + '_send_name', $(this).parent()).val();
        var send_email = $('input#' + template + '_send_email', $(this).parent()).val();
        var friend_name = $('input#' + template + '_friend_name', $(this).parent()).val();
        var friend_email = $('input#' + template + '_friend_email', $(this).parent()).val();
        //FromName: $('input#' + template + '_send_name', $(this).parent()).val(),
        //FromAddress: $('input#' + template + '_send_email', $(this).parent()).val(),
        //ToName: $('input#' + template + '_friend_name', $(this).parent()).val(),
        //ToAddress: $('input#' + template + '_friend_email', $(this).parent()).val()

        if ((send_name.length == 0) || (send_email.length == 0) || (friend_name.length == 0) || (friend_email.length == 0)) {
            alert("Please fill out all the fields");
            return false;
        }
        //email validation
        var pattern = /^[\w\.=-]+@[\w\.-]+\.[\w]{2,3}$/;
        if ((!pattern.test(send_email)) || (!pattern.test(friend_email))) {
            alert("Email address is incorrectly formatted");
            return false;
        }

        $.ajax({
            type: 'POST',
            url: '/util/share_process.aspx',
            data: {
                TemplateName: template,
                FromName: send_email,
                FromAddress: send_email,
                ToName: friend_name,
                ToAddress: friend_email
            },
            success: function (data) {
                $('div.' + template + ' button.send').parent("div").hide();
                $('div.' + template + ' div.success').fadeIn("slow");
                $('div.' + template + ' button.send').parents("div.share").animate({
                    height: "340px"
                }, {
                    duration: 500,
                    specialEasing: {
                        height: 'linear',
                        height: 'easeOutBack'
                    }
                });

                $('input#' + template + '_send_name').val('');
                $('input#' + template + '_send_email').val('');
                $('input#' + template + '_friend_name').val('');
                $('input#' + template + '_friend_email').val('');
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            }
        });

        return false;
    });
    $('div.success button.ok').click(function () {
        $(this).parent("div").show();
        $('div.success').fadeOut("slow");
        return false;
    });
    $('div.share button.cancel').click(function () {
        $(this).parents("div.share").find("button.open").fadeIn();
        $(this).parents("div.share").animate({
            height: "200px"
        }, {
            duration: 600,
            specialEasing: {
                height: 'linear'
            }
        });
        return false;
    });
    $('div.rebate button.cancel').click(function () {
        $(this).parents("div.rebate").find("button.open").fadeIn();
        $(this).parents("div.rebate").animate({
            height: "200px"
        }, {
            duration: 600,
            specialEasing: {
                height: 'linear'
            }
        });
        return false;
    });
    $('div.share button.ok').click(function () {
        $(this).parents("div.share").find("button.open").fadeIn();
        $(this).parents("div.share").animate({
            height: "200px"
        }, {
            duration: 600,
            specialEasing: {
                height: 'linear'
            },
            complete: function () {

                //window.reload();
            }
        });
        return false;
    });

    // rebate
    var rebate = "/about_carac/download_rebate.aspx?dld=.pdf";
    $('a[href*="download-rebate"]').click(function () {
        window.open(rebate);
        return false
    });
    $("div#sidebar li:nth-child(3)").click(function () {
        window.open(rebate);
        return false;
    });
    // ipractice
    $('img[alt="ipractice"]').click(function () {
        var ipractice = "http://www.ipractice.com/";
        window.open(ipractice);
    });

    $("div#sidebar li:nth-child(2)").click(function () {
        location.href = '/util/share.aspx';
    });

    $("div#sidebar li:nth-child(1)").click(function () {
        var cookie = getCookie('extLink');
        if (!cookie) {
            $("div[id='overlay']").fadeIn('fast');
            $("[class*='confirm_link']").fadeIn('fast');
            $("[class*='confirm_link'][button[id='continue']").click(function () {
                location.reload();
                window.open('http://itunes.apple.com/us/app/sunskin/id431604248?mt=8', '', '', 'false');
                $(this).parents("div.modal").fadeOut('fast');
                $("div#overlay").hide();
            });
            return false;
        }
        return false;
    });

    /*
    $('div.rebate').click(function() {
    var rebate = "http://www.carac.info/aboutcarac/download-rebate.aspx?dld=.pdf";
    window.open(rebate);
    });
    */
    // placeholder
    var i = document.createElement('input');
    if ('placeholder' in i) {
        return;
    }

    var isPassword = function (input) {
        return $(input).attr('realType') == 'password';
    }

    var valueIsPlaceholder = function (input) {
        return input.value == $(input).attr('placeholder');
    }

    var showPlaceholder = function (input, loading) {
        if (input.value == '' || (loading && valueIsPlaceholder(input))) {
            if (isPassword(input)) {
                try {
                    input.setAttribute('type', 'input');
                } catch (e) { }
            }
            input.value = $(input).attr('placeholder');
            $(input).addClass('placeholder');
        }
    }

    var hidePlaceholder = function (input) {
        if (valueIsPlaceholder(input) && $(input).hasClass('placeholder')) {
            if (isPassword(input)) {
                try {
                    input.setAttribute('type', 'password');
                    input.focus();
                } catch (e) { }
            }

            input.value = '';
            $(input).removeClass('placeholder');
        }
    }

    $(':text[placeholder],:password[placeholder]').each(function (index) {
        if ($(this).attr('type') == 'password') {
            $(this).attr('realType', 'password');
        }

        showPlaceholder(this, true);

        $(this).focus(function () { hidePlaceholder(this) });
        $(this).blur(function () { showPlaceholder(this, false) });
    });
    // custom function to allow toggle/fade
    jQuery.fn.fadeToggle = function (speed, easing, callback) {
        return this.animate({ opacity: 'toggle' }, speed, easing, callback);
    };

    var uvLoc = getCookie('uvLoc');
    if (uvLoc != null && uvLoc != '') {
        $('div#UVindex input').val(uvLoc);
        $("div#UVindex button").trigger('click');
    }
});                                                                                                                   // end jQuery

function showPI() {
    window.open('http://products.sanofi-aventis.us/carac/carac.html', 'pi', 'width=640,height=500,resizable=1,scrollbars=1');
    return false;
}

// cookie setter
function setCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}
// cookie getter
function getCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}
// cookie deleter
function deleteCookie(name,path,domain) {
  if(getCookie(name)) document.cookie = name + "=" +
  ((path) ? ";path=" + path : "") +
  ((domain) ? ";domain=" + domain : "" ) +
  ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}


 

