function initBinding()
{
	setTimeout(function(){
		$('.new img.lazy').lazyload({
			effect : "fadeIn"
		});

		$('.new img.click-lazy').lazyload({
			event : "sporty"
		});

		$('.new .details').hover(function(e){
			$(this).parent().hide();
			$('.product-overlay', $(this).parent().parent()).show();
		});

		$('.new .product-overlay').hover(function(e){
			// do nothing
		},function(e){
			$(this).hide();
			$('.product-footer', $(this).parent()).show();
		});

		$('.new').removeClass('new');
	}, 250); // this fixes bug in chrome - sometimes images didn't appear before scrolling
}

$(document).ready(function(){
	var loader = $('.loader'),
		searchResults = $('#search-results'),
		searching = false,
		typingDoneTimer,
		resultsShown = false,
		switching = false
		isTyping = false;

	$('#search-box').keyup(function(){
		var val = $(this).val();

		clearTimeout(typingDoneTimer);
		isTyping = true;

		if(resultsShown)
		{
			searchResults.hide();
			resultsShown = false;
		}

		if(val.length >= 3) { // && !searching

			typingDoneTimer = setTimeout(function(){
				searching = true;
				$.ajax({
			        type: "GET",
			        url: suggestionUrl,
			        data: 'query=' + val,
			        async: true,
			        success: function(msg){

			        	if(isTyping)
			        	{
				        	$('ul', searchResults).html('').append(msg);

				        	if(!resultsShown)
				        	{
				            	searchResults.show();
				            }

				            resultsShown = true;
			            }
			            // searching = false;
			        },
			        error: function() {
			        	// do nothing
			        	// searching = false;
			        }
			     });
			}, 250); //fires after 200 ms
		}
		else {
			searchResults.hide();
			resultsShown = false;
		}
	});

	/*$('#search-form').submit(function(e){
		e.preventDefault();
	});*/

	$('#search-box').blur(function() {
		isTyping = false;

		setTimeout(function(){
			searchResults.hide();
			resultsShown = false;
		}, 300);
	});

	if(jQuery().lazyload)
	{
		initBinding();

		$('.fancybox').live('click', function(){
			$("img.click-lazy", $(this).parent()).trigger("sporty");
		});

		$('.next, .previous').live('click', function(e){
			$("img.click-lazy", $(this).parent().parent()).trigger("sporty");
		});
	}

	$('#menu a').hover(
		function () {
    		$('#menu_arrow').css({left: ($(this).width()/2)-6}).appendTo($(this).parent());
  		}
	);

	if(jQuery().fancybox)
	{
		$(".fancybox").fancybox({
			prevEffect	: 'none',
			nextEffect	: 'none',
			helpers	: {
				title	: {
					type: 'outside'
				},
				overlay	: {
					opacity : 0.8,
					css : {
						'background-color' : '#000'
					}
				}/*,
				thumbs	: {
					width	: 50,
					height	: 50
				}*/
			}
		});
	}

	$('#categories-menu a').click(function(e){
		
		if(!switching)
		{
			switching = true;

			$('.products').remove();
			loader.addClass('active');
			$('#categories-menu a.active').removeClass('active');

			$(this).addClass('active');

			$.ajax({
	            type: "GET",
	            url: $(this).attr('href'),
	            async: true,
	            success: function(msg){
	                
	                loader.before(msg);

	                initBinding();

	                loader.removeClass('active');

	                switching = false;
	            },
	            error: function() {
	            	loader.removeClass('active');
	            	switching = false;
	            },
	            statusCode: {
				    404: function() {
				    	// alert('page not found');
					},
					500: function() {
				    	// alert('page not found');
					}
				}
	         });
         }

		e.preventDefault();
	});

	$('.product-navigation .next').live('click', function(e){

		var currentThumbnail = $('.product-thumbnail .current', $(this).parent().parent());

		if(currentThumbnail.next().length == 0) {
			currentThumbnail.removeClass('current');
			$('a:first-child', currentThumbnail.parent()).addClass('current');
		}
		else { 
			currentThumbnail.removeClass('current').next().addClass('current');
		}

		e.preventDefault();
	});

	$('.product-navigation .previous').live('click', function(e){
		var currentThumbnail = $('.product-thumbnail .current', $(this).parent().parent());

		if(currentThumbnail.prev().length == 0) {
			currentThumbnail.removeClass('current');
			$('a:last-child', currentThumbnail.parent()).addClass('current');
		}
		else { 
			currentThumbnail.removeClass('current').prev().addClass('current');
		}

		e.preventDefault();
	});
  
    $(window).scroll(function(){
        if ($(this).scrollTop() == ($(document).height() - $(this).height()) && typeof enable_pagination!="undefined" && !last_result_empty && !is_loading && enable_pagination && nb_pages >= next_page) {
  
            is_loading = true;
            loader.addClass('active');
  
            $.ajax({
                type: "GET",
                url: script_url,
                data: script_data + (script_data==''?'':'&') + 'page=' + next_page,
                async: true,
                success: function(msg){
                    next_page += 1;
                    is_loading = false;
                    loader.removeClass('active');
                    
                    $('.products').append(msg);

                    initBinding();
                },
                statusCode: {
                    404: function() {
                        last_result_empty = true;
                        is_loading = false;
                        loader.removeClass('active');
                    }
                }
             });
        }
    });
});







