(function($){jQuery.fn.canvasSwap=function(options){var options=jQuery.extend({},jQuery.fn.canvasSwap.defaults,options);jQuery(this).hover(function(){var thesrc=$(this).attr('src');var name=thesrc.substring(0,thesrc.lastIndexOf('.'));var extension=thesrc.substring(thesrc.lastIndexOf('.'));$(this).attr('src',name+options.suffix+extension);},function(){var thesrc=$(this).attr('src');var name=thesrc.substring(0,thesrc.lastIndexOf('.')-options.suffix.length);var extension=thesrc.substring(thesrc.lastIndexOf('.'));$(this).attr('src',name+extension);});};jQuery.fn.canvasSwap.defaults={suffix:'_over',ie6_support:false}})(jQuery);

function doSearch() {
	url = 'index.php?route=product/search';
	 
	var filter_name = $('input[name=\'filter_name\']').attr('value')
	
	if (filter_name) {
		url += '&filter_name=' + encodeURIComponent(filter_name);
	}
	
	location = url;
}
$(document).ready(function() {
	/* Search */
	$('.button-search').bind('click', function() {
		doSearch();
	});
	
	$('.swap').canvasSwap();
	
	$('input[name=\'filter_name\']').keydown(function(e) {
		if (e.keyCode == 13) {
			doSearch();
		}
	});
	
	/* Ajax Cart */
	$('#cart > .heading a').bind('click', function() {
		$('#cart').addClass('active');
		
		$.ajax({
			url: 'index.php?route=checkout/cart/update',
			dataType: 'json',
			success: function(json) {
				if (json['output']) {
					$('#cart .content').html(json['output']);
				}
			}
		});			
		
		$('#cart').bind('mouseleave', function() {
			$(this).removeClass('active');
		});
	});
	
	/* Mega Menu */
	$('#menu ul > li > a + div').each(function(index, element) {
		// IE6 & IE7 Fixes
		if ($.browser.msie && ($.browser.version == 7 || $.browser.version == 6)) {
			var category = $(element).find('a');
			var columns = $(element).find('ul').length;
			
			$(element).find('ul').css('float', 'left');
		}		
		
		var menu = $('#menu').offset();
		var dropdown = $(this).parent().offset();
		
		i = (dropdown.left + $(this).outerWidth()) - (menu.left + $('#menu').outerWidth());
		
		if (i > 0) {
			$(this).css('margin-left', '-' + (i + 5) + 'px');
		}
	});

	// IE6 & IE7 Fixes
	if ($.browser.msie) {
		if ($.browser.version <= 6) {
			$('#column-left + #column-right + #content, #column-left + #content').css('margin-left', '195px');
			
			$('#column-right + #content').css('margin-right', '195px');
		
			$('.box-category ul li a.active + ul').css('display', 'block');	
		}
		
		if ($.browser.version <= 7) {
			$('#menu > ul > li').bind('mouseover', function() {
				$(this).addClass('active');
			});
				
			$('#menu > ul > li').bind('mouseout', function() {
				$(this).removeClass('active');
			});	
		}
	}
});

$('.success img, .warning img, .attention img, .information img').live('click', function() {
	$(this).parent().fadeOut('slow', function() {
		$(this).remove();
	});
});

function addToCart(product_id) {
	$.ajax({
		url: 'index.php?route=checkout/cart/update',
		type: 'post',
		data: 'product_id=' + product_id,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information, .error').remove();
			
			if (json['redirect']) {
				location = json['redirect'];
			}
			
			if (json['error']) {
				if (json['error']['warning']) {
					$('#notification').html('<div class="warning" style="display: none;">' + json['error']['warning'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				}
			}	 
						
			if (json['success']) {
				$('#notification').html('<div class="attention" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				
				$('.attention').fadeIn('slow');
				
				$('#cart_total').html(json['total']);
				
				$('html, body').animate({ scrollTop: 0 }, 'slow'); 
			}	
		}
	});
}

function removeCart(key, callback) {
	$.ajax({
		url: 'index.php?route=checkout/cart/update',
		type: 'post',
		data: 'remove=' + key,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information').remove();
			
			if (json['output']) {
				$('#cart_total').html(json['total']);
				
				$('#cart .content').html(json['output']);
			}			
			if (callback != undefined)
				callback();
		}
	});
}

function removeVoucher(key) {
	$.ajax({
		url: 'index.php?route=checkout/cart/update',
		type: 'post',
		data: 'voucher=' + key,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information').remove();
			
			if (json['output']) {
				$('#cart_total').html(json['total']);
				
				$('#cart .content').html(json['output']);
			}			
		}
	});
}

function addToWishList(product_id) {
	$.ajax({
		url: 'index.php?route=account/wishlist/update',
		type: 'post',
		data: 'product_id=' + product_id,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information').remove();
						
			if (json['success']) {
				$('#notification').html('<div class="attention" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				
				$('.attention').fadeIn('slow');
				
				$('#wishlist_total').html(json['total']);
				
				$('html, body').animate({ scrollTop: 0 }, 'slow'); 				
			}	
		}
	});
}

function addToCompare(product_id) { 
	$.ajax({
		url: 'index.php?route=product/compare/update',
		type: 'post',
		data: 'product_id=' + product_id,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information').remove();
						
			if (json['success']) {
				$('#notification').html('<div class="attention" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				
				$('.attention').fadeIn('slow');
				
				$('#compare_total').html(json['total']);
				
				$('html, body').animate({ scrollTop: 0 }, 'slow'); 
			}	
		}
	});
}

function checkoutRemove(product_id) {
	removeCart (product_id, function() {
		location.reload(true);
	});
}

function pu_close(){
	$("#pu,#bg").fadeOut();
}

function pu_show(){
	$("#pu,#bg").fadeIn();
}

function contact(){
	
	// Validation
	var err = "";
	var email = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	
	if(!$("#contact_name").val()){ err = "יש להכניס את שמך"; $("#contact_name").focus(); }
	
	if((!err && !$("#contact_email").val()) || (!err && !email.test($("#contact_email").val()))){ err = "יש להכניס כתובת מייל תקינה"; $("#contact_email").focus(); }
	
	if(!$("#contact_subject").val()){ err = "יש להכניס נושא"; $("#contact_name").focus(); }
	
	if(!err && !$("#contact_message").val()){ err = "יש להכניס את תוכן ההודעה"; $("#contact_message").focus(); }
	
	
	if(err){
		$("#pu .err").html(err).show();
		return false;
	}else{
		$("#pu p.desc").show();
		$("#pu .err").hide();
		$("#pu .loading").show();
		$("$contact_form").submit();	/*$.post('/index.php?route=information/contact',{name:$("#contact_name").val(),email:$("#contact_email").val(),email_subject:$("#contact_subject").val(),enquiry:$("#contact_message").val()},function(r){
			$("#pu .loading").hide();
			if(r.length < 3){
				$("#pu p.desc").html('תודה, הודעתך נשלחה!');
				//$("#pu .row,#pu .hsep,#pu .btn_continue").hide();
			}else{
				alert('אנו מצטערים, אירעה שגיאה במהלך השליחה!');
			}
		});*/
	}
}

