// Bind Functions to Dom on document ready
$(function(){
    // Lightbox Popup-related functions
    $('#emailfriend-link').click(function() {
        showPopup('#emailfriend-container');
        // send omniture data
        var s=s_gi('kohlerengines.com,kohlerglobal');
        s.linkTrackVars='channel,prop1,prop2,pageName,prop9';
        s.prop1='KOHLER ENGINES';
        s.channel='KOHLER ENGINES:EMAIL CONTENT';
        s.prop2='KOHLER ENGINES:EMAIL CONTENT:FORM';
        s.pageName='KOHLER ENGINES:EMAIL CONTENT:FORM';
        s.prop9='TOOLS';
        void(s.t());
        return false;
    });
    $('#manuals-decal-link').click(function() { showPopup('#manuals-decal-overlay'); return false });
    $('.scene7-link').click(function() {
        $('#scene7-container').queue(function() {
            $('.rotateStuff').css('left', '-3000px');
            $('.zoomStuff').css('left', '1px');

            $('#scene7-content .right').show();
            $('#lb-scene7-images .left').css('width', '450px');
            $('#lb-scene7-images .left').css('margin-right', '16px');
            $('#scene7-zoomImage').css('width', '448px');
            $('#scene7-zoomImage').css('height', '448px');

            $('#lb-otherViews-tabs .tab').removeClass('active');
			$('#lb-otherViews-tabs #zoom').addClass('active');
			$('#scene7-container').dequeue();
		});
		$('#scene7-container').queue(function() {
			showPopup('#scene7-container', 5); 
			$('#scene7-container').dequeue();
		});
		return false;
	});
	$('.rotate-link').click(function() {
		$('#scene7-container').queue(function() {
            $('.zoomStuff').css('left', '-3000px');
            $('.rotateStuff').css('left', '1px');

            $('#scene7-content .right').hide();
            $('#lb-scene7-images .left').css('width', '645px');
            $('#lb-scene7-images .left').css('margin-right', '0');
            $('#scene7-zoomImage').css('width', '643px');
            $('#scene7-zoomImage').css('height', '426px');

            $('#lb-otherViews-tabs .tab').removeClass('active');
			$('#lb-otherViews-tabs #rotate').addClass('active');
			$('#scene7-container').dequeue();
		});
		$('#scene7-container').queue(function() {	
			showPopup('#scene7-container', 5);
			$('#scene7-container').dequeue();
		});
		return false;
	});

    $('a#equipmentpoweredbyseries-link').click(function() {
		$('.lightbox-scrollMe').jScrollPane({showArrows:false, scrollbarWidth:10});
		showPopup('#equipmentpoweredbyseries-container');
		return false;
	});

    $('li.download a').live("click", function() { showDownloadsPopup(this); return false;});
    $('tr.productmatrix-resultrow img.ql-button').live("click", function() { showQuickLook(this) });
    
    $('#tabcontent-curves div.twocol-left li.enlarge a,#tabcontent-curves div.twocol-left img').click(function() { showPopup('#horsepower-container', 7); return false });
    $('#tabcontent-curves div.twocol-right li.enlarge a,#tabcontent-curves div.twocol-right img').click(function() { showPopup('#torque-container', 7); return false });

    // Set up enlarge image link on press release pages
    $('.eng-pressRelease-image li.enlarge a').click(function() {
        var leftPixels = ($(window).width() / 2) - ($('#pressrelease-enlargedimage-overlay').width() / 2);
        var topPixels = ($(window).height() / 2) - ($('#pressrelease-enlargedimage-overlay').height() / 2);
        $('#pressrelease-enlargedimage-overlay').css('left', leftPixels + 'px');
        $('#pressrelease-enlargedimage-overlay').css('top', topPixels + 'px');

        $('#document-overlay').show().css('cursor', 'pointer').css('cursor', 'hand');
        $('#document-overlay').click(hidePopup);
    });

    // Set up definition lightboxes on where to buy page
    var dealerDefTimeout;
    $('.dealerdefinition-questionmark, #dealerLocator-dieselService').hover(function() {
        clearTimeout(dealerDefTimeout);
        var dealerType;
        if ($(this).attr('id').indexOf('dealerdefinition') == -1) {
            dealerType = 'checkbox';
        } else {
            dealerType = $(this).attr('id').split('-')[2];
        }
        $('.dealerdefinition').hide();
        //$('#dealerdefinition-overlay').bgiframe();
        $('#dealerdefinition-' + dealerType).show();

        var positionOfQuestionMark = $(this).offset();
        var popupHeight = $('#dealerdefinition-overlay').height();
        if (positionOfQuestionMark['top'] - (popupHeight + $(window).scrollTop()) < 10) {
            $('#dealerdefinition-overlay').css({top:positionOfQuestionMark['top'] + 20, left:positionOfQuestionMark['left'] - 10});
        } else {
            $('#dealerdefinition-overlay').css({top:positionOfQuestionMark['top'] - popupHeight, left:positionOfQuestionMark['left'] - 10});
        }
        return false;
    }, function() {
        dealerDefTimeout = setTimeout('hidePopup()', 100);
        return false;
    });
    $('#dealerdefinition-overlay').hover(function() {
        clearTimeout(dealerDefTimeout);
    }, function() {
        dealerDefTimeout = setTimeout('hidePopup()', 100);
    });

    $('#about-landing-images li.enlarge a').click(function() {
        var imageId = $(this).attr('id').split('-')[2];
        $('.about-enlargedimage').hide();
        $('#about-enlargedimage-' + imageId).show();

        var leftPixels = ($(window).width() / 2) - ($('#about-enlargedimage-overlay').width() / 2);
        var topPixels = ($(window).height() / 2) - ($('#about-enlargedimage-overlay').height() / 2) + $(window).scrollTop();
        $('#about-enlargedimage-overlay').css('left', leftPixels + 'px');
        $('#about-enlargedimage-overlay').css('top', topPixels + 'px');

        $('#document-overlay').show().css('cursor', 'pointer').css('cursor', 'hand');
        $('#document-overlay').click(hidePopup);
        return false;
    });

    $('.new-engines-bottomcontainer a').click(function() {
        var contentId = $(this).attr('id').split('-')[3];
        $('.new-engines-popupcontent').hide();
        $('#new-engines-' + contentId).show();

        var leftPixels = ($(window).width() / 2) - ($('#new-engines-overlay').width() / 2);
        var topPixels = ($(window).height() / 2) - ($('#new-engines-overlay').height() / 2) + $(window).scrollTop();
        $('#new-engines-overlay').css('left', leftPixels + 'px');
        $('#new-engines-overlay').css('top', topPixels + 'px');

        $('#document-overlay').show().css('cursor', 'pointer').css('cursor', 'hand');
        $('#document-overlay').click(hidePopup);
        return false;
    });

    $('.lb-closebutton,#otherViews-close').click(hidePopup);
});

