/// <reference path="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.3.2.min-vsdoc.js" />

jQuery.noConflict();

jQuery(document).ready(function () {



    // empty introduction
    jQuery('.introduction:empty').addClass('empty');
    jQuery('.introduction:contains(  )').addClass('empty');

    // birth data
    jQuery('.invDetails').parent().addClass('invDetailsArea');

    // search resoults
    jQuery('.resoults').detach().prependTo('.listProducts');

    //drop down menu

    jQuery('.mainMenu .submenu li:first-child, .productsLine li:first-child').addClass('first');

    jQuery('.submenuArea').show();
    jQuery('.submenuArea').css('visibility', 'hidden');

    jQuery(".mainMenu li").hoverIntent(
         /*function () {
              jQuery(this).find('.submenuArea').slideDown(300);
          },
          function () {
              jQuery(this).find('.submenuArea').delay(400).slideUp(500);
          }
          */
          function () {
              jQuery(this).find('.submenuArea').slideDown(100);
          },
          function () {
              jQuery(this).find('.submenuArea').delay(250).slideUp(200);
          }
          /*function () {
              jQuery(this).find('.submenuArea').slideDown(150);
          },
          function () {
              jQuery(this).find('.submenuArea').hide();
          }*/

        );
    Array.max = function (array) {
        return Math.max.apply(Math, array);
    };

    var mainBoxWidth = jQuery('.main').width();

    var mainBoxLeft = jQuery('.main').offset().left;

    var mainMenuWidth = jQuery('.mainMenu').width();

    jQuery('.submenu').css('width', mainMenuWidth - 10);

    jQuery(".mainMenu > li").each(function () {
        var self = jQuery(this);
        var leftPosCurrItem = self.offset().left - mainBoxLeft;
        var subMenu = self.find('ul.submenu');

        var tmpWidths = subMenu.find('li').map(function () {

            var sensor = jQuery('<a>' + jQuery.trim(jQuery(this).text()) + '</a>').css({ margin: 0, padding: 0, position: 'absolute', left: '-9999em', 'white-space': 'nowrap' });
            jQuery(this).append(sensor);
            var currwidth = sensor.width() + 34;
            sensor.remove();

            return currwidth;
        }).get();

        maxElWidth = Array.max(tmpWidths);

        widthMenu = mainBoxWidth - leftPosCurrItem - 10;
        if (widthMenu < maxElWidth) {
            widthMenu = maxElWidth;
        }

        var paddingLeft = mainMenuWidth - widthMenu + 7;
        subMenu.find("> li > a").css("padding-left", paddingLeft + "px");
    })

    jQuery('.submenuArea').css('visibility', 'visible').hide();


    //table zebra
    jQuery("table.zebra").find("tr:nth-child(even)").each(
        function (i) {
            if (0 == jQuery(this).find("th").length) {
                jQuery(this).addClass("even");
            }
        }
      );

    jQuery('#pikame img').change(function () { alert(1) })


    // expand
    jQuery('ul.productView li .expandContent').hide();
    jQuery('ul.productView li.expand .expandContent').show();
    jQuery('ul.productView li h3.expandable').click(function () {

        var el = jQuery(this).parent();
        if (el.hasClass('expand')) {
            el.removeClass('expand');
            el.find('.expandContent').slideUp(400);

        }
        else {
            el.addClass('expand');
            el.find('.expandContent').slideDown(400);
        }
        return false;
    });


    //cart
    jQuery('.productsInCart table tr:last-child').addClass('lastTr')



    //resize products image


    jQuery("#pikame img").click(function () {
        var width = 319;
        var height = 280;
        jQuery('.pika_subdiv img').removeAttr('style');

        var newImg = new Image();

        newImg.onload = function () {
            var img_width = newImg.width;
            var img_height = newImg.height;

            fitElementSize({
                max_height: height,
                max_width: width,
                element: newImg,
                target: '.pika_subdiv img'
            });
        }

        newImg.src = jQuery(this).attr('src');
    });



    jQuery("#pikame li p .dialog_link").click(function () {
        var width = 920;
        var height = 720;
        jQuery('#dialog img').removeAttr('style');

        var newImg = new Image();

        newImg.onload = function () {
            var img_width = newImg.width;
            var img_height = newImg.height;

            fitElementSize({
                max_height: height,
                max_width: width,
                element: newImg,
                target: '#dialog img'
            });
        }

        newImg.src = jQuery(this).parents('li').find('img').attr('src');
        jQuery('#dialog img').addClass('showPic');
    });






    fitElementSize = function (parameters) {
        if (parameters.element.height >= parameters.max_height || parameters.element.width >= parameters.max_width) {
            if (parameters.element.height >= parameters.element.width) {
                jQuery(parameters.target).height(parameters.max_height);
            }

            else {
                jQuery(parameters.target).width(parameters.max_width);
            }
        }
    }


    if (jQuery.browser.msie) {
        jQuery('.productsLine li img').each(function () {
            var maxWidth = 159; // Max width for the image
            var maxHeight = 140;    // Max height for the image
            var width = jQuery(this).width();    // Current image width
            var height = jQuery(this).height();  // Current image height
            // Check if the current width is larger than the max
            if (width > height && width > maxWidth) {
                ratio = maxWidth / width;   // get ratio for scaling image
                jQuery(this).css("width", maxWidth); // Set new width
                jQuery(this).css("height", height * ratio); // Scale height based on ratio
            }
            else if (height > width && height > maxHeight) {
                ratio = maxHeight / height; // get ratio for scaling image
                jQuery(this).css("height", maxHeight);   // Set new height
                jQuery(this).css("width", width * ratio);    // Scale width based on ratio
            }
        });

        jQuery('.productsList li .productImage img').each(function () {
            var maxWidth = 140;
            var maxHeight = 210;
            var width = jQuery(this).width();
            var height = jQuery(this).height();

            if (width > height && width > maxWidth) {
                ratio = maxWidth / width;
                jQuery(this).css("width", maxWidth);
                jQuery(this).css("height", height * ratio);
            }
            else if (height > width && height > maxHeight) {
                ratio = maxHeight / height;
                jQuery(this).css("height", maxHeight);
                jQuery(this).css("width", width * ratio);
            }
        });
    }
    else {
        jQuery('.productsLine li img').load(function () {
            var maxWidth = 159; // Max width for the image
            var maxHeight = 140;    // Max height for the image
            var width = jQuery(this).width();    // Current image width
            var height = jQuery(this).height();  // Current image height
            // Check if the current width is larger than the max
            if (width > height && width > maxWidth) {
                ratio = maxWidth / width;   // get ratio for scaling image
                jQuery(this).css("width", maxWidth); // Set new width
                jQuery(this).css("height", height * ratio); // Scale height based on ratio
            }
            else if (height > width && height > maxHeight) {
                ratio = maxHeight / height; // get ratio for scaling image
                jQuery(this).css("height", maxHeight);   // Set new height
                jQuery(this).css("width", width * ratio);    // Scale width based on ratio
            }
        });



        jQuery('.productsList li .productImage img').load(function () {
            var maxWidth = 140;
            var maxHeight = 210;
            var width = jQuery(this).width();
            var height = jQuery(this).height();

            if (width > height && width > maxWidth) {
                ratio = maxWidth / width;
                jQuery(this).css("width", maxWidth);
                jQuery(this).css("height", height * ratio);
            }
            else if (height > width && height > maxHeight) {
                ratio = maxHeight / height;
                jQuery(this).css("height", maxHeight);
                jQuery(this).css("width", width * ratio);
            }
        });
    }



    jQuery(".tabsTitle td").click(function () {
        jQuery('.productsLine li img').each(function () {
            var maxWidth = 150;
            var maxHeight = 140;
            var width = jQuery(this).width();
            var height = jQuery(this).height();

            if (width > height && width > maxWidth) {
                ratio = maxWidth / width;
                jQuery(this).css("width", maxWidth);
                jQuery(this).css("height", height * ratio);
            }
            else if (height >= width && height >= maxHeight) {
                ratio = maxHeight / height;
                jQuery(this).css("height", maxHeight);
                jQuery(this).css("width", width * ratio);
            }
        });
    });



});







