function enable_smooth_scroll()
{
	function filterPath(string) {
		return string
				.replace(/^\//,'')
				.replace(/(index|default).[a-zA-Z]{3,4}$/,'')
				.replace(/\/$/,'');
	}

	var locationPath = filterPath(location.pathname);

	var scrollElement = 'html, body';
	$('html, body').each(function () {
		var initScrollTop = $(this).attr('scrollTop');
		$(this).attr('scrollTop', initScrollTop + 1);
		if ($(this).attr('scrollTop') == initScrollTop + 1) {
			scrollElement = this.nodeName.toLowerCase();
			$(this).attr('scrollTop', initScrollTop);
			return false;
		}
	});

	$('a[href*=#]').each(function() {
		var thisPath = filterPath(this.pathname) || locationPath;
		if  (   locationPath == thisPath
				&& (location.hostname == this.hostname || !this.hostname)
				&& this.hash.replace(/#/, '')
			)
			{
				if ($(this.hash).length)
				{
					$(this).click(function(event)
					{
						var targetOffset = $(this.hash).offset().top;
						var target = this.hash;
						event.preventDefault();
						$(scrollElement).animate(
							{scrollTop: targetOffset},
							500,
							function() {
								location.hash = target;
						});
					});
				}
			}
	});
}


var Products = jQuery.extend(Products || {},
{
	add_or_remove_to_compare: function (product_id, elem)
	{
		if(elem.tagName == 'A') {//equal to the remove link (in the compared items right box)
			elem = $('#item'+product_id);
			$(elem).removeAttr('checked');
			elem.checked = false;
		}

		//on attempt to add more than 3 items in compares list
		if($('#list_of_items_to_compare li').size() == 3 && elem.checked){
			alert(_LABELS.up_to_3_products);
			elem.checked = false;
			return true;
		}

		var product_name = $(elem).parents('._product').find('h3 a').text();

		var data = {
			method : 'add_or_remove_to_compare',
			product_id: product_id,
			product_name: product_name,
			is_new: elem.checked
		}

		var _this = this;
		oncomplete = function() {
			elem.checked
				? _this.add_product_in_compare_list(product_id, product_name)
				: _this.remove_product_from_compare_list(product_id);
		};
		this._make_ajax_request(data, oncomplete)
	},

	add_product_in_compare_list: function(product_id, product_name)
	{
		var item_container = $('#item'+product_id).parents('._product').get(0);

		$(item_container).effect("highlight", {}, 600);
		if($('#items_to_compare_block').is(':hidden')) {
			$('#items_to_compare_block').fadeIn('slow');
		}

		$(item_container).effect
		(
			"transfer",
			{ to: $("#list_of_items_to_compare").get(0) },
			800,
			function()
			{
				$('<li id="itc_'+product_id+'"><span>'+product_name+' <a href="javascript:;" id="remove_itc_'+product_id+'" class="delete_ico">'+_LABELS.remove_label+'</a></span></li>')
					.appendTo('#list_of_items_to_compare');

				setTimeout(function(){
					Products.recreate_compare_link();
					$('#items_to_compare_block li #remove_itc_'+product_id).click(function(){
						Products.add_or_remove_to_compare(product_id, this);
					});
				}, 0);
			}
		)
	},

	remove_product_from_compare_list: function(product_id)
	{
		$('#itc_'+product_id).remove();

		if(!$('#list_of_items_to_compare li').size()) {
			$('#items_to_compare_block').fadeOut();
		}

		Products.recreate_compare_link();
	},

	recreate_compare_link: function()
	{
		var ids = new Array();
		$('#list_of_items_to_compare li').each(function(ind, elem){
			ids.push($(elem).attr('id').toString().replace(/[^\d]+/,''));
		});
		link_href = $('#make_the_compare').attr('href');
		link = link_href.substr(0, link_href.lastIndexOf('/')+1);

		$('#make_the_compare').attr('href', link+'?products='+ids.join(','))
	},

	save_back_link_url: function()
	{
		var data = 'method=save_back_link_url';
			data += '&url=' + encodeURIComponent(window.location.toString());

		this._make_ajax_request(data)
	},

	add_to_cart: function(product_id)
	{
		var data = 'method=add_to_cart';
			data += '&product_id=' + product_id;

		this._make_ajax_request(data, function(){
			if(confirm(_LABELS.go_to_cart)) {
				window.location = _BASE_DOMAIN_URL + '/cart/'
			}
		})
	},

	add_to_fav: function(product_id)
	{
		var data = 'method=add_to_favourites';
			data += '&product_id=' + product_id;

		this._make_ajax_request(data)
	},

	_make_ajax_request: function(data, callback)
	{
		callback = callback || function(){};

		$.ajax ({
			url: '/xml_http_request',
			data: data,
			success: callback
		});
	}
});

function _add_to_cart()
{
	var _link = $(this);
	var container = $(this).parents('._product').get(0);

	if(_link.hasClass('in_cart')) {
		if(!confirm(_LABELS.product_exist_in_cart)) {
			return;
		}
	}

	$(container).addClass('in_cart').effect("highlight", {}, 600);
	$(container).effect
	(
		"transfer",
		{ to: $("div.cart").get(0) },
		800,
		function()
		{
			Products.add_to_cart(_link.attr('id').replace(/[^\d]+/,''));
			products_count_container = $($($("div.cart").get(0)).find('span').get(0));
			products_count_container.html(parseInt(products_count_container.html())+1);

			if(!_link.hasClass('in_cart')) {
				_link.addClass('in_cart')
			}
		}
	);
};

function _add_to_fav()
{
	var _link = $(this);
	var container = $(this).parents('._product').get(0);
	var fav_count_container = $("#logout_form .fav_text").get(0);

	if(!IS_LOGGED) {
		if(!alert(_LABELS.fav_you_must_be_logged)) {
			return;
		}
	}

	if(_link.hasClass('in_fav')) {
		if(!alert(_LABELS.product_exist_in_favourites)) {
			return;
		}
	}

	$(container).effect("highlight", {color: '#FFE8B9'}, 600);
	$(container).effect
	(
		"transfer",
		{ to: fav_count_container },
		800,
		function()
		{
			Products.add_to_fav(_link.attr('id').replace(/[^\d]+/,''));

			if(!_link.hasClass('in_fav')) {
				fav_products_count_container = $(fav_count_container);
				fav_products_count_container.html(parseInt(fav_products_count_container.html())+1);
				_link.addClass('in_fav')
			}
		}
	);
}

function _make_search()
{
	var loc_query_string = $('#search_category').val() + '/';

	var keyword = $('#search_keyword').val();
	if(keyword && (keyword != _search_for_default_value || keyword != _LABELS.default_search_for_label)){
		loc_query_string += '?q=' + encodeURIComponent(keyword);
	}

	setTimeout(function(){window.location=_BASE_DOMAIN_URL + '/search/' + loc_query_string;}, 0);
}

var search_for_default_value = '';
$(function()
{
	//ie6
	$('.wrap').pngFix();

	//menu
	$('.submenu_in').html( $(".active .submenu").html() );
	$('.submenu_in li:last').css("background", "none");


	/*$(".main_menu li a").hover(function () {
		$('.submenu_in').html( $(this).parent().children('.submenu').html() );
		$('.submenu_in li:last').css("background", "none");
		$(this).css("color", "#ca031a");
		$(this).parent().siblings().children().css("color", "#fff");
	});*/

	 // expand
	 $('.control').click(function() {
		if ($(this).parent().children('.expandable').is(':visible')) {
	  		$(this).parent().children('.expandable').slideToggle('fast');
			$(this).addClass("control_expanded");
		}
		else {
	  		$(this).parent().children('.expandable').slideToggle('fast');
			$(this).removeClass("control_expanded");
		}
	});
	$('.control:odd').css('background-color', '#ACACAC');

	// slide on anchor

	//opacity
	$('.product_item a').hover(
		function() {$(this).children('img').animate({ opacity: "0.6" }, "fast");},
		function() {$(this).children('img').animate({ opacity: "1" }, "slow");}
	);

	// last tr border on cart
	$('.cart_in tr:last td').css("border", "none");

	// compare
	$('.compare tr:odd td').addClass('odd');
	$('.compare tr:odd th').addClass('odd');

	$('.compare tr').hover(
		function() {$(this).addClass('over_tr');},
		function() {$(this).removeClass('over_tr');}
	);

	//attach add to cart functionality
	$('.go_add_to_cart').click(_add_to_cart);

	$('.in_cart').each(function(){
		$($(this).parents('._product').get(0)).addClass('in_cart');
	});

	//attach add to favourites functionality
	$('.go_add_to_fav').click(_add_to_fav);

	//attach auto hide to the for the search, user login and user pass fields
	var start_values = {'search_keyword': null, 'login_user': null, 'login_pass': null};
	for(id in start_values)
	{
		if($('#'+id).size())
		{
			$('#'+id).focus(function(){
				if(start_values[this.id] == null) start_values[this.id] = this.value;
				if(this.value == start_values[this.id]) this.value = '';
			}).blur(function(){
				if(!this.value.toString()) this.value = start_values[this.id];
			});
		}
	}

	_search_for_default_value = $('#search_keyword').val().toString();
	$('.print_ico').click(function(){print();})

	$('#search').click(_make_search);

	$('#search_form').submit(function(){_make_search();return false;});

	enable_smooth_scroll();

});