this.imagePreview = function(){	
	/* CONFIG */
		
		xOffset = 200;
		yOffset = 30;
		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
		
	/* END CONFIG */
	$("a.preview").hover(function(e){
		
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='preview'><img src='"+ this.href +"' alt='Image preview' />"+ c +"</p>");
	
		$('#preview img').one('load',function(){
			displayType = e.pageX + $('#preview').width() < $(window).width();
			if (displayType) {								 
				$("#preview")
					.css("top",(e.pageY - xOffset) + "px")
					.css("left",(e.pageX + yOffset) + "px")
					.fadeIn("fast");
			} else {
				$("#preview")
					.css("top",(e.pageY - xOffset) + "px")
					.css("left",(e.pageX - $('#preview').width() - yOffset) + "px")
					.fadeIn("fast");
			}	
		})
		.each(function(){
			if(this.complete) $(this).trigger('load');
		});
						
    },
	function(){
		this.title = this.t;	
		$("#preview").remove();
    });	
	$("a.preview").mousemove(function(e){
		if (window.displayType != undefined) {
			if (displayType) {								 
				$("#preview")
					.css("top",(e.pageY - xOffset) + "px")
					.css("left",(e.pageX + yOffset) + "px")
					.fadeIn("fast");
			} else {
				$("#preview")
					.css("top",(e.pageY - xOffset) + "px")
					.css("left",(e.pageX - $('#preview').width() - yOffset) + "px")
					.fadeIn("fast");
			}
		}
	});			
};

function regExpEscape(text){
  if( !arguments.callee.sRE ){
    var specials = [
      '/', '.', '*', '+', '?', '|',
      '(', ')', '[', ']', '{', '}', '\\'
    ];
    arguments.callee.sRE = new RegExp(
      '(\\' + specials.join('|\\') + ')', 'g'
    );
  }
  return text.replace(arguments.callee.sRE, '\\$1');
}
function showLoading(){
	var loadingLeft = ($('body').width() - 85) / 2;	
	var loadingTop = ($('body').height() - 30) / 3;			
	$('body').append('<div class="js_loading" style="top: ' + loadingTop + 'px;left: ' + loadingLeft + 'px;">Loading...</div>');
} 
function hideLoading(){
	$('.js_loading').remove();
}
function jsAddToCart(){

	$('.js_add_to_cart').click(function(){
			
		showLoading();
							
		pattern = new RegExp('^(' + regExpEscape(host) + (window.languageIdentifier ? ('\\/' + languageIdentifier) : '') + ')\\/(.+)\\/([^\\/]+)$');
		var productName = $(this).attr('href').match(pattern);
		productName = productName[2];				

		$.ajax({
			url: 'ajax.php',
			data: (window.languageIdentifier ? ('lang=' + languageIdentifier + '&') : '') + 'ajax=add_to_cart&product=' + productName,
			dataType: 'html',
			cache: false,	
			success: function(data, textStatus){				
									
				$('.js_cart').replaceWith(data);	
				
				hideLoading();
				
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){}
		});
		
		return false;		
				
	});

}
function jsAddToWishList(){

	$('.js_add_to_wishlist').click(function(){
					
		showLoading();
	
		pattern = new RegExp('^(' + regExpEscape(host) + (window.languageIdentifier ? ('\\/' + languageIdentifier) : '') + ')\\/(.+)\\/([^\\/]+)$');
		var productName = $(this).attr('href').match(pattern);
		productName = productName[2];
				
	
		$.ajax({
			url: 'ajax.php',
			data: (window.languageIdentifier ? ('lang=' + languageIdentifier + '&') : '') + 'ajax=add_to_wishlist&product=' + productName,
			dataType: 'html',
			cache: false,	
			success: function(data, textStatus){				
									
				$('.js_wishlist').replaceWith(data);	
				
				hideLoading();
				
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){}
		});
		
		return false;	
				
	});
}

js_popup_image = '';
function js_popup(image){
	image = js_popup_image.length ? js_popup_image : image;
	if (image.length) {
		window.open(host + '/view_image.php?image=' + image, image, 'status=no,scrollbars=yes,resizable=yes,menubar=no,fullscreen=yes');
	}
}
function swapImage(pimg, oimg, oimgf){
	js_popup_image = oimgf;
	$('#main_img a').attr('href', oimg);
	$('#main_img img').attr('src', pimg);
}

// starting the script on page load
$(document).ready(function(){

	$('.hide_onload').hide();
	
	$('.submit_onchange').change(function(){
		$(this).parents('form').submit();
	});
	
	$('.preview').click(function(){
		$(this).attr('href', $(this).parents('.featured').find('.name-and-price .product-name a').attr('href'));
		//return false;
	});
	
	var searchFormClicked = false;
	$('.search_form input').click(function(){
		if( !searchFormClicked ){
			$('.search_form input[type="text"]').val('');
			searchFormClicked = true;		
		}
	});
	
	var loginFormClicked = false;
	$('.login input').click(function(){
		if( !loginFormClicked ){
			$('.login input[type="text"]').val('');
			$('.login input[type="password"]').val('');
			loginFormClicked = true;		
		}
	});
	
	var mouseOverLoginBox = 0;
	$('.js_login').click(function(){
		if ($('.login_box:visible').length) {
			$('.login_box').fadeOut();
			mouseOverLoginBox = 0;		
		}
		else {
			$('.login_box').fadeIn();
			$('.login_box').bind('mouseenter', function(){
				mouseOverLoginBox = 1;
			});
			$('.login_box').bind('mouseleave', function(){
				mouseOverLoginBox = 0;
			});
		}
		return false;
	});
	$(document).mousedown(function(){
		if ($('.login_box:visible').length && !mouseOverLoginBox) {
			$('.login_box').fadeOut();
			mouseOverLoginBox = 0;
		}
	});
	
	//$('#content h1').prepend('<span class="chrome">&nbsp;</span>');
	
	$('table.sort_table th, table.sort_table td').each(function(){
		if( $(this).html() == '' )
			$(this).html('&nbsp;');
	});
	$('table.sort_table').attr('cellspacing', 0);

	$('.cart .links input').hover(function(){
		$(this).css('color', '#000');
	}, function(){
		$(this).css('color', '#6F6F6F');
	});

	imagePreview();
	
	jsAddToCart();
	jsAddToWishList();
	
}); 
