var submenu = false;
$(document).ready(function(){
	
	$("#header .content .menu-1 .menu-products").hover(
		function(){
			$('#product-sub-menu').slideDown();
			submenu = true;
		},
		function (){
			submenu = false;
			setTimeout('hideSubMenu()',200);
		}
	);
	$('#product-sub-menu').hover(
		function(){
			$('#header .content .menu-1 .menu-products a').css('color','#ffffff');
			$('#header .content ul.menu-1 li.menu-products').removeClass('menuItem');
			$('#header .content ul.menu-1 li.menu-products').addClass('activeMenuTab');
			submenu = true;
			$(this).show();
		},
		function(){
			$('#header .content .menu-1 .menu-products a').css('color','#ffffff');
			$('#header .content ul.menu-1 li.menu-products').removeClass('menuItem');
			$('#header .content ul.menu-1 li.menu-products').addClass('activeMenuTab');
			submenu = false;
			setTimeout('hideSubMenu()',200);
		}
	);
	
	$('#header .content ul.menu-1 li.menu-products').hover(function(){
		$('#header .content .menu-1 .menu-products a').css('color','#ffffff');
		$('#header .content ul.menu-1 li.menu-products').removeClass('menuItem');
		$('#header .content ul.menu-1 li.menu-products').addClass('activeMenuTab');
	});
	
	evaluateProductCategoriesAjaxLinks();
});

function hideSubMenu(){
	if(!submenu){
		$('#product-sub-menu').hide();
		$('#header .content .menu-1 .menu-products a').css('color','#414141');
		$('#header .content ul.menu-1 li.menu-products').addClass('menuItem');
		$('#header .content ul.menu-1 li.menu-products').removeClass('activeMenuTab');
	}
}

function appendHeader(path){
	
	$('head').append('<link rel="stylesheet" href="'+path+'" type="text/css" />');
		
}

function lightboxImages(){
	$(".view-market_place .image-file ul li").hover(function() {
		
		var preview = $(this).find('img').attr('src');
		preview = preview.replace("/thumbnail-100/","/original/");
		$(this).find('img').attr('src',preview);
		
		$(this).css({'z-index' : '10'}); /*Add a higher z-index value so this image stays on top*/ 
		$(this).find('img').addClass("hover").stop() /* Add class of "hover", then stop animation queue buildup*/
			.animate({
				marginTop: '-110px', /* The next 4 lines will vertically align this image */ 
				marginLeft: '-110px',
				top: '50%',
				left: '80%',
				width: '200px', /* Set new width */
				height: '150px', /* Set new height */
				padding: '20px'
			}, 200); /* this value of "200" is the speed of how fast/slow this hover animates */
	
		} , function() {
			
		var thumb = $(this).find('img').attr('src');
		thumb = thumb.replace("/original/","/thumbnail-100/");
		$(this).find('img').attr('src',thumb);
			
		$(this).css({'z-index' : '0'}); /* Set z-index back to 0 */
		$(this).find('img').removeClass("hover").stop()  /* Remove the "hover" class , then stop animation queue buildup*/
			.animate({
				marginTop: '0', /* Set alignment back to default */
				marginLeft: '0',
				top: '0',
				left: '50',
				width: '100px', /* Set width back to default */
				height: '75px', /* Set height back to default */
				padding: '5px'
			}, 400);
	});
}

function evaluateProductCategoriesAjaxLinks(){
	
	$("#block_products_onecatalog .view-product_catalogs .record .field-title a").bind('click',function(){
		var urlLink = $(this).attr('href');
		
		$("#block_products_onecatalog .view-product_catalogs .record").removeClass('active');
		$(this).parent().parent().parent().addClass('active');
		
		$(".view-product_categories .record").removeClass('active');
		
		$('#products-container').html("<img align='center' src='/themes/default/images/ajax-loader.gif' alt='' border='0' />");
		$.ajax({
			url : urlLink+"&ajax=1",
			success: function(response){
				$('#products-container').html(response);
				evaluateProductItemsPagination();
			}
		});
		return false;
	});
	
	if( $('#products-container .view-product_items').html() =='No products available at the moment.'){
		
		//check if categories exist
		if( $('.view-product_categories .record-first').html() != '' && $('.view-product_categories .record-first').html() != null){
			
			//get products of first entry and highlight it
			var urlLink = $(".view-product_categories .record-first a").attr('href');
			
			$(".view-product_categories .record").removeClass('active');
			$(".view-product_categories .record-first a").parent().parent().parent().addClass('active');
			
			$('#products-container').html("<img align='center' src='/themes/default/images/ajax-loader.gif' alt='' border='0' />");
			$.ajax({
				url : urlLink,
				success: function(response){
					$('#products-container').html(response);
					evaluateProductItemsPagination();
				}
			});
		}
		else if( $('.view-product_categories .record-last').html() != '' && $('.view-product_categories .record-last').html() != null){
			
			//get products of last entry and highlight it
			var urlLink = $(".view-product_categories .record-last a").attr('href');
			
			$(".view-product_categories .record").removeClass('active');
			$(".view-product_categories .record-last a").parent().parent().parent().addClass('active');
			
			$('#products-container').html("<img align='center' src='/themes/default/images/ajax-loader.gif' alt='' border='0' />");
			$.ajax({
				url : urlLink,
				success: function(response){
					$('#products-container').html(response);
					evaluateProductItemsPagination();
				}
			});
		}
	}
	//bind the click to act as an ajax tab
	$(".view-product_categories .record a").bind('click',function(){
		var urlLink = $(this).attr('href');
		
		$(".view-product_categories .record").removeClass('active');
		$(this).parent().parent().parent().addClass('active');
		
		$('#products-container').html("<img align='center' src='/themes/default/images/ajax-loader.gif' alt='' border='0' />");
		$.ajax({
			url : urlLink,
			success: function(response){
				$('#products-container').html(response);
				evaluateProductItemsPagination();
			}
		});
		return false;
	});
	
	
}

function evaluateProductItemsPagination(){
	
	$('#products-container .pagination ul li a').bind('click',function(){
		var urlLink = $(this).attr('href').split("=");
		urlLink = urlLink[1];
		urlLink = "/products/catalog.html?ajax=1&id="+$.url.param('id')+"&product_items_page="+urlLink;
		
		$('#products-container').html("<img align='center' src='/themes/default/images/ajax-loader.gif' alt='' border='0' />");
		$.ajax({
			url : urlLink,
			success: function(response){
				$('#products-container').html(response);
				evaluateProductItemsPagination();
			}
		});
		return false;
	});
	
}
