$(document).ready(function() {
	/* Search */
	$('.button-search').bind('click', function() {
		url = 'index.php?route=product/search';
		 
		var filter_name = $('input[name=\'filter_name\']').attr('value')
		
		if(filter_name == 'Subscribe newsletter with your email id')
		filter_name = '';
			
		if(filter_name == 'Type your brand...'){
			$("#header #searchbox #search").css('color','#A00');
			return;
		}
		
		if (filter_name) {
			url += '&filter_name=' + encodeURIComponent(filter_name);
		}
		
		location = url;
	});
	
	$('input[name=\'filter_name\']').keydown(function(e) {
		if (e.keyCode == 13) {
			
			url = 'index.php?route=product/search';
			 
			var filter_name = $('input[name=\'filter_name\']').attr('value')
			
			if(filter_name == 'Type your brand...'){
				$("#header #searchbox #search").css('color','#A00');
				return;
			}
			
			if (filter_name) {
				url += '&filter_name=' + encodeURIComponent(filter_name);
			}
			location = url;
		}
	});
	
	/* Order Tracking */
	
	$('#order_tracking .heading').bind('click',function(){

	  	$('#order_tracking').addClass('active');	
		
		$('#order_tracking .content').show();
		
		$('#order_tracking').bind('mouseleave', function() {
			$(this).removeClass('active');
			$('#order_tracking .content').hide();
		});
	
	});
	
	/* 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).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/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');
				
				$('#MyCart').html(json['total']); 

				$('#CartDropDown').html(json['output']);
				
				$('html, body').animate({ scrollTop: 0 }, 'slow'); 
				$('#MyCart').trigger('mousehover');
			}	
		}
	});
}

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']) {
				 $('#MyCart').html(json['total']+'<div id="CartDropDown"></div>')
				 $('#CartDropDown').html(json['output']);
			}			
		}
	});
}

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="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				
				$('.success').fadeIn('slow');
				
				$('#wishlist_total').html(json['total']);
				
				$('html, body').animate({ scrollTop: 0 }, 'slow'); 				
			}else{
				
			   $("#login_type").fancybox().trigger('click');
	
			}	
		}
	});
}

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'); 
			}	
		}
	});
}

/* Order tracking */ 
function check_orderstatus(){
	
  $.ajax({
			type: 'post',
			url: 'index.php?route=module/ordertracking/checkstatus',
			dataType: 'html',
            data:$("#TrackOrderForm").serialize(),
			success: function (html) {
				$('#TrackOrderResults').html(html);
			}}); 
  }

  $('#order_id').keydown(function(e) {
	if (e.keyCode == 13) {
		check_orderstatus();
	}
});
/* end order tracking */

/* NUMMBER ONLY */

function numbersonly(myfield, e, dec){
	var key;
	var keychar;
	
	if (window.event)
	   key = window.event.keyCode;
	else if (e)
	   key = e.which;
	else
	   return true;
	keychar = String.fromCharCode(key);

	// control keys
	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) ){
	   return true;
	}
	// numbers
	else {	
		if ((("0123456789.").indexOf(keychar) > -1)){
			return true;
		}else{
			// decimal point jump
			if (dec && (keychar == ".")){			 	
				//myfield.form.elements[dec].focus();
				var deci = document.getElementById(myfield).value.indexOf(".");
				if(deci!=-1)return false;
				return true;
			} else {
				//document.getElementById(myfield).focus();
				return false;
			}
		}
	}
}
/* END NUMMBER ONLY */

$(document).ready(function(){
	
  //Jq image tool tip start 
  $('.zoom1').mouseenter(function(){
	  show_thumb_popup(this);
    });
  	
  $('.zoom1').mouseout(function(){
	$('.image_zoom').remove();
  });
	
});

function show_thumb_popup(obj) {
	img_path = $(obj).attr('rel');
	    
	if($(obj).attr('lang')=="left") {
	  W_position = $(obj).position();
	  N_position_top = W_position.top -100+12;
	  N_position_left = W_position.left - (4*100 + 10);
	  $(obj).append('<div class="image_zoom"><img src="'+img_path+'"></div>');
  	  $('.image_zoom').css({'top':N_position_top,'left':N_position_left});
	  //$('.image_zoom').slideDown('slow');
	  $('.image_zoom').show();
	}else if($(obj).attr('lang')=="right"){
	  W_position = $(obj).position(); 
	  N_position_top = W_position.top - 100;
	  N_position_left = W_position.left + 100  + 25; 
	  $(obj).append('<div class="image_zoom"><img src="'+img_path+'"></div>');
  	  $('.image_zoom').css({'top':N_position_top,'left':N_position_left});
	  //$('.image_zoom').slideDown('slow'); 
	  $('.image_zoom').show();
	}

	if($(obj).attr('lang')=="left") {	
     	$('.zoom1 img').jrumble({
		rumbleEvent: 'hover',
		rangeX: 1,
		rangeY: 1,
		rangeRot: 1,
		rumbleSpeed:50
	  });	 
	}
}
