$(document).ready(function() {
    // fix hover fx on menu
    $('.top-section .menulevel1 ul li a').each(function(c) {
        function swapActImg(objLink) {
            var strInactSrc = objLink.children('.inactive').attr('src');
            var strActSrc = objLink.children('.active').attr('src');
            objLink.children('.inactive').attr('src', strActSrc);
            objLink.children('.active').attr('src', strInactSrc);
            return false;
        }

        $(this).bind('mouseover', function(e) {
            return swapActImg($(this));
        });
        $(this).bind('mouseout', function(e) {
            return swapActImg($(this));
        });
    });

    // fix click on media columns (should be on every page). First check if there are a single link
    $('.content-section .type1 .content-media-columns .item').each(function() {
        var objLink = $(this).children('a');
        if (objLink.length == 1) {
            $(this).css('cursor', 'pointer');
            $(this).bind('click', function() {
                var t = objLink.attr('target');
                if (t.length == 0 || t == '_self' || t == '_top') {
                    window.location.href = objLink.attr('href');
                }
                else {
                    common.openWin({ url: objLink.attr('href'), name: objLink.attr('target'), skipprop: true });
                }
                return false;
            });
        }
    });

    // setup behavior on thumbnail lists
    $('.images-items-list').each(function(c1) {
        var objList = $(this);
        var bUseImgText = objList.hasClass('use-image-desc');
        $(this).children('a').each(
			function(c2) {
			    $(this).bind('click', function(e) {
			        var objLargeImg = objList.parent().children('.images-items-large');
			        objLargeImg.attr('src', $(this).attr('href'));
			        $(this).parent().children().removeClass('active');
			        $(this).addClass('active');

			        if (bUseImgText) {
			            objList.parent().children('.image-desc').html($(this).children('img').attr('alt'));
			        }
			        $(this).trigger('blur');
			        return false;
			    });
			}
		);
    });

    // fix click on image bg menus
    //    $('.intro-listitem a').each(function() {
    //        var strUrl = $(this).attr('href');
    //        var objParent = $(this).parent().parent();
    //        objParent.css('cursor', 'pointer');
    //        objParent.bind('click', function() {
    //            window.location.href = strUrl;
    //            return false;
    //        });
    //    });
    $('.intro-listitem h1 a').each(function() {
        var strUrl = $(this).attr('href');
        var objParent = $(this).parent().parent().parent();
        objParent.css('cursor', 'pointer');
        objParent.bind('click', function() {
            window.location.href = strUrl;
            return false;
        });
    });


    function setupQuickMenu() {
        function click(e) {
            $(this).trigger('blur');
            common.openWin({ url: $(this).attr('href'), name: $(this).attr('rel'), addprop: e.data.addprop });
            return false;
        }
        $('.static .quickmenu .row1 a').each(function() {
            var strRel = $(this).attr('rel');
            if (strRel == 'ext-docs') {
                $(this).bind('click', { addprop: 'width=700,height=700,scrollbars=yes' }, click);
            }
            else if (strRel == 'ext-freight') {
                $(this).bind('click', { addprop: 'width=550,height=550,scrollbars=yes' }, click);
            }
        });
        $('.static .quickmenu .row2 a').each(function() {
            var strRel = $(this).attr('rel');
            if (strRel == 'ext-broshures') {
                $(this).bind('click', { addprop: 'width=700,height=700,scrollbars=yes' }, click);
            }
            else if (strRel == 'ext-freight') {
                $(this).bind('click', { addprop: 'width=550,height=550,scrollbars=yes' }, click);
            }
        });
    }

    function setupDescBoxes() {
        var idSuffix = '_CustomInfoBox';
        var curActiveDescBoxID = null;
        var LEFT_ADJUST = -80;
        function createNewObject() {
            var objNew = $('#CustomInfoBoxPrototype').clone();
            objNew.attr('id', '');
            return objNew;
        }
        function closeCurrent() {
            if (curActiveDescBoxID != null) {
                $('#' + curActiveDescBoxID).hide();
                curActiveDescBoxID = null;
            }
            return false;
        }

        //$('fieldset label.desc').each(function(c1) {
        $('label.desc').each(function(c1) {
            $(this).bind('click', function() {
                $('.custom-info-box').hide(); // close all others
                var strTempNewDescBoxID = $(this).attr('id') + idSuffix;
                if (curActiveDescBoxID == strTempNewDescBoxID) {
                    // check if current is the active, close it
                    return closeCurrent();
                }
                curActiveDescBoxID = null;
                // first check ,there might be a box created already
                var objBox = $(this).children('.custom-info-box');
                if (objBox.length == 0) {
                    // create the box
                    var pos = $(this).position();
                    objBox = createNewObject();
                    objBox.css({ 'top': (pos.top + 20) + 'px', 'left': (pos.left + LEFT_ADJUST) + 'px' });
                    $(this).append(objBox);
                    objBox.attr('id', strTempNewDescBoxID);
                    objBox.children('.list').children('.box-text').children('div').text($(this).children('span').text());
                }
                objBox.show();
                window.setTimeout(function() { // need to do like this, otherwise the
                    curActiveDescBoxID = objBox.attr('id');
                }, 100);
            });
        });
        $(document).bind('click', function() {
            closeCurrent();
        });
    }

    setupDescBoxes();
    setupQuickMenu();

    $('.bottom-functions a.print').bind('click', function() {
        window.print();
        return false;
        //return favorites.add(EPI_CURRENT_PAGE_ID);
    });


    // setup finder box:
    //common.setupSearchInput($('.top-section .static .search-box .bg input.box'));

    // make sure QA search is executed when pressing enter.
    $('.Ident-FinderSearchText').bind('keypress', function(e) {
        if (e.keyCode == '13') {
            $('.Ident-FinderSearchButton').trigger('click');
            return false;
        }
    });

    // fix the ugly active border around links
    // removes many of them, but it will cost, is it worth it ?
    //    $('a').each(function() {
    //		$(this).bind('focus', function() {
    //			$(this).trigger('blur');
    //		});
    //    });

});

