jQuery(document).ready(function() {
    //input hint
    if (jQuery('input')[0]) {
        jQuery('input').hint();
    }

    // home boxes equal height
    if (jQuery('.homeBoxContent')[0]) {
        jQuery('#homePageBoxes .homeBoxContent').equalHeights();
    }
    if (jQuery('.homeBox .tabContainer')[0]) {
        jQuery('.homeBox .tabContainer').equalHeights();
    }
    // footer latest item
    jQuery('#footerAddress li:last').addClass("latestItem");

    // top menu latest item
    jQuery('#topMenu li:last').addClass("latestItem");

    // main menu functions
    jQuery('#mainMenu li:first').addClass("firstItem");
    jQuery('#mainMenu li:last').addClass("lastItem");

    jQuery('#mainMenu li a#active').parent().prev().addClass("prevactive");
    jQuery('#mainMenu li a#active').parent().prev('li.firstItem').attr("id", "preactive");

    jQuery('#mainMenu li a').hover(
      function() {
          jQuery(this).addClass("hovered");
          jQuery(this).parent().prev().children('a').addClass("prevhovered");
      },
      function() {
          jQuery(this).removeClass("hovered");
          jQuery(this).parent().prev().children('a').removeClass("prevhovered");
      }
    );

    if (jQuery.browser.msie) {
        if (jQuery.browser.version < 7) {
            if (jQuery('.firstItem.prevactive')[0]) {
                jQuery('.firstItem.prevactive a').addClass('previe')
            }
        }
    }

    // front page home boxes
    jQuery('#homePageBoxes .homeBox:eq(1), #homePageBoxes .homeBox:eq(3), #orderTabs .homeBox:eq(1), #orderTabs .homeBox:eq(3), #orderTabs .homeBox:eq(5), #productsTwoCol .homeBox:eq(1), #productsThreeCol .homeBox:eq(2), #productsTwoCol .homeBox:eq(3), #productsThreeCol .homeBox:eq(5), #productsTwoCol .homeBox:eq(5), #productsThreeCol .homeBox:eq(8), #productsTwoCol .homeBox:eq(7), #productsThreeCol .homeBox:eq(11), #productsTwoCol .homeBox:eq(9), #productsThreeCol .homeBox:eq(14), #productsThreeCol .homeBox:eq(17), #productsThreeCol .homeBox:eq(20), #productsThreeCol .homeBox:eq(23), #productsThreeCol .homeBox:eq(26), #productsThreeCol .homeBox:eq(29), #productsThreeCol .homeBox:eq(32), #productsThreeCol .homeBox:eq(35)').css({ "padding-right": "0" });

    // quick order
    var quickOrderLink = jQuery('#quickOrder h2 a');
    var quickOrderDialog = jQuery('#quickOrderPopup').dialog({
        autoOpen: false,
        bgiframe: true,
        width: 257,
        height: 333
    });

    quickOrderLink.click(function() {
        quickOrderDialog.dialog('open');
        jQuery(".quickOrderLine .inpQuickQuantity:first").focus();
        return false;
    });


    jQuery('#quickOrderPopup input.inpBestilnu').click(function() {
        myform = jQuery('form:first');
        jQuery('#quickOrderPopup').appendTo(myform);
        jQuery('#quickOrderPopup input.inpBestilnu').submit();
    });

    // order tabs 
    if (jQuery('#orderTabbed')[0]) {
        jQuery('#orderTabbed').tabs();
    }

    if (jQuery('#favoritenTabbed')[0]) {
        jQuery('#favoritenTabbed').tabs();
    }

    jQuery('#orderTabbed ul.tabbedList li:first').addClass("firstItem");
    jQuery('#favoritenTabbed ul.tabbedList li:first').addClass("firstItem");

    jQuery('.tabContainer table tr:even').addClass("odd");

    // order calendar
    if (jQuery('#shippingForm .inpDate')[0]) {
        jQuery.datepicker.setDefaults(jQuery.extend(jQuery.datepicker.regional['da']));
        jQuery('#shippingForm .inpDate').datepicker({ showOn: 'button', buttonImage: '../images/icon_calendar.gif', buttonImageOnly: true, firstDay: 1 });
    }

    if (jQuery('#productSearchContent input.inpDate')[0]) {
        jQuery.datepicker.setDefaults(jQuery.extend(jQuery.datepicker.regional['da']));
        jQuery('#productSearchContent input.inpDate').datepicker({ showOn: 'button', buttonImage: '../images/icon_calendar.gif', buttonImageOnly: true, firstDay: 1 });
    }

    // loading dialog
    var loaderLink = jQuery('a#launchLoader');
    var loaderDialog = jQuery('#loader').dialog({
        autoOpen: false,
        bgiframe: true,
        width: 257,
        height: 200
    });

    loaderLink.click(function() {
        loaderDialog.dialog('open');
        return false;
    });

    // my profile tabs
    if (jQuery('#myProfile')[0]) {
        jQuery('#myProfile.tabbed').tabs();
    }

    // my profile contacts
    jQuery('#openContacts').live("click", function() {
        jQuery('#myContacts').css({ "display": "block" });
    });
    jQuery('#closeContacts').live("click", function() {
        jQuery('#myContacts').css({ "display": "none" });
    });

    // my addresses and business table
    jQuery('table.addressTable tr:even, table.businessTable tr:even, table.tableHistory tr:even, table.table tr:even').addClass("odd");

    jQuery('table.addressTable tr').each(function() {
        jQuery(this).children('td:first').addClass("firstCell");
    });

    // reference line1 and line2
    if (jQuery('.line1 .referenceProcedure')[0]) {
        jQuery('.line1 .referenceProcedure label').equalHeights();
        jQuery('.line1 .referenceProcedure, .line1 .referenceSplit').equalHeights();
    }

    if (jQuery('.line2 .referenceProcedure')[0]) {
        jQuery('.line2 .referenceProcedure label').equalHeights();
        jQuery('.line2 .referenceProcedure, .line2 .referenceSplit').equalHeights();
    }

    // order toggle
    jQuery("#filterTrigger").hide();

    jQuery("#filterTrigger2").click(function() {
        jQuery("#orderDetails").show();
        jQuery("#filterTrigger").show();
        jQuery("#filterTrigger2").hide();
    });

    jQuery("#filterTrigger").click(function() {
        jQuery("#orderDetails").hide();
        jQuery("#filterTrigger2").show();
        jQuery("#filterTrigger").hide();
    });

    // cat menu
    jQuery('#catMenu ul li:last').addClass("latestItem");
    jQuery('#catMenu ul li:first').addClass("firstItem");

    // produkt gallery
    if (jQuery('#gallery')[0]) {
        jQuery('.galContent').css({ "display": "block" });
        var gallery = jQuery('#gallery').galleriffic('#navigation',
		{
		    delay: 2000,
		    numThumbs: 12,
		    imageContainerSel: '#slideshow',
		    controlsContainerSel: '#controls',
		    titleContainerSel: '#image-title',
		    descContainerSel: '#image-desc',
		    downloadLinkSel: '#download-link',
		    fixedNavigation: true,
		    galleryKeyboardNav: true,
		    autoPlay: false
		});

        gallery.onFadeOut = function() {
            jQuery('#details').fadeOut('fast');
        };

        gallery.onFadeIn = function() {
            jQuery('#details').fadeIn('fast');
        };
    }

    jQuery('#maximizeImage a').click(function() {
        jQuery("#slideshow a").trigger('click');
    });

    // product print with no prices
    jQuery("input#noPrice").change(function() {
        if (jQuery(this).is(':checked')) {
            jQuery('#masterContainer').addClass("noPrices");
        } else {
            if (jQuery(this).not(':checked')) {
                jQuery('#masterContainer').removeClass("noPrices");
            }
        }
    });

    // product table listing
    jQuery('table.productListingTable tr:even').addClass("odd");

    // product image listing
    jQuery('#prodImageListing .prodImageBox:eq(3), #prodImageListing .prodImageBox:eq(7), #prodImageListing .prodImageBox:eq(11), #prodImageListing .prodImageBox:eq(15), #prodImageListing .prodImageBox:eq(19), #prodImageListing .prodImageBox:eq(23), #prodImageListing .prodImageBox:eq(27)').css({ "margin-right": "0" });
    jQuery('#prodImageListing .prodImageLine:even .prodImageBox').addClass("withBlueBack");

    // keyboard navigation
    jQuery('#quickOrderForm .quickOrderLine .inpQuickQuantity').keydown(
      function(e) {
          //down
          if (e.keyCode == 40) {
              jQuery(this).parent().next().find('.inpQuickQuantity').focus();
          }
          //up
          if (e.keyCode == 38) {
              jQuery(this).parent().prev().find('.inpQuickQuantity').focus();
          }
          if (e.keyCode == 13) {
              jQuery(this).parent().find('.inpQuickItemNo').focus();
              return false;
          }
      }
    );

    jQuery('#quickOrderForm .quickOrderLine .inpQuickItemNo').keydown(function(e) {
        if (e.keyCode == 13) {
            jQuery('#quickOrderPopupTitle .inpBestilnu').trigger('click');
            return false;
        }
    });

    jQuery('#quickOrderForm .quickOrderLine .inpQuickItemNo').keydown(
      function(e) {
          //down
          if (e.keyCode == 40) {
              jQuery(this).parent().next().find('.inpQuickItemNo').focus();
          }
          //up
          if (e.keyCode == 38) {
              jQuery(this).parent().prev().find('.inpQuickItemNo').focus();
          }
          if (e.keyCode == 13) {
              jQuery(this).parent().next().find('.inpQuickQuantity').focus();
              return false;
          }
      }
    );

    // my profil dialogs
    var quickOrderLink1 = jQuery('#open_user1');
    var quickOrderDialog1 = jQuery('#open_user1_popup').dialog({
        autoOpen: false,
        bgiframe: true,
        modal: true,
        width: 187
        // height: 333
    });

    quickOrderLink1.click(function() {
        quickOrderDialog1.dialog('open');
        return false;
    });

    // open close advanced search
    if (jQuery('#centerSearchAdvanced input').is(':checked')) {
        jQuery("#centerSearchAdvanced, #closeAdvanced, #openAdvanced").toggle();
    }

    jQuery("#openAdvanced, #closeAdvanced").click(function() {
        jQuery("#centerSearchAdvanced, #closeAdvanced, #openAdvanced").toggle();
        return false;
    });

    if (jQuery('#contactDataTable')[0]) {
        jQuery("#contactDataTable").tablesorter({
            sortList: [[2, 0]],
            widgets: ['zebra']
        });
    }

    // cufon special fonts
    Cufon.replace('#newsItemOptions', { fontFamily: 'Myriad Pro', hover: true });
    Cufon.replace('#homePageTeasertext h1, .rightBoxContent h2, .customerService h2, #orderFilterSimple h2, #orderOptions h2 a, .newsRight h2, #quickOrderPopupTitle h2', { fontFamily: 'Myriad Pro Condensed', hover: true });

    if (jQuery(this).find('#map_canvas').html() != null) {
        initialize();
        StartInfo();
    }

    jQuery('#produktRight .produktPrintLink').click(
    function() {
        window.print();
    }
    );
    /*
    jQuery('#ConfirmOpenOrder').dialog({
    modal: true,
    bgiframe: true,
    width: 187    
    });
    */

    jQuery('#noPrice').click(function() {
        if (jQuery('#noPrice').is(':checked')) {
            myHref = jQuery('#lnkPrint').attr('href');
            jQuery('#lnkPrint').attr('href', myHref + '&showPrice=no');
        }
        else {            
            jQuery('#lnkPrint').attr('href', jQuery('#hdnOriginalUrl').val());
        }
    });
});