$(document).ready( function() {
    Cufon.replace('h1');
    Cufon.replace('ul#navigation_ul', {
        hover: true
    });
    
    Cufon.replace('#search_col_left h2');
    
    $("#header_search input").keypress(function(e){
        if (e.which == 13) {
            e.preventDefault();
            $('#header_search form').submit();
        }
    });
    
    $("#search_col_left input").keypress(function(e){
        if (e.which == 13) {
            e.preventDefault();
            $('#search_col_left form').submit();
        }
    });
    
    $("#search_term").focus(function() {
       $(this).val(''); 
    });
    
    $("#search_term_top").focus(function() {
       $(this).val(''); 
    });
	
	$("#search_by_product_family").hover(function() {
		$("#actual_products_results").animate({opacity: "show"}, "slow");
	});
	
	$("#search_by_application").hover(function() {
		$("#actual_applications_results").animate({opacity: "show"}, "slow");
	}); 
	
	$("#products_twin").mouseleave(function() {
		$("#actual_products_results").animate({opacity: "hide"}, "fast");
	});
	
	$("#applications_twin").mouseleave(function() {
		$("#actual_applications_results").animate({opacity: "hide"}, "fast");
	});
    
    
    if ($("#search_col_right").length > 0) {
        $("#pajinate-results").pajinate();
    }
});

$(window).load( function () {
    if ($("#installation_slider").length > 0) {

        $("#image_overlay").css('display', 'block');        
        $("#installation_slider ul li").css('display', 'block');

        var slider_width = $("#installation_slider ul li").first().outerWidth() * $("#installation_slider ul li").length;
        var in_scroll = false;

        $("#installation_slider ul").css(
        'width',
        slider_width + 'px');

        $("#installation_sidebar").css('height', $("#installation_slider").outerHeight());

        $('#installation_slider').jScrollPane();

    }

    if ($("#frontpage").length > 0) {
        
        $("#image_overlay").css('display', 'block');
        $("#frontpage ul li img").css('display', 'block');

        var slider_width = $("#frontpage ul li").last().outerWidth(true) * $("#frontpage ul li").length;
        var element_width = $("#frontpage ul li").first().width();
        var hover_width = element_width - 70;
        var in_scroll = false;

        $("#installation_slider ul li h2").equalHeights().css('overflow', 'hidden');

        $("#frontpage ul").css(
        'width',
        slider_width + 'px');

        var frontpage_pane = $('#frontpage')

        frontpage_pane.jScrollPane({
            animateScroll: true,
            animateDuration: 900
        });
        var api = frontpage_pane.data('jsp');
        
        var position = 0;
        var img_width = $('.jspPane ul li:last-child').outerWidth();
    
        $('#jspArrowLeft').bind(  
        'click', function() {
          // Note, there is also scrollToX and scrollToY methods if you only
          // want to scroll in one dimension
          if (position > 0) {
            position = position - img_width;
          }
          api.scrollToX(position); 
          return false;
        });        
    
        $('#jspArrowRight').bind(
        'click', function() {
          position = position + img_width;
          // Note, there is also scrollToX and scrollToY methods if you only
          // want to scroll in one dimension
          api.scrollToX(position);
          
          return false;
        });
        
        var hover_arrow_hide_flag_width = 0;
        
        $('#body').hover(function() {
            $('#jspArrowLeft').css({
                'display': 'block'
            });
            
            hover_arrow_hide_flag_width = slider_width - (element_width * 3);
            
            if (position < hover_arrow_hide_flag_width) {
                $('#jspArrowRight').css({
                    'display': 'block'
                });
            }
            
        }, function() {
            $('#jspArrowLeft').css({
                'display': ''
            });
            $('#jspArrowRight').css({
                'display': ''
            });
        });

        $("#frontpage ul li div").css({
            'width': hover_width + 'px'
        });

        $("#frontpage ul li").hover( function() {
            $(this).children('div').css('display', 'block');
        }, function() {
            $(this).children('div').css('display', 'none');
        });
        
        
    }
	
	if ($("#press").length > 0) {
			
		$("#image_overlay").css('display', 'block');
		$("#press ul li img").css('display', 'block');
	
		var slider_width = $("#press ul li").last().outerWidth(true) * $("#press ul li").length;
		var element_width = $("#press ul li").first().width();
		var hover_width = element_width - 70;
		var in_scroll = false;
	
		$("#installation_slider ul li h2").equalHeights().css('overflow', 'hidden');
	
		$("#press ul").css(
		'width',
		slider_width + 'px');
	
		var press_pane = $('#press')
	
		press_pane.jScrollPane({
			animateScroll: true
		});
		var api = press_pane.data('jsp');
		
		var position = 0;
		var img_width = $('.jspPane ul li:last-child').outerWidth();
	
		$('.jspArrowLeft').bind(  
		'click', function() {
		  // Note, there is also scrollToX and scrollToY methods if you only
		  // want to scroll in one dimension
		  if (position > 0) {
			position = position - img_width;
		  }
		  api.scrollToX(position); 
		  return false;
		});        
	
		$('.jspArrowRight').bind(
		'click', function() {
		  position = position + img_width;
		  // Note, there is also scrollToX and scrollToY methods if you only
		  // want to scroll in one dimension
		  api.scrollToX(position);
		  return false;
		});
	
		/*
		$("#press ul li div").css({
			'width': hover_width + 'px'
		});
	
		$("#press ul li").hover( function() {
			$(this).children('div').css('display', 'block');
		}, function() {
			$(this).children('div').css('display', 'none');
		});
		*/
		
	}

    if ($("#installation_slider_details").length > 0) {
        
        $("#image_overlay").css('display', 'block');
        $("#installation_slider_details ul li").css('display', 'block');

        var slider_width = $("#installation_slider_details ul li").last().outerWidth() * $("#installation_slider_details ul li").length;
        slider_width = slider_width - 300;
        var in_scroll = false;

        $("#installation_slider_details ul").css(
        'width',
        slider_width + 'px');

        if ($("#installation_sidebar_details").outerHeight() < $("#installation_slider_details").outerHeight()) {
            $("#installation_sidebar_details").css('height', $("#installation_slider_details").outerHeight());
        }
        else {
            $("#installation_slider_details").css('height', $("#installation_sidebar_details").outerHeight());
        }
       
        $('#installation_slider_details').bind(
        'jsp-scroll-x', function(event, scrollPositionX, isAtLeft, isAtRight) {
            if (isAtLeft == true) {
                $("#installation_sidebar_details").css({
                    'display': 'block',
                    'left': 0
                });
                in_scroll = false;
            } else {
                $("#installation_sidebar_details").css({
                    'display': 'none',
                    'left': scrollPositionX + 'px'
                });
                in_scroll = true;
            }
        }
        )
        .jScrollPane();

        $("#installation_slider_details").hover( function() {
            $("#installation_sidebar_details").css('display', 'block');
        }, function() {
            if (in_scroll == true) {
                $("#installation_sidebar_details").css('display', 'none');
            }
        });
    }

    if ($("#product_slider").length > 0) {
        
        $("#image_overlay").css('display', 'block');
        $("#product_slider ul li").css('display', 'block');

        var slider_width = $("#product_slider ul li").first().outerWidth(true) * $("#product_slider ul li").length;
        var in_scroll = false;

        $("#product_slider ul").css(
        'width',
        slider_width + 'px');

        $("#product_sidebar").css('height', $("#product_slider").outerHeight());

        $('#product_slider').bind(
        'jsp-scroll-x', function(event, scrollPositionX, isAtLeft, isAtRight) {
            if (isAtLeft == true) {
                $("#product_sidebar").css({
                    'display': 'block',
                    'left': 0
                });
                in_scroll = false;
            } else {
                $("#product_sidebar").css({
                    'display': 'none',
                    'left': scrollPositionX + 'px'
                });
                in_scroll = true;
            }
        }
        )
        .jScrollPane();

    }

    if ($("#product_slider_details").length > 0) {
        
        $("#image_overlay").css('display', 'block');
        $("#product_slider_details ul li").css('display', 'block');

        var slider_width = $("#product_slider_details ul li").last().outerWidth() * $("#product_slider_details ul li").length;
        slider_width = slider_width - 300;
        var in_scroll = false;

        $("#product_slider_details ul").css(
        'width',
        slider_width + 'px');
       
        if ($("#product_sidebar_details").outerHeight() < $("#product_slider_details").outerHeight()) {
            $("#product_sidebar_details").css('height', $("#product_slider_details").outerHeight());
        }
        else {
            $("#product_slider_details").css('height', $("#product_sidebar_details").outerHeight());
        }

        $('#product_slider_details').bind(
        'jsp-scroll-x', function(event, scrollPositionX, isAtLeft, isAtRight) {
            if (isAtLeft == true) {
                $("#product_sidebar_details").css({
                    'display': 'block',
                    'left': 0
                });
                in_scroll = false;
            } else {
                $("#product_sidebar_details").css({
                    'display': 'none',
                    'left': scrollPositionX + 'px'
                });
                in_scroll = true;
            }
        }
        )
        .jScrollPane();

        $("#product_slider_details").hover( function() {
            $("#product_sidebar_details").css('display', 'block');
        }, function() {
            if (in_scroll == true) {
                $("#product_sidebar_details").css('display', 'none');
            }
        });
    }
    
    if ($("#right_products_over").length > 0) {
        
        $("#right_products_over .product_entry").css('display', 'block');
        
        $("#image_overlay").css('display', 'block');
        var slider_width = $("#right_products_over div.product_entry").last().outerWidth() * $("#right_products_over div.product_entry").length;
        slider_width = Math.floor(slider_width / 2) + $("#right_products_over div.product_entry").last().outerWidth() + 295;
        
        $("#right_products_over").css(
        'width',
        slider_width + 'px');
        
        $('#right_products_stock').jScrollPane();
    }
    
    if ($("#image_overlay").length > 0) {
        var overlay_height = $("#body").height() - 4;
        $("#image_overlay").height(overlay_height);
    }
    
    if ($("#contact_sidebar").length > 0) {

        $("#image_overlay").css('display', 'block');        

        var slider_width = $("#contact_sidebar ul li#contact_entries ul li:first").outerWidth() * $("#contact_sidebar ul li#contact_entries ul").length;       
        slider_width = Math.floor(slider_width / 2) + $("#contact_sidebar ul li#contact_entries ul li:first").outerWidth() + 295;
        
        // compensate for the first col and the padding
        var contact_image_map_width = slider_width - 320 - 40;
        
        if (contact_image_map_width < 300)
        {
            contact_image_map_width = 300;
        }

        if (slider_width < contact_image_map_width + 320) {
            slider_width = contact_image_map_width + 400;
        }
        
        $("#contact_image_map").css('width', contact_image_map_width + 'px');
        
        // set the same width to the item's li as well        
        $("#contact_entries").css('width', contact_image_map_width + 'px');
        
        // overwrite the height of the image_overlay
        var image_overlay_height = $("#image_overlay").height() - 438;
        $("#image_overlay").height(image_overlay_height)

        $("#contact_sidebar ul").css(
        'width',
        slider_width + 'px');

        $("#contact_main").css('height', $("#contact_sidebar").height());
        
        /*$("#contact_sidebar ul li.contact h3").equalHeights();
        $("#contact_sidebar ul li.contact div").equalHeights();*/
        $("#contact_sidebar ul li.contact").equalHeights();
        
        
        //$('#contact_sidebar').jScrollPane();
        
    }
    
    if ($("#contact_image_map").length > 0) {
        var initial_height = $(".jspContainer").height();
        
        $("area").hover(function() {
            var request_url = $(this).attr('href');
            
            //$("#request_catalogue_link").remove();
            //$("#family_description").remove();
            //$("#request_catalogue_text").remove();
            $("#contact_entries ul").html('<img src="' + BASE_URI + 'addons/themes/minimal/img/ajax-loader.gif" />');
            
            $.get(request_url, function(data){
                $("#contact_entries ul").html(data);
                
                var contact_entries_height = 0;
                if ($("#contact_entries").outerHeight(true) < 400)
                {
                    contact_entries_height = 400;
                }       
                else
                {
                    contact_entries_height = $("#contact_entries").outerHeight(true);
                    
                    var request_sidebar_height = 60 + contact_entries_height;
                                    
                    $("#contact_main").css('height', request_sidebar_height + 'px');
                }     
                
                var jspContainer_height = initial_height + contact_entries_height;
                
                $(".jspContainer").height(jspContainer_height + 'px');
                
            }, 'html');            
            
             
        }, function() {
            
        });
		
		$("area").click(function() {
			if ($(this).attr('opt_email')) {
				window.location = "mailto:" + $(this).attr('opt_email') + "?cc=sales@nulux.com&subject=Request a Catalog";
			}
		}); 

    }
});
