$.fn.dropdown = function(options) {
	var defaults = {
		hideSpeed: 50,
		showSpeed: 350,
		parentBGHoverColor: false,
		parentTextHoverColor: false,
		zIndex: 5000
	};
	
    var settings = $.extend({}, defaults, options);

    this.each(function() {
		
        $(this).find("li > ul").css("display", "none");
        
        $(this).find("li > ul").parent().hover(
			function(){
				show(this);
			},
			
			function(){
				hide(this);
			}
		);
    });
    
    function show(menu){
		if($(menu).children("li > ul").css("display") == "none"){
			
			$(menu).attr("showing", "1");
			
			if(settings.parentBGHoverColor){
				if(!$(menu).attr("dropdownParentOriginalBGColor")){$(menu).attr("dropdownParentOriginalBGColor", $(menu).css("background-color"));}
				$(menu).css("background-color", settings.parentBGHoverColor);
			}
			
			if(settings.parentTextHoverColor){
				if(!$(menu).attr("dropdownParentOriginalTextColor")){$(menu).attr("dropdownParentOriginalTextColor", $(menu).children("span").children("a").css("color"));}
				$(menu).children("span").children("a").css("color", settings.parentTextHoverColor);
			}
			
			$(menu).children("ul").css("position", "absolute").css("left", $(menu).css("left")).css("z-index", settings.zIndex);
			
			$(menu).children("ul").slideDown(settings.showSpeed, function(){
				$(this).parent().attr("showing", "0");
			});
		}
	}
	
	function hide(menu){
		$(menu).children("ul").slideUp(settings.hideSpeed, function(){
			
			if(settings.parentBGHoverColor){
				$(this).parent().css("background-color", $(this).parent().attr("dropdownParentOriginalBGColor"));
			}
			
			if(settings.parentTextHoverColor){
				$(this).parent().children("span").children("a").css("color", $(this).parent().attr("dropdownParentOriginalTextColor"));
			}
		});
	}
    
    return this;
};

$.fn.scrollLoad = function(options) {
	var defaults = {
		showSpeed: 500
	};
	
    var settings = $.extend({}, defaults, options);
    var index = 0;
    var sources = Array();
    var images = Array();
    var scrolling = false;

    this.each(function() {
		
		sources[index] = $(this).attr("src");
		
		$(this).removeAttr("src");
		
		images[index] = this;
		
        $(this).attr("index", index);
		
		index++;        
    });
    
    $(window).scroll(function(){
		if(!scrolling)
		{
			scrolling = true;
			
			showVisibleImages();
			
			scrolling = false;
		}
	});
	
	setTimeout(function(){showVisibleImages();}, 250);
	
	function showVisibleImages(){
		for(var i in images)
		{
			if($(images[i]).attr("src") == undefined && inviewport(images[i], {threshold:0}))
			{
				$(images[i]).css("display", "none").attr("src", sources[$(images[i]).attr("index")]).fadeIn(settings.showSpeed);
			}
		}
	}
    
    function belowthefold(element, settings) {
        var fold = $(window).height() + $(window).scrollTop();
        return fold <= $(element).offset().top - settings.threshold;
    };

    function abovethetop(element, settings) {
        var top = $(window).scrollTop();
        return top >= $(element).offset().top + $(element).height() - settings.threshold;
    };

    function rightofscreen(element, settings) {
        var fold = $(window).width() + $(window).scrollLeft();
        return fold <= $(element).offset().left - settings.threshold;
    };

    function leftofscreen(element, settings) {
        var left = $(window).scrollLeft();
        return left >= $(element).offset().left + $(element).width() - settings.threshold;
    };

    function inviewport(element, settings) {
        return !rightofscreen(element, settings) && !leftofscreen(element, settings) && !belowthefold(element, settings) && !abovethetop(element, settings);
    };
    
    return this;
};

$(document).ready(function(){
	$(".product-info img").scrollLoad();
	
	$("#top-search").focus(function(){
		if($(this).val() == "search...")
		{
			$(this).val("");
		}
		$(this).css("color", "#000");
	});
	
	$("#top-search").blur(function(){
		if($(this).val() == "")
		{
			$(this).val("search...");
		}
		$(this).css("color", "#d3d3d3");
	});
});

