$(function() {
    $(".tab-content").hide(); //Hide all content
    $("ul.tabs li").removeClass("active"); //Remove any "active" class
    $(".product-tab").show();
    var thisUrl = location.href.toString();
    var referringUrl = document.referrer;
    if (thisUrl.toUpperCase().indexOf('/CAT/') != -1) {
        $($("ul.tabs li")[2]).addClass("active").show();
        $($(".tab-content")[2]).show();
    }
    else if (thisUrl.toUpperCase().indexOf('/DOG/') != -1) {
        $($("ul.tabs li")[1]).addClass("active").show(); //Activate first tab
        $($(".tab-content")[1]).show(); //Show first tab content
    }
    else if (thisUrl.toUpperCase().indexOf('/FLEA-AND-TICK/') != -1) {
        $($("ul.tabs li")[4]).addClass("active").show(); //Activate first tab
        $(".tab-content:first").show();
    }
    else if (thisUrl.toUpperCase().indexOf('/FISH/') != -1 || thisUrl.toUpperCase().indexOf('/BIRDS/') != -1
    || thisUrl.toUpperCase().indexOf('/HORSE/') != -1 || thisUrl.toUpperCase().indexOf('/FERRET/') != -1
    || thisUrl.toUpperCase().indexOf('/RABBIT/') != -1 || thisUrl.toUpperCase().indexOf('/GUINEA-PIG/') != -1
    || thisUrl.toUpperCase().indexOf('/HAMSTER-AND-GERBIL/') != -1 || thisUrl.toUpperCase().indexOf('/SMALL-ANIMALS/') != -1
    ) {
        $($("ul.tabs li")[3]).addClass("active").show(); //Activate first tab
        $(".tab-content:first").show();
    }
   
    else if (thisUrl.toUpperCase().indexOf('/PRODUCT/') != -1) {
        setSubPage(referringUrl);
    }
    else if (thisUrl.toUpperCase().indexOf('/BRAND/') != -1) {
        setSubPage(referringUrl);
    }
    
    else {
        $("ul.tabs li:first").addClass("active").show(); //Activate first tab
        $(".tab-content:first").show(); //Show first tab content
    }


    //On Click Event
    $("ul.tabs li").click(function() {
        var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
        var activePage = $(this).attr("class");
        var newUrl = '';

        $("ul.tabs li").removeClass("active"); //Remove any "active" class
        $(this).addClass("active"); //Add "active" class to selected tab
        $(".tab-content").hide(); //Hide all tab content
        $(activeTab).show(); //Fade in the active ID content


        switch (activePage) {
            case 'PHARMACY':
                newUrl = '/default.aspx';
                break;
            case 'DOGSTORE':
                newUrl = '/landing/Dog-Center.aspx';
                break;
            case 'CATSTORE':
                newUrl = '/landing/Cat-Center.aspx';
                break;
            case 'OTHERANIMALS':
                newUrl = '/landing/Small-Animal-Center.aspx';
                break;
            case 'FLEATICK':
                newUrl = '/landing/Flea-and-Tick-Control-Medication.aspx';
                break;

        }

        newUrl = newUrl + '#' + activePage.replace(' ', '');
        window.location.replace(newUrl);
        return false;
    });

    url = window.location.toString();
    if (url.indexOf('#') != -1) {
        loadTab = url.substring(url.indexOf('#') + 1);
		switch (loadTab) {
            case 'PHARMACY':
            case 'DOGSTORE':
            case 'CATSTORE':
            case 'OTHERANIMALS':
            case 'FLEATICK':
                break;
            default:
                loadTab = 'PHARMACY';
                break;
        }

        showTabOnLoad(loadTab);
    }
    $('#product-content hr').remove();
    $('#ctl00_MainContent_RecProducts').css({ 'width': '550px' });
    //$('#ctl00_MainContent_RecProducts').css({'width':'898px'});
    $('#ctl00_MainContent_Baynote2').css({ 'width': '150px' });
    $("#ctl00_MainContent_PaymentManager_PaymentManagerCreditCardViewLabel").hide();
    $('.npp-drop').nmcDropDown({ show_delay: 300 });
    $('.search').toggleVal();
	 $('.membersemail').toggleVal();
    $(".search-results-product-options").each(function(i) {
        $(".search-results-sizes:nth-child(odd)").addClass("alt");
    });
	$("#tabs-21").removeAttr("style");
    $("#tabs-22").removeAttr("style");
    $("#tabs-23").removeAttr("style");
    $("#tabs-24").removeAttr("style");
    $("#tabs-25").removeAttr("style");

});
function showTabOnLoad(tabName) {
$("ul.tabs li").removeClass("active"); //Remove any "active" class
$('.' + tabName).addClass("active"); //Add "active" class to selected tab
$(".tab-content").hide(); //Hide all tab content
var activeTab = $('.' + tabName).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
$(activeTab).show(); //Fade in the active ID content
}
// autoship modal
// 0 disabled; 1 enabled;
var popupStatus = 0;
//loading popup
function loadPopup(){
//loads popup only if it is disabled
if(popupStatus==0){
$("#autoshipBkgd").css({
"opacity": "0.15"
});
$("#autoshipBkgd").fadeIn("fast");
$("#autoshipInfo").fadeIn("fast");
popupStatus = 1;
}
}
//disabling popup
function disablePopup(){
//disables popup only if it is enabled
if(popupStatus==1){
$("#autoshipBkgd").fadeOut("fast");
$("#autoshipInfo").fadeOut("fast");
popupStatus = 0;
}
}
//centering popup
function centerPopup(){
//request data for centering
var windowWidth = document.documentElement.clientWidth;
var windowHeight = document.documentElement.clientHeight;
var popupHeight = $("#autoshipInfo").height();
var popupWidth = $("#autoshipInfo").width();
//centering
//$("#autoshipInfo").css({
// "position": "absolute",
// "top": windowHeight/2-popupHeight/2,
// "left": windowWidth/2-popupWidth/2
//});
//only need force for IE6
$("#autoshipBkgd").css({
"height": windowHeight
});
}
//CONTROLLING EVENTS
$(document).ready(function(){
//LOADING POPUP
//button click
$("#button1").click(function(){
//centering with css
centerPopup();
//load popup
loadPopup();
});
//CLOSING POPUP
//Click close
$("#autoshipInfoClose").click(function(){
disablePopup();
});
$("#continue").click(function(){
disablePopup();
});
//Click out event!
$("#autoshipBkgd").click(function(){
disablePopup();
});
//Press Escape event!
$(document).keypress(function(e){
if(e.keyCode==27 && popupStatus==1){
disablePopup();
}
});
});

function setSubPage(referringUrl) {
    if (!referringUrl) {//Make sure reffering URL is not null
    } else {
        if (referringUrl.toUpperCase().indexOf('/CAT/') != -1 || referringUrl.toUpperCase().indexOf('CAT-CENTER.ASPX') != -1) {
            $($("ul.tabs li")[2]).addClass("active").show();
            $($(".tab-content")[2]).show();
        }
        else if (referringUrl.toUpperCase().indexOf('/DOG/') != -1 || referringUrl.toUpperCase().indexOf('DOG-CENTER.ASPX') != -1) {
            $($("ul.tabs li")[1]).addClass("active").show(); //Activate first tab
            $($(".tab-content")[1]).show(); //Show first tab content
        }
        else if (referringUrl.toUpperCase().indexOf('/FLEA-AND-TICK/') != -1) {
            $($("ul.tabs li")[4]).addClass("active").show(); //Activate first tab
            $(".tab-content:first").show();
        }
        else if (referringUrl.toUpperCase().indexOf('/FISH/') != -1 || referringUrl.toUpperCase().indexOf('/BIRDS/') != -1
            || referringUrl.toUpperCase().indexOf('/HORSE/') != -1 || referringUrl.toUpperCase().indexOf('/FERRET/') != -1
            || referringUrl.toUpperCase().indexOf('/RABBIT/') != -1 || referringUrl.toUpperCase().indexOf('/GUINEA-PIG/') != -1
            || referringUrl.toUpperCase().indexOf('/HAMSTER-AND-GERBIL/') != -1 || referringUrl.toUpperCase().indexOf('/SMALL-ANIMALS/') != -1
        ) {
            $($("ul.tabs li")[3]).addClass("active").show(); //Activate first tab
            $($(".tab-content")[3]).show();
        }
        else {
            $("ul.tabs li:first").addClass("active").show(); //Activate first tab
            $(".tab-content:first").show(); //Show first tab content
        }
    }
}       
