$(document).ready(function() {
	   
    /* Add Phone Number */
    showing = false;
    
    $('#showAddPhone').bind('click', function(){
        if(showing != true) {
            $('#phoneAdd').show('fast');
            showing = true;
        } else {
            $('#phoneAdd').hide('fast');
            
            $('#status_image').html('');
            $('#phoneName, #phoneNumber').css('border', '1px solid #e3e9ef').val('');
            
            showing = false;    
        }
    })
    
    $('#closePhoneAdd').bind('click', function(){
        $('#phoneAdd').hide('fast');
        $('#status_image').html('');
        $('#phoneName, #phoneNumber').css('border', '1px solid #e3e9ef').val('');
        
        showing = false;
    });
    
    $('#phoneAddNumber').click(function(){
        p_name   = $('#phoneName').val();
        p_number = $('#phoneNumber').val();

        if(p_name && p_number) {
            $('#status_image').html('<img src="image/templates/ajax-loader.gif" alt="" />');
            $('#phoneName, #phoneNumber').css('border', '1px solid #e3e9ef');
            
            $.ajax({
                url: 'index.php?route=module/phones/add',
                type: 'post',
                data:     'p_name=' + p_name + '&p_number=' + p_number,
                dataType: 'json',
                success: function(data) {
                    if(data['result'] == 'success')
                        $('#status_image').html('<img src="image/templates/success.png" alt="" />');
                    else
                        $('#status_image').html('<img src="image/templates/warning.png" alt="" />');      
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    $('#status_image').html('<img src="image/templates/warning.png" alt="" />');
                }
            });   
        } else {
            $('#status_image').html('<img src="image/templates/warning.png" alt="" />');
            $('#phoneName, #phoneNumber').css('border', '1px solid #ff0000');
        }
    });
    
    
    /* Search */
	$('.search_form .but').bind('click', function() {
		url = 'index.php?route=product/search';
		 
		var filter_name = $('.search_form .str').attr('value')
		
		if (filter_name) {
			url += '&filter_name=' + encodeURIComponent(filter_name);
		}
		
		location = url;
	});
	
	$('.search_form .str').keydown(function(e) {
		if (e.keyCode == 13) {
			url = 'index.php?route=product/search';
			 
			var filter_name = $('.search_form .str').attr('value')
			
			if (filter_name) {
				url += '&filter_name=' + encodeURIComponent(filter_name);
			}
			
			location = url;
		}
	});
	
	/* Ajax Cart */
	$('#addItemToCart').bind('click', function() {
		$('#cart').addClass('active');
		
		$.ajax({
			url: 'index.php?route=checkout/cart/update',
			dataType: 'json',
			success: function(json) {
				if (json['output']) {
					$('.product .order').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');
			});	
		}
	}
    
    
    $('#productQuantity').change(function(){
        $('#statusImage').html('<img src="images/templates/ajax-loader.gif" alt="" />');
        p_quantity = $(this).val();
    });
    
    
});

$('.success img, .warning img, .attention img, .information img').live('click', function() {
	$(this).parent().fadeOut('slow', function() {
		$(this).remove();
	});
});

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']);
			}			
		}
	});
}

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'); 
			}	
		}
	});
}

