﻿function showTireSizes() {
    $(document).ready(function () {
        if ($("#ctl00_ContentPlaceHolder1_menuTireSize").is(":hidden")) {
            $("#ctl00_ContentPlaceHolder1_menuTireSize").slideDown();
            $("#ctl00_ContentPlaceHolder1_navSelectViewTires").slideUp();
        } else {
            $("#ctl00_ContentPlaceHolder1_menuTireSize").slideUp();
        }
    });
}

function showGrid1() {
    $(document).ready(function () {
        closeGrid2();
        closeGrid3();
        if ($("#ctl00_ContentPlaceHolder1_tabProdGrid1").is(":hidden")) {
            $("#ctl00_ContentPlaceHolder1_tabProdGrid1").slideDown();
        } else {
            $("#ctl00_ContentPlaceHolder1_tabProdGrid1").slideUp();
        }
    });
}

function showGrid2() {
    $(document).ready(function () {
        closeGrid1();
        closeGrid3();
        if ($("#ctl00_ContentPlaceHolder1_tabProdGrid2").is(":hidden")) {
            $("#ctl00_ContentPlaceHolder1_tabProdGrid2").slideDown();
        } else {
            $("#ctl00_ContentPlaceHolder1_tabProdGrid2").slideUp();
        }
    });
}

function showGrid3() {
    $(document).ready(function () {
        closeGrid2();
        closeGrid1();
        if ($("#ctl00_ContentPlaceHolder1_tabProdGrid3").is(":hidden")) {
            $("#ctl00_ContentPlaceHolder1_tabProdGrid3").slideDown();
        } else {
            $("#ctl00_ContentPlaceHolder1_tabProdGrid3").slideUp();
        }
    });
}

function closeGrid1() {
    $("#ctl00_ContentPlaceHolder1_tabProdGrid1").slideUp();
}

function closeGrid2() {
    $("#ctl00_ContentPlaceHolder1_tabProdGrid2").slideUp();
}

function closeGrid3() {
    $("#ctl00_ContentPlaceHolder1_tabProdGrid3").slideUp();
}

function confirmDelete() {
    if (confirm("Are you sure you want to proceed?")) {
        return true;
    } else {
        return false;
    }
}

////////////////SEARCH
function checkSearch() {
    var tboxName = document.getElementById('ctl00_txtSearch');
    if (tboxName.value == 'Search by Keyword') {
        tboxName.value = '';
    }
}
function checkSearchEmpt() {
    var tboxName = document.getElementById('ctl00_txtSearch');
    if (tboxName.value == '') {
        tboxName.value = 'Search by Keyword';
    }
}

function checkSearchAdmin() {
    var tboxName = document.getElementById('ctl00_txtSearchAdmin');
    if (tboxName.value == 'Search Admin') {
        tboxName.value = '';
    }
}
function checkSearchEmptAdmin() {
    var tboxName = document.getElementById('ctl00_txtSearchAdmin');
    if (tboxName.value == '') {
        tboxName.value = 'Search Admin';
    }
}

//////////////// GALLERY ///////////////////////

// Opacity and Fade in script.
// Script copyright (C) 2008 http://www.cryer.co.uk/.
// Script is free to use provided this copyright header is included.
function SetOpacity(object, opacityPct) {
    // IE.
    object.style.filter = 'alpha(opacity=' + opacityPct + ')';
    // Old mozilla and firefox
    object.style.MozOpacity = opacityPct / 100;
    // Everything else.
    object.style.opacity = opacityPct / 100;
}
function ChangeOpacity(id, msDuration, msStart, fromO, toO) {
    var element = document.getElementById(id);
    var opacity = element.style.opacity * 100;
    var msNow = (new Date()).getTime();
    opacity = fromO + (toO - fromO) * (msNow - msStart) / msDuration;
    if (opacity < 0)
        SetOpacity(element, 0)
    else if (opacity > 100)
        SetOpacity(element, 100)
    else {
        SetOpacity(element, opacity);
        element.timer = window.setTimeout("ChangeOpacity('" + id + "'," + msDuration + "," + msStart + "," + fromO + "," + toO + ")", 1);
    }
}
function FadeIn(id) {
    var element = document.getElementById(id);
    if (element.timer) window.clearTimeout(element.timer);
    var startMS = (new Date()).getTime();
    element.timer = window.setTimeout("ChangeOpacity('" + id + "',1000," + startMS + ",0,100)", 1);
}
function FadeOut(id) {
    var element = document.getElementById(id);
    if (element.timer) window.clearTimeout(element.timer);
    var startMS = (new Date()).getTime();
    element.timer = window.setTimeout("ChangeOpacity('" + id + "',1000," + startMS + ",100,0)", 1);
}
function FadeInImage(foregroundID, newImage, backgroundID) {
    var foreground = document.getElementById(foregroundID);
    if (backgroundID) {
        var background = document.getElementById(backgroundID);
        if (background) {
            background.style.backgroundImage = 'url(' + foreground.src + ')';
            background.style.backgroundRepeat = 'no-repeat';
        }
    }
    SetOpacity(foreground, 0);
    foreground.src = newImage;
    if (foreground.timer) window.clearTimeout(foreground.timer);
    var startMS = (new Date()).getTime();
    foreground.timer = window.setTimeout("ChangeOpacity('" + foregroundID + "',1000," + startMS + ",0,100)", 10);
}

//////////////// THUMBS ///////////////////////

function ViewImg(folderType, folder, fileIndex, fileArray) {

    //fade in new image
    var elementLInk = document.getElementById('ctl00_ContentPlaceHolder1_linkGalleryMain');
    var element = document.getElementById('ctl00_ContentPlaceHolder1_imgGalleryMain');
    if (element.filters) {
        element.style.filter = "blendTrans(duration=1)";
        element.filters.blendTrans.Apply();
    }
    element.src = "/" + folderType.toString() + "/" + folder.toString() + "/images/" + fileArray[fileIndex];
    elementLInk.href = "/" + folderType.toString() + "/" + folder.toString() + "/images/" + fileArray[fileIndex];
    elementLInk.rel = "modalPanel";

    if (element.filters) element.filters.blendTrans.Play();

    window.location.hash = 'gallery';
}

//////////////// MODAL POPUP ///////////////////////

(function ($) {
    $.fn.extend({
        modalPanel: function () {

            //Our function for hiding the modalbox
            function modalHide() {
                $(document).unbind("keydown", handleEscape);
                var remove = function () { $(this).remove(); };
                overlay.fadeOut(remove);
                modalWindow
					.fadeOut(remove)
					.empty();
            }

            //Our function that listens for escape key.
            function handleEscape(e) {
                if (e.keyCode == 27) {
                    modalHide();
                }
            }

            //Create our overlay object
            var overlay = $("<div id='modal-overlay'></div>");
            //Create our modal window
            var modalWindow = $("<div id='modal-window'></div>");

            return this.each(function () {
                //Listen for clicks on objects passed to the plugin
                $(this).click(function (e) {

                    if (typeof document.body.style.maxHeight === "undefined") { //if IE 6
                        $("body", "html").css({ height: "100%", width: "100%" });
                    }

                    //Append the overlay to the document body
                    $("body").append(overlay.click(function () { modalHide(); }));
                    //Add a loader to our page
                    $("body").append("<div id='modal-load'></div>");

                    //Set the css and fade in our overlay
                    overlay.css("opacity", 0.8);
                    overlay.show();

                    //Prevent the anchor link from loading
                    e.preventDefault();

                    //Activate a listener 
                    $(document).keydown(handleEscape);

                    //Load the image
                    var img = new Image();
                    $(img).load(function () {
                        var imageWidth = img.width / 2;
                        var imageHeight = img.height / 2;
                        modalWindow.css({
                            "margin-left": -imageWidth,
                            "margin-top": -imageHeight
                        });
                        $("#modal-load").remove();
                        modalWindow.append(img);
                        $(this).addClass("modal-image");
                        $("body").append(modalWindow);
                        modalWindow.fadeIn(50);
                    })
					.attr({ src: this.href }).click(function () {
					    modalHide();
					});
                });
            });
        }
    });
})(jQuery);


$(function () {
    $('#modalbox-demo a').modalPanel();
});

function modalHide() {
    $(document).unbind("keydown", handleEscape)
    var remove = function () { $(this).remove(); };
    overlay.fadeOut(remove);
    modalWindow
		.fadeOut(remove)
		.empty();
}
