jQuery(document).ready(function() {
	/* Search */
	item_id='';
	
	if(jQuery('input[name=\'item_param_id\']').val())
		item_id=jQuery('input[name=\'item_param_id\']').val();
	
	jQuery('.button-search').bind('click', function() {
		url = 'index.php?option=com_opencart&'+item_id+'route=product/search';
		 
		var filter_name = jQuery('input[name=\'filter_name\']').attr('value')
		
		if (filter_name) {
			url += '&filter_name=' + encodeURIComponent(filter_name);
		}
		
		location = url;
	});
	
	jQuery('#header_ext input[name=\'filter_name\']').keydown(function(e) {
		if (e.keyCode == 13) {
			url = 'index.php?option=com_opencart&'+item_id+'route=product/search';
			 
			var filter_name = jQuery('input[name=\'filter_name\']').attr('value')
			
			if (filter_name) {
				url += '&filter_name=' + encodeURIComponent(filter_name);
			}
			
			location = url;
		}
	});
	
	/* Ajax Cart */
	jQuery('#cart > .heading a').bind('click', function() {
		jQuery('#cart').addClass('active');
		
		jQuery.ajax({
			url: 'index.php?option=com_opencart&'+item_id+'tmpl=component&route=checkout/cart/update',
			dataType: 'json',
			success: function(json) {
				if (json['output']) {
					jQuery('#cart .content_ext').html(json['output']);
				}
			}
		});			
		
		jQuery('#cart').bind('mouseleave', function() {
			jQuery(this).removeClass('active');
		});
	});
	
	/* Mega Menu */
	jQuery('#menu_ext ul > li > a + div').each(function(index, element) {
		// IE6 & IE7 Fixes
		if (jQuery.browser.msie && (jQuery.browser.version == 7 || jQuery.browser.version == 6)) {
			var category = jQuery(element).find('a');
			var columns = jQuery(element).find('ul').length;
			
			jQuery(element).css('width', (columns * 143) + 'px');
			jQuery(element).find('ul').css('float', 'left');
		}		
		
		var menu = jQuery('#menu_ext').offset();
		var dropdown = jQuery(this).parent().offset();
		
		i = (dropdown.left + jQuery(this).outerWidth()) - (menu.left + jQuery('#menu_ext').outerWidth());
		
		if (i > 0) {
			jQuery(this).css('margin-left', '-' + (i + 5) + 'px');
		}
	});

	// IE6 & IE7 Fixes
	if (jQuery.browser.msie) {
		if (jQuery.browser.version <= 6) {
			jQuery('#column-left + #column-right + #content_ext, #column-left + #content_ext').css('margin-left', '195px');
			
			jQuery('#column-right + #content_ext').css('margin-right', '195px');
		
			jQuery('.box-category ul li a.active + ul').css('display', 'block');	
		}
		
		if (jQuery.browser.version <= 7) {
			jQuery('#menu_ext > ul > li').bind('mouseover', function() {
				jQuery(this).addClass('active');
			});
				
			jQuery('#menu_ext > ul > li').bind('mouseout', function() {
				jQuery(this).removeClass('active');
			});	
		}
	}
});

jQuery('.success img, .warning img, .attention img, .information img').live('click', function() {
	jQuery(this).parent().fadeOut('slow', function() {
		jQuery(this).remove();
	});
});

function addToCart1(product_id) {
	jQuery.ajax({
		url: 'index.php?option=com_opencart&'+item_id+'tmpl=component&route=checkout/cart/update',
		type: 'post',
		data: 'product_id=' + product_id,
		dataType: 'json',
		success: function(json) {
			jQuery('.success, .warning, .attention, .information, .error').remove();
			
			if (json['redirect']) {
				location = json['redirect'];
			}
			
			if (json['error']) {
				if (json['error']['warning']) {
					jQuery('#notification').html('<div class="warning" style="display: none;">' + json['error']['warning'] + '<img src="components/com_opencart/catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				}
			}	 
						
			if (json['success']) {
				jQuery('#notification').html('<div class="attention" style="display: none;">' + json['success'] + '<img src="components/com_opencart/catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				
				jQuery('.attention').fadeIn('slow');
				
				jQuery('#cart_total').html(json['total']);
				jQuery('#cart_module .content_ext').html(json['output'].replace(/<td class="image">/g,'<td style="display:none;" class="image">'));				
				jQuery('html, body').animate({ scrollTop: 0 }, 'slow'); 
			}	
		}
	});
}



function addToCart(product_id) {
	jQuery('.button_ext').bind('click', function() {
		url = 'index.php?option=com_gmapfp&view=gmapfp&layout=groupe&catid=7&id_perso=0&Itemid=3';
		location = url;
	});
}







function removeCart(key) {
	jQuery.ajax({
		url: 'index.php?option=com_opencart&'+item_id+'tmpl=component&route=checkout/cart/update',
		type: 'post',
		data: 'remove=' + key,
		dataType: 'json',
		success: function(json) {
			jQuery('.success, .warning, .attention, .information').remove();
			
			if (json['output']) {
				jQuery('#cart_total').html(json['total']);
				
				jQuery('#cart .content_ext').html(json['output']);
				jQuery('#cart_module .content_ext').html(json['output'].replace(/<td class="image">/g,'<td style="display:none;" class="image">'));
			}			
		}
	});
}

function removeVoucher(key) {
	jQuery.ajax({
		url: 'index.php?option=com_opencart&'+item_id+'tmpl=component&route=checkout/cart/update',
		type: 'post',
		data: 'voucher=' + key,
		dataType: 'json',
		success: function(json) {
			jQuery('.success, .warning, .attention, .information').remove();
			
			if (json['output']) {
				jQuery('#cart_total').html(json['total']);
				
				jQuery('#cart .content_ext').html(json['output']);
				jQuery('#cart_module .content_ext').html(json['output'].replace(/<td class="image">/g,'<td style="display:none;" class="image">'));
			}			
		}
	});
}

function addToWishList(product_id) {
	jQuery.ajax({
		url: 'index.php?option=com_opencart&'+item_id+'tmpl=component&route=account/wishlist/update',
		type: 'post',
		data: 'product_id=' + product_id,
		dataType: 'json',
		success: function(json) {
			jQuery('.success, .warning, .attention, .information').remove();
						
			if (json['success']) {
				jQuery('#notification').html('<div class="attention" style="display: none;">' + json['success'] + '<img src="components/com_opencart/catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				
				//jQuery('.attention').fadeIn('slow');
				
				jQuery('#wishlist_total').html(json['total']);
				jQuery('html, body').animate({ scrollTop: 0 }, 'slow'); 				
			}	
		}
	});
}

function addToCompare(product_id) {
	jQuery.ajax({
		url: 'index.php?option=com_opencart&'+item_id+'tmpl=component&route=product/compare/update',
		type: 'post',
		data: 'product_id=' + product_id,
		dataType: 'json',
		success: function(json) {
			jQuery('.success, .warning, .attention, .information').remove();
						
			if (json['success']) {
				/*jQuery('#notification').html('<div class="attention" style="display: none;">' + json['success'] + '<img src="components/com_opencart/catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');*/
				jQuery('#notification').html('<div class="attention" style="display: none;">' + json['success'] + '</div>');
				
				jQuery('.attention').fadeIn('slow');
				
				jQuery('#compare_total').html(json['total']);
				jQuery('html, body').animate({ scrollTop: 0 }, 'slow'); 
			}	
		}
	});
}

