$(document).ready(function() {
	
noSpam = function( user, domain ){
    document.location = 'mailto:' + user + '@' + domain;
}
	
	/* Search */
	$('#button-search').bind('click', function() {
		url = $('base').attr('href') + 'index.php?route=product/cauta';
		 
		var filter_name = $('input[name=\'filter_name\']').attr('value');
		
		if (filter_name) {
			url += '&filter_name=' + encodeURIComponent(filter_name);
		}
		
		location = url;
		
	});

function getSearch() {	
		var tip =  $('select[name=\'tip\']').attr('value');		
		var lat =  $('select[name=\'latime\']').attr('value');
		var inl =  $('select[name=\'inaltime\']').attr('value');
		var dim =  $('select[name=\'diametru\']').attr('value');
		var mar =  $('select[name=\'marca\']').attr('value');
		
				$.ajax({
					url: 'index.php?route=product/cauta/getSearch&tip='+ tip + '&latime='+ lat + '&inaltime=' + inl + '&diametru='+ dim+ '&marca=' + mar,
					dataType: 'json',
					contentType: "application/json; charset=utf-8",
					beforeSend: function() {
                                                $('#pleasewait').show();
						$('#pleasewait').righttop();
					},
					complete: function() { 
						$('#pleasewait').hide();
					},
					success: function(result) {
						for(var y in result){
						if (result[y].hasOwnProperty) {
							for(var i in result[y]){
								//alert(i);
								if (i == "latime") {
									$("#filtru-latime").get(0).options.length = 0;
							
									if (result[y][i].hasOwnProperty) {
									$("#filtru-latime").get(0).options[0] = new Option("-- Toate --", "");
											for(var j in result[y][i]) {
												if (result[y][i][j].val == lat) {
													$("#filtru-latime").get(0).options[$("#filtru-latime").get(0).options.length] = new Option(result[y][i][j].val, result[y][i][j].val, true, true);	
												} else {
													$("#filtru-latime").get(0).options[$("#filtru-latime").get(0).options.length] = new Option(result[y][i][j].val, result[y][i][j].val);
												}
												// alert(result[0][i][j].val);
											}
									}
								}
								if (i == "inaltime") {
									$("#filtru-inaltime").get(0).options.length = 0;
							
									if (result[y][i].hasOwnProperty) {
									$("#filtru-inaltime").get(0).options[0] = new Option("-- Toate --", "");
											for(var j in result[y][i]) {
												if (result[y][i][j].val == inl) {
													$("#filtru-inaltime").get(0).options[$("#filtru-inaltime").get(0).options.length] = new Option(result[y][i][j].val, result[y][i][j].val, true, true);	
												} else {
													$("#filtru-inaltime").get(0).options[$("#filtru-inaltime").get(0).options.length] = new Option(result[y][i][j].val, result[y][i][j].val);
												}
												// alert(result[0][i][j].val);
											}
									}
								}
								if (i == "diametru"){
									$("#filtru-diametru").get(0).options.length = 0;
							
									if (result[y][i].hasOwnProperty) {
									$("#filtru-diametru").get(0).options[0] = new Option("-- Toate --", "");
											for(var j in result[y][i]) {
												if (result[y][i][j].val == dim) {
													$("#filtru-diametru").get(0).options[$("#filtru-diametru").get(0).options.length] = new Option(result[y][i][j].val, result[y][i][j].val, true, true);	
												} else {
													$("#filtru-diametru").get(0).options[$("#filtru-diametru").get(0).options.length] = new Option(result[y][i][j].val, result[y][i][j].val);
												}
												// alert(result[0][i][j].val);
											}
									}									
								}
								/*
								if (i == "marca"){
									$("#filtru-marca").get(0).options.length = 0;
							
									if (result[y][i].hasOwnProperty) {
									$("#filtru-marca").get(0).options[0] = new Option("-- Toate --", "");
											for(var j in result[y][i]) {
												if (result[y][i][j].val == mar) {
													$("#filtru-marca").get(0).options[$("#filtru-marca").get(0).options.length] = new Option(result[y][i][j].name, result[y][i][j].val, true, true);	
												} else {
													$("#filtru-marca").get(0).options[$("#filtru-marca").get(0).options.length] = new Option(result[y][i][j].name, result[y][i][j].val);
												}
												// alert(result[0][i][j].val);
											}
									}										
								}*/
							}
						} 
						}

					},         
					error: function() {
						// alert("Faild to getSearch!");
					}
				});
				
}

$("#filtru-tip").bind("change", function() { 
var tip = $(this).val();
	if (tip == "" || tip == "undefined") {
		$("#filtru-latime").get(0).options.length = 0;
		$("#filtru-inaltime").get(0).options.length = 0;
		$("#filtru-diametru").get(0).options.length = 0;
		//$("#filtru-marca").get(0).options.length = 0;
	} else {
		$("#filtru-latime").get(0).options.length = 0;
		$("#filtru-inaltime").get(0).options.length = 0;
		$("#filtru-diametru").get(0).options.length = 0;
		//$("#filtru-marca").get(0).options.length = 0;
		getSearch();
	}
});
				
$("#filtru-latime").bind("change", function() { 
var lat = $(this).val();
	if (lat == "" || lat == "undefined") { 
		$("#filtru-inaltime").get(0).options.length = 0;
		$("#filtru-diametru").get(0).options.length = 0;
		//$("#filtru-marca").get(0).options.length = 0;
	} else {
		$("#filtru-inaltime").get(0).options.length = 0;
		$("#filtru-diametru").get(0).options.length = 0;
		//$("#filtru-marca").get(0).options.length = 0;
		getSearch();
	}
});

$("#filtru-inaltime").bind("change", function() {
var inl = $(this).val();
	if (inl == "" || inl == "undefined") { 
		$("#filtru-diametru").get(0).options.length = 0;
		//$("#filtru-marca").get(0).options.length = 0;
	} else {
		$("#filtru-diametru").get(0).options.length = 0;
		//$("#filtru-marca").get(0).options.length = 0;
		getSearch();
	}
});

$("#filtru-diametru").bind("change", function() {
var dim = $(this).val();
	if (dim == "" || dim == "undefined") { 
		//$("#filtru-marca").get(0).options.length = 0;
	} else {
		//$("#filtru-marca").get(0).options.length = 0;
		getSearch();
	}
});

	/* Custom */ /*
	$('.button-cauta').bind('click', function() {
		alert("CUSTOM");
		
		url = $('base').attr('href') + 'index.php?route=product/cauta';
		 
		var filter_name = $('input[name=\'filter_name\']').attr('value')
		
		if (filter_name) {
			url += '&filter_name=' + encodeURIComponent(filter_name);
		}
		
		location = url;
	});
	*/
	

	$("#button-cautare").click(function(){ cautare(); });

	function cautare(){
				
			url = $('base').attr('href') + 'index.php?route=product/cauta';
			 
			var filter_name = $('input[name=\'filter_name\']').attr('value')
			var tip =  $('select[name=\'tip\']').attr('value')
			var latime =  $('select[name=\'latime\']').attr('value')
			var inaltime =  $('select[name=\'inaltime\']').attr('value')
			var diametru =  $('select[name=\'diametru\']').attr('value')
			var marca =  $('select[name=\'marca\']').attr('value')
			var sezon =  $('select[name=\'sezon\']').attr('value')
			selecttip = document.getElementById('filtru-tip');
			
			var error = "";
						
			
			if (filter_name) {
				url += '&filter_name=' + encodeURIComponent(filter_name);
			}
			if (tip) {
				url += '&tip=' + encodeURIComponent(tip);
			} else if (tip == "" && filter_name == "") {
				//alert ( "Va rugam sa selectati campul tip!" );
				selecttip.focus(); 
				//error = 1;	
			}
			if (latime) {
				url += '&latime=' + encodeURIComponent(latime);
			}
			if (inaltime) {
				url += '&inaltime=' + encodeURIComponent(inaltime);
			}
			if (diametru) {
				url += '&diametru=' + encodeURIComponent(diametru);
			}
			if (marca) {
				url += '&marca=' + encodeURIComponent(marca);
			}
			if (sezon) {
				url += '&sezon=' + encodeURIComponent(sezon);
			}
					
		if (error == "") {
			location = url;
		}
	}
	

	$('#header input[name=\'filter_name\']').keydown(function(e) {
		if (e.keyCode == 13) {
			url = $('base').attr('href') +  'index.php?route=product/cauta';
			 
			var filter_name = $('input[name=\'filter_name\']').attr('value')
			
			if (filter_name) {
				url += '&filter_name=' + encodeURIComponent(filter_name);
			}
			
			location = url;
		}
	});
	
	/* 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 .content, #module_cart .cart-module').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).css('width', (columns * 143) + 'px');
			$(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/nico2/image/close.png" alt="" class="close" /></div>');
				}
			}	 
						
			if (json['success']) {
				
				//$('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/nico2/image/close.png" alt="" class="close" /></div>');
				
				//$('.success').fadeIn('slow');
				
				//$('#cart_total').html(json['total']);
				
				//$('.block .cart-module').html(json['output']); // cart widget contribution
				
				//$('html, body').animate({ scrollTop: 0 }, 'slow');
				
				window.location='index.php?route=checkout/cart';  
			}	
		}
	});
}

function removeCart(key) {
	$.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']);
				$('#cart .content, .block .cart-module').html(json['output']);
				window.location='index.php?route=checkout/cart';
			}
						
		}
	});
}

function removeCart2(key) {
	$.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']);
				$('#cart .content, .block .cart-module').html(json['output']);
				
				
			}
			if (json['number_items']) {
				$('#number_items').html(json['number_items']);
			}
			
						
		}
	});
}

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']);
				$('#cart .content, .block .cart-module').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/nico2/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/nico2/image/close.png" alt="" class="close" /></div>');
				
				$('.attention').fadeIn('slow');
				
				$('#compare_total').html(json['total']);
				
				$('html, body').animate({ scrollTop: 0 }, 'slow'); 
			}	
		}
	});
}
