function switchPhoto(index) {
    var count = 0;
    
    $("#standardPhoto img").each(function() {
        if (count == index) {
            this.className = "active";
        } else {
            this.className = "";
        }
        
        count++;
    });
    
    count = 0;
    
    $("#thumbnailPhoto img").each(function() {
        if (count == index) {
            this.className = "active";
        } else {
            this.className = "";
        }
        
        count++;
    });
}

function showStores(textInput) {
    textInput.value = trim(textInput.value);
    
    var onlineStoreFrame = document.getElementById("onlineStoreFrame");
    
    if (onlineStoreFrame && isUsZipCode(textInput.value)) {
        onlineStoreFrame.src = "http://www.zipwise.com/weblocator/results.php?a=912&zip=" + escape(textInput.value);
        
        onlineStoreFrame.style.display = "block";
        onlineStoreFrame.style.width   = "600px";
        onlineStoreFrame.style.height  = "600px";
    }
}

function hasRetailerPassword(form) {
    var password = form.elements["password"];
    
    if (password.value == "orderzoobies") {
        return true;
    }
    
    alert("Entered password is incorrect. Please re-enter your password and try again.");
    
    password.value = "";
    password.focus();
    
    return false;
}

function updateTotal() {
    var total = 0;
    
    jQuery("input.qty").each(function() {
        var qty  = parseInt(this.value);
        var cost = parseFloat(this.getAttribute("cost"));
        
        if (!isNaN(qty) && !isNaN(cost)) {
            total += qty * cost;
        }
    });
    
    if (total > 0) {
        $("td.amountTotal").text("$" + Math.floor(100 * total).toString().replace(/^(.*?)(\d{2})$/, "$1.$2"));
    } else {
        $("td.amountTotal").text("");
    }
}

function updateTotals(inputElement, productID) {
    var quantity = parseInt(inputElement.value);
    var tdObject = document.getElementById("amount_" + productID);
    
    if (isNaN(quantity) || quantity < 1) {
        inputElement.value = 0;
        
        tdObject.innerHTML = "";
    } else {
        var amount = quantity * parseFloat(inputElement.getAttribute("cost"));
        
        tdObject.innerHTML = "$" + Math.floor(100 * amount).toString().replace(/^(.*?)(\d{2})$/, "$1.$2");
    }
    
    updateTotal();
}

function toggleMoreNews(newsID) {
    var paragraphs = $("#news_" + newsID + " .copy p");
    var moreToggle = $("#news_" + newsID + " .more");
    
    paragraphs.each(function() {
        if (/active/.test(this.className)) {
            this.className = trim(this.className.replace(/active/, ""));
        } else {
            this.className = trim(this.className.toString() + " active");
        }
    });
        
    if (moreToggle.text() == "Read More") {
        moreToggle.text("Hide More");
    } else {
        moreToggle.text("Read More");
    }
}

var flashVars = {
    "file"       : null,
    "image"      : null,
    "title"      : null,
    "backcolor"  : "ffffff",
    "frontcolor" : "111111",
    "lightcolor" : "9bbad9",
    "autostart"  : "true",
    "skin"       : "/swf/flvplayer_skin_schoon.swf",
    "abouttext"  : "Zoobies",
    "aboutlink"  : "/",
    "fullscreen" : "false"
};

var flashAttribs = { "id" : "ZoobieVideos", "name" : "Zoobie Videos" };

function runVideo() {
    if (flashVars.file) {
        var zoobiesVideo = document.getElementById("ZoobieVideos");
        
        if (zoobiesVideo) {
            var videoParent = zoobiesVideo.parentNode;
            
            videoParent.removeChild(zoobiesVideo);
            
            var videoPlayer = document.createElement("div");
            
            videoPlayer.setAttribute("id", "videoPlayer");
            
            videoParent.appendChild(videoPlayer);
        }
        
        $("#videoTitle").text(flashVars.title.toString());
        
        swfobject.embedSWF(
            "/swf/player.swf",
            "videoPlayer",
            "320",
            "220",
            "9.0.0",
            "/swf/expressInstall.swf",
            flashVars,
            {},
            flashAttribs
        );
    }
}

function launchVideo(file, image, title) {
    flashVars.file  = file;
    flashVars.image = image && image.length > 0 ? image : null;
    flashVars.title = title && title.length > 0 ? title : null;
    
    runVideo();
}

$(document).ready(function() {
    $("#theCarousel").jcarousel({
        "wrap" : "circular"
    });
    
    if ($("#internationalFlash")) {
        swfobject.embedSWF(
            "/swf/worldMap.swf",
            "internationalFlash",
            "758",
            "348",
            "9.0.0",
            "/swf/expressInstall.swf"
        );
    }
    
    if ($("#videoPlayer")) {
        runVideo();
    }
    
    var mainRotator = $("#mainRotator");
    
    if (mainRotator) {
        mainRotator.innerfade({
            "speed"           : 1000,
            "timeout"         : 5000,
            "type"            : "sequence",
            "containerheight" : "440px"
        });
    }
    
    var contactForm = $("#contactForm");
    var orderForm   = $("#orderForm");
    var shipDate    = $('#orderForm input[name="ship_date"]');
    
    if (contactForm.length > 0) { contactForm.validate(); }
    if (orderForm.length > 0)   { orderForm.validate();   }
    if (shipDate.length > 0)    { shipDate.datepicker();  }
});

