var zoom;
var magInterval;

function LoadProductImage(handle,showloadingimage){
	
	var mainImageID = "myimage";
	var instructionsID = "ProductImageInstructions";
	var loadingImageSrc = "/_resources/dvss.com/images/ajax-loader.gif";
	var thumbWidth = 50;
	var thumbHeight = 50;
	var mainImageWidth = 250;
	var mainImageHeight = 250;
	var zoomImageWidth = 400;
	var zoomImageHeight = 400;	
	
	var imgSrc;
	var magnifySrc;	
	imgSrc = handle.src;
	imgSrc = imgSrc.replace("width="+thumbWidth,"width="+mainImageWidth);
	imgSrc = imgSrc.replace("height="+thumbHeight,"height="+mainImageHeight);
	magnifySrc = imgSrc;
	magnifySrc = magnifySrc.replace("width="+mainImageWidth,"width="+zoomImageWidth);
	magnifySrc = magnifySrc.replace("height="+mainImageHeight,"height="+zoomImageHeight);
	
	if(zoom){zoom.parentNode.removeChild(zoom);}
	
	if(showloadingimage){
		// -- if used in IE before main image is done loading, we get the '1 item loading' bug --- //
		var parentHandle = getElement(mainImageID).parentNode;
		parentHandle.removeChild(getElement(mainImageID));
	
		var imageLoading = new Image();
		imageLoading.onload = function() {
			imageLoading.onload = null;
		}
		imageLoading.src = loadingImageSrc;
		imageLoading.id = mainImageID;
	
		parentHandle.appendChild(imageLoading);
		// --- end IE bug --- //
	}
	
	var zoomImage = new Image();
	zoomImage.onload = function() {
		zoomImage.onload = null;
		var image = new Image();
		
		image.onload = function() {
			
			image.onload = null;
			getElement(mainImageID).src = imgSrc;
			
			var imageWidth = getElement(mainImageID).offsetWidth;			
			if(imageWidth == 16){
				magInterval = setInterval("TryToMagnifyIt('" + mainImageID + "','" + instructionsID + "','" + magnifySrc + "')", 50);
			}
			else{
				MagnifyIt(mainImageID,instructionsID,magnifySrc);
			}
			
		};
		image.src = imgSrc;
		
	};
	zoomImage.src = magnifySrc;		
}

function TryToMagnifyIt(mainImageID,instructionsID,magnifySrc){
	var imageWidth = getElement(mainImageID).offsetWidth;
	if(imageWidth > 16){
		clearInterval(magInterval);
		MagnifyIt(mainImageID,instructionsID,magnifySrc);
	}
}

function MagnifyIt(mainImageID,instructionsID,magnifySrc){
	//alert("MagnifyIt");
	// move stuff out of the way...
	var imageWidth;
	var imageHeight;
	var instructionsHeight;
	
	var applyFirefoxFix = false;

	imageWidth = getElement(mainImageID).offsetWidth;
	imageHeight = getElement(mainImageID).offsetHeight;
	instructionsHeight = getElement(instructionsID).offsetHeight;
	
	if(imageWidth > 0){				
		getElement("ProductDetailLeft").style.width = imageWidth + "px";
	}
	if(imageHeight > 0){	
		getElement("ProductMultipleImages").style.top = (imageHeight + instructionsHeight) + "px";
		getElement("ProductMultipleImages").style.display = "block";
	}	
	zoom = MojoMagnify.makeMagnifiable(getElement(mainImageID),magnifySrc);
}

function getElement(name) {
	var o1 = null;
	if(document.all) {
		o1 = document.all(name);
	}
	else if(document.getElementById) {
		o1 = document.getElementById(name);
	}
	return o1;
}

function getInternetExplorerVersion() {

    var rv = -1; // Return value assumes failure.

    if (navigator.appName == 'Microsoft Internet Explorer') {

        var ua = navigator.userAgent;

        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");

        if (re.exec(ua) != null)

            rv = parseFloat(RegExp.$1);

    }

    return rv;

}

//New Cutup

(function ($) {
    // VERTICALLY ALIGN FUNCTION
    $.fn.vAlign = function () {
        return this.each(function (i) {
            var ah = $(this).height();
            var ph = $(this).parent().height();
            var mh = Math.ceil((ph - ah) / 2);
            $(this).css('margin-top', mh);
        });
    };
})(jQuery);
$(document).ready(function () {

    $('.Search').val('Product Search');
    $('.Search').click(function () { if($('.Search').val()=="Product Search") { $('.Search').val('') } });
    $('.Search').focusout(function () { if($('.Search').val()=="") { $('.Search').val('Product Search') } });
    if ($('.sku').text().length > 0 || $('.products-bottom-bar').text().length > 0) {
        $('.ad0').css('display', 'none');
    }

    var direction = "up";
    //If more then 2 change this variable to an integer
    var orientation = "right";

    //Move the product menu
    $('#headMenu > li a').not('li ul li a').each(function () {

        var thisElement = $(this);

        if (thisElement.text() == "Products") {

            $(this).parent().append('<ul></ul>');
            var productMenu = $('#prodMenu').html();
            $(this).parent().children('ul').html(productMenu);

        }
    });

    var count = 0
    var span = ""

    $('#headMenu > li a').not('li ul li a').each(function () {


        var thisElement = $(this).clone();
        if (count > 1) {
            span = "<span class='FooterLinkDivider'>|</span>"
            $('.footermenu').append(span);

        }
        if (count >= 1) {
            $('.footermenu').append(thisElement);
        }

        count = count + 1;
    });

    //Retrieves the current page sub menu items

    //Check if main menu level is on if not check lower levels
    var nop = $('li.on').parents('ul').length;
    if (nop == 1) {

        var subMenu = $('li.on').children('ul').html();
        $('#item1').html(subMenu);

    } else if (nop == 2) {

        var subMenu = $('li.on').parent('ul').html();
        $('#item1').html(subMenu);


    } else if (nop == 3) {

        //slide left load into correct ul's into item1 and item2

    }




    //Handles the Vertical Sliding Menu

    $('#slidebutton1').click(function () {

        if (direction == "up") {

            $('#slidebutton1').css('background-position', '-113px 0');
            direction = "down";

        } else {

            $('#slidebutton1').css('background-position', '0 0');
            direction = "up";
        };

        $('#submenu1').slideToggle('normal');

    });

    //Retrieves the Horizantal Menu Items

    //$('.menu1 ul li').click(function () {

    // var noc = $(this).children().size();
    //if (noc <= 1) {

    //return true;

    //} else {
    // var newmenu = $(this).children('ul').html();
    //if (orientation == "left") {
    //slideRight();
    //}
    //$('#item1').html(newmenu);
    //$('#item2').html("");



    //if (direction == "up") {

    //

    //} else {

    //$('#submenu1').slideToggle('normal');
    //$('#slidebutton1').css('background-position', '0 0');
    //direction = "up";

    //};

    //return false;
    //}
    //});

    //Handles the Horizantal Sliding Menu

    $('.item li').live('click', function () {

        //Check if there are children
        var noc = $(this).children().size();
        if (noc <= 1) {

            return true;

        } else {

            if (orientation == "right") {

                var newmenuz = $(this).children('ul').html();
                $('#item2').html(newmenuz);
                slideLeft();

            } else {

                //For more than 2 orientations

            }

            return false;
        }
    });

    $('#backbutton').click(function () {

        slideRight();

    });

    //Animate Horizontal Menu

    function slideLeft() {

        $('#mask').animate({ "right": "990" }, 1000);
        $('#backbutton').vAlign();
        orientation = "left";

    };

    function slideRight() {

        $('#mask').animate({ "right": "0" }, 1000);
        $('#backbutton').vAlign();
        orientation = "right";

    };

    if ($('#item1 li').text() == "") {
        $('#submenu1').toggle();
        $('#slidebutton1').css('background-position', '-113px 0');
        direction = "down";
    }

    if ($("input:text[name='username']").length > 0) {
        $("input:text[name='username']").focus();
    }

});
