
function selected_ids ( elem, selected ) {
	var i = 0;
	var value = new Array();

	$(elem).each( function () {
		if ( $(this).val() > 0 ) {
			value[i++] = $(this).val();
		}
		if ( $(selected).attr('id') == $(this).attr('id') ) {
			return false;
		}
	});
	return value;
}


function clear_options ( elem ) {
	var index = $('select[name=templ_field_value_id[]]').index(elem);
	var i = 0;
	$('select[name=templ_field_value_id[]]').each( function () {
		if ( i++ > index) {
			$(this).attr('disabled', 'disabled');
			$(this).empty();
		}

	});
	$('#price').html(window.option_price_copy);
	$('#goods_option_id').val( 0 );

}
function update_options ( data ) {
	var index = 0;
	for ( var key in data ) {
		var sel = $('#option_name-'+key);

		if ( sel.hasClass('ignore') ) continue;
		
		var selected_option = sel.find(':selected').val();
		sel.removeAttr('disabled');
		sel.empty();
		for ( var i in data[key] ) {
			var selected = data[key][i].id == selected_option ? ' selected="selected" ' : '';
			sel.append('<option value="'+data[key][i].id+'" '+selected+'>'+data[key][i].name+'</option>');
		}
		index++;
	}
}
goods_options = function ( elem )  {

	var value = new Array();
	value = selected_ids('select[name=templ_field_value_id[]]', elem);
	clear_options( elem );
	value = $.param( {
		'value_id' : value
	} );

	if ( ! value )
		return;
	var good_id = get_id($(elem).parent().parent().attr('id'));
	var name = get_id($(elem).attr('id'));
	//if we are changinh the first send only the first
	$.post(base_url+'options/filter', 'good_id='+good_id+'&name_id='+name+'&'+value, function ( data ) {
		if ( data.options )
			update_options( data.options );
		if ( data.price )
			$('#price').html( data.price );
		if ( data.option_id ) {
			$('#goods_option_id').val( data.option_id );
			if ( data.qty > 0 ) {
				$('#goods_option_id').parent().find('.options-dn').removeClass('dn');
			}
			else {
				alert(global_msg_options_qty);
				clear_options();
			}
		}
	}, 'json')
}

function cookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

set_coupon_code = function(e) {
	if($('#coupon_code').val() == $('#coupon_code').attr('title'))
		$('#coupon_code').focus();
	else{
		$('#step1_wrap').load(site_url+"/carts/update_coupon", {
			coupon_action:'set_coupon',
			coupon_code:$('#coupon_code').val(),
			cart_id:$('#cart_id').val()
		});
	}
}

get_additional_option = function(url, good_id, o){
	var sel_value = $(o).attr('name').match(/\[(\d+)\]/)[1];
	var data = {
		good_id:good_id,
		option_name:$(o).val(),
		select_id:sel_value
	};
	$.post(url, data, function(r){
		var jo = $(o);
		if (r.html != undefined){
			if (jo.next(':input').length > 0) jo.next(':input').replaceWith(r.html);
			else $(o).after(r.html);
		}
	}, 'json');
};

get_price_option = function(url, good_id, o){
	var data = $('form#add_to_cart1'+good_id).serialize();
	$.post(url, data, function(r){
		$('#price').html(r.price);
	}, 'json');
};

submit_poll = function(){
	var answer_id = $('input[name="answer"]:checked').val();
	if(answer_id > 0){
		$.post(site_url + '/main/submit_poll', {
			answer_id:answer_id
		},
		function(response){
			$('#poll_result').html(response);
		});
	}
}

show_poll_result = function(){
	$.get( site_url + '/main/show_poll_result', function(data){
		$('#poll_result').html(data);
	});
}

function get_id (name, index) {
	if ( index !== 0 ) index = 1;
	var arr = name.split('-');
	if (arr.hasOwnProperty(index))
		return arr[index];
	return false;
}

function select_payment () {
	$('#payment_method_form a.sel_payment_method').removeClass('selected');
	$('#payment_method_form input.sel_payment_method').each ( function () {
		$(this).parent().parent().removeClass('selected');
		$(this).removeAttr('checked');
	});
	var value = $('#payment_method_form [name=paymentMethod]').val();
	var elem = $('#payment_method_form #payment-'+value);
	if ( $(elem).is('input') ) {
		$(elem).parent().parent().addClass('selected');
		$(elem).attr('checked', 'checked');
	}
	else if ($(elem).is('a')){
		$(elem).addClass('selected');
	}
}

$(function(){
	$.ajaxSetup({
		cache:false
	});
	$('.valign').vAlign();
	
	$.validator.setDefaults({
		ignoreTitle:true,
		errorClass: 'input-invalid',
		validClass: 'input-valid',
		errorElement: 'em',
		onkeyup: false
	});

	// validate if value != title
	jQuery.validator.addMethod("nottitle", function(value, element, param) {
		return (value != $(element).attr('title'));
	}, jQuery.validator.messages.required);
	jQuery.validator.addClassRules("nottitle", {
		nottitle: ".nottitle"
	});
	
	if($("a.fancy-image").length > 0)
		$("a.fancy-image").fancybox({
			'titlePosition'  : 'inside'
		});

	$("#contact-info a").fancybox({
		href: base_url+'/user/contact_form',
		'titlePosition'  : 'inside',
		onComplete:	function() {
			init();
		}
	});

	$("#contact-form-submit").live('click', function(){
		var form = $(this).closest('form');
		$(form).validate();
		var res = $(form).valid();
		if (!res) {
			$.fancybox.resize();
			return false;
		}
		var data = $(form).serialize();
		$.post( base_url+'/user/send_message', data, function(result){
			$(form).replaceWith('<div id="contact-form-success">'+result+'</div>');
		});
		return false;
	});
	
	$('#cart_login_form').validate();
	
	if($('#user_register').length > 0){
		if($('#cart_register_form1').is(':visible') && $('#radio_physical').is(':checked')) {
			$('#radio_physical').removeAttr("checked");
			$('#radio_legal').attr("checked", "checked");
		}else if( $('#cart_register_form2').is(':visible') && $('#radio_legal').is(':checked') ) {
			$('#radio_legal').removeAttr("checked");
			$('#radio_physical').attr("checked", "checked");
		}
	}
	//register person
	$('#cart_register_form1').validate({
		rules: {
			name: "required",
			surname: "required",
			address: "required",
			post: "required",
			city: "required",
			email: {
				required:true,
				email:true
			},
			pass: {
				required:($('#cart_register_form1 input[name=user_id]').val() < 0)
			},
			pass2: {
				equalTo:"#person_pass11"
			}
		}
	});

	//register company
	$('#cart_register_form0').validate({
		rules: {
			name:"required",
			company:"required",
			address:"required",
			surname:"required",
			caddress:"required",
			city:"required",
			post:"required",
			ccode:{
				required:true,
				minlength:6,
				digits:true
			},
			cvat:{
				required:true,
				minlength:10
			},
			email:{
				required:true,
				email:true
			},
			pass:{
				required:($('#cart_register_form1 input[name=user_id]').val() < 0)
			},
			pass2:{
				equalTo:"#person_pass10"
			}
		}
	});

	if($("form#user_password_forgot").length > 0)
		$("form#user_password_forgot").validate();

	if($("form#user_password_reset").length > 0){
		$("form#user_password_reset").validate({
			rules: {
				confirm_pass: {
					equalTo: "#reset_new_pass"
				}
			}
		});
	}

	if($("#login_form_b").length > 0)
		$("#login_form_b").validate();
	
	$('#payment_method_form .sel_payment_method').click( function () {
		var method = get_id(this.id);
		$('#payment_method_form [name=paymentMethod]').val(method);
		select_payment();
		return false;
	})
	if( $('.sel_payment_method').length > 0 ) {
		select_payment();
	}

	$("#confirm_order").live('click', function(e){
		e.preventDefault();
		$('#content').load(site_url+"/carts/update_confirm", {
			action:'confirm_step5',
			cart_id:$('#cart_id').val()
		});
	});

	$("#licence_agreement,#licence_agreement1,#licence_agreement0").each( function () {
		$(this).fancybox({
			'titlePosition' : 'inside',
			'transitionIn' : 'fade',
			'transitionOut' : 'fade',
			'centerOnScroll':true
		});
	});

	function init () {
		$('.auto-focus').focus();
		$('input:text, textarea').focus(function(e){
			$(this).addClass('input-focus');
		});
		$('input:text, textarea').blur(function(e){
			$(this).removeClass('input-focus');
		});
		$('input:text[title], textarea[title]').live('focus', function(e){
			if($(this).val() == $(this).attr('title')) $(this).val('');
		});
		$('input:text[title], textarea[title]').live('blur', function(e){
			if($(this).val() == '') $(this).val($(this).attr('title'));
		});
	}

	init();

	// newsletter block
	$('#newsletter_submit').click(function(e){
		e.preventDefault();
		$('#form_newsletter').submit();
	});
	$('#form_newsletter').validate({
		submitHandler: function(form){
			$.post('/newsletter/subscribe', $(form).serialize(),
				function(data){
					if (data.result == false) $('#block_newsletter_error').html(data.message).removeClass('dn');
					else $('#block_newsletter_body').load('/newsletter/index');
				}, 'json');
		}
	});

	//block_recomend
	$('#recommend_submit').click(function(e){
		e.preventDefault();
		$('#block_recommend_error:not(.dn), #block_recommend_sucess:not(.dn)').addClass('dn').html('');
		$('#form_recommend').submit();
	});
	$('#form_recommend').validate({
		submitHandler: function(form){
			$.post('/user/recommend', $(form).serialize(),
				function(data){
					if (data.result == false) $('#block_recommend_error').html(data.message).removeClass('dn');
					else $('#block_recommend_success').html(data.message).removeClass('dn');
				}, 'json');
		}
	});

	// login block
	$('#block_login_submit').click(function(e){
		e.preventDefault();
		$('#block_login_form').submit();
	});
	$('#block_login_form').validate({
		submitHandler: function(form){
			form.submit();
		}
	});

	// search block
	$('#block_search_submit').click(function(e){
		e.preventDefault();
		$('#block_search_form').submit();
	});

	//main search
	if($("form#main_search_form").length > 0){
		$("#suggestion_submit").live('click', function(e){
			e.preventDefault();
			$('#main_search_form').submit();
		});
		$(document).bind('click', function (e) {
			if (!$(e.target).is("#search-suggestion *")) {
				if(!$('#search-suggestion').hasClass('dnn'))
					$('#search-suggestion').slideToggle().html('').addClass('dnn');
			}
		});
		$("#main-search-input").attr( "autocomplete", "off" );
		$("#main-search-input").focus(function(){
			if($(this).val() == $.validator.messages.minlength('3'))
				$(this).val('');
		});
		var suggestion_post = null;
		var suggestion_timer = null;
		get_suggestion = function() {
			suggestion_post = $.post('/search/suggestion', $("#main_search_form").serialize(),
				function(data){
					if(data !== null){
						if (data.result == false){
							if(!$('#search-suggestion').hasClass('dnn'))
								$('#search-suggestion').slideToggle().addClass('dnn');
						} else{
							if($('#search-suggestion').hasClass('dnn')) {
								$('#search-suggestion').html(data.suggestion).slideToggle(function(){
									$(this).removeClass('dnn');
								});
							} else $('#search-suggestion').html(data.suggestion);
							$('.valign').vAlign();
						}
					}
				}, 'json');
		}
		$("#main-search-input").keyup(function(){
			if($(this).val().length >= 3) {
				clearTimeout( suggestion_timer );
				if(suggestion_post)
					suggestion_post.abort();
				suggestion_timer = setTimeout(function(){
					get_suggestion();
				}, 150);
			}else{
				if(!$('#search-suggestion').hasClass('dnn'))
					$('#search-suggestion').slideToggle().addClass('dnn');
			}
		});

		$("form#main_search_form").submit(function(){
			if($("form#main_search_form #main-search-input").val().length < 3){
				$("form#main_search_form #main-search-input").val($.validator.messages.minlength('3'));
				return false;
			}
			if($("form#main_search_form #main-search-input").val() == $.validator.messages.minlength('3'))
				return false;
			if ( $('#main-search-input').attr('title') == $('#main-search-input').val()) {
				$('#main-search-input').focus();
				return false;
			}
			return true;
		});

		$('#main-search-input').keypress(function(e) {
			if(e.which == 13)
				$(this).closest('form').submit();
		});

	}

	//top_menu
	if($('#hor-menu').length > 0) {
		$('#hor-menu ul > li.active-cat-on').prev('.menu-spacer').removeClass('menu-spacer').addClass('menu-spacer-blank');
		$('#hor-menu ul > li.active-cat-on').next('.menu-spacer').removeClass('menu-spacer').addClass('menu-spacer-blank');
		$('#hor-menu ul > li.with-sub, #hor-menu ul > li.active-cat-on').hover(
			function(){
				$(this).css('z-index', '11');
				$(this).children('div.submenu').removeClass('dn').hide();
				if($(this).hasClass('with-sub'))
					$(this).removeClass('with-sub').addClass('active-cat');
				if($(this).hasClass('active-cat-on'))
					$(this).addClass('active-cat');
				$(this).prev('.menu-spacer').removeClass('menu-spacer').addClass('menu-spacer-blank');
				$(this).next('.menu-spacer').removeClass('menu-spacer').addClass('menu-spacer-blank');
				$(this).children('div.submenu').animate({
					height: 'toggle'
				}, 300, 'easeOutBounce');
			},
			function(){
				$(this).css('z-index', '10');
				$(this).children('div.submenu').addClass('dn');
				if($(this).hasClass('active-cat') && !$(this).hasClass('active-cat-on') ) {
					$(this).removeClass('active-cat').addClass('with-sub');
					if(!$(this).prev().prev().hasClass('active-cat-on'))
						$(this).prev('.menu-spacer-blank').removeClass('menu-spacer-blank').addClass('menu-spacer');
					if(!$(this).next().next().hasClass('active-cat-on'))
						$(this).next('.menu-spacer-blank').removeClass('menu-spacer-blank').addClass('menu-spacer');
				} else if($(this).hasClass('active-cat-on')){
					$(this).removeClass('active-cat');
				}
			});
	}

	//block search
	if($('#block_search_form').length > 0) {
		$("#block_search_form").validate({
			rules: {
				q: {
					minlength: 3
				},
				price_from: {
					number: true
				},
				price_to: {
					number: true
				}
			},
			onsubmit: false,
			onfocusout: false,
			onkeyup: false,
			onclick: false
		});
		$('#block_search_form').submit(function(e){
			var valid = true;
			var not_empty = false;
			$(this).find('input:text').each( function () {
				if ( $(this).attr('title') == $(this).val()){
					$(this).val('');
				} else if($(this).val() != ''){
					if(!$("#block_search_form").validate().element(this))
						valid = false;
					not_empty = true;
				}
			});
			if(valid && not_empty){
				return true;
			} else{
				$(this).find('input:text').each( function () {
					if ( $(this).attr('title') && $(this).val() == '')
						$(this).val($(this).attr('title'));
				});
				$("#block_search_form input[name=q]").focus();
				return false;
			}
		});
	}
	
	$(".submit-button").live('click', function () {
		$(this).closest('form').submit();
		return false;
	});

	$('#goodsPerPage, #sortGoodsBy,#goods_per_line').change(function(e){
		e.preventDefault();
		$(this).parents('form').submit();
	});

	$('#block_poll input[name="answer"]').click(function(){
		submit_poll();
	});

	$('.login-on #login_form #login_passwd').focus(function(){
		$(this).addClass('dn');
		$('.login-on #login_form #login_real_passwd').removeClass('dn').focus();
	});

	$('.login-on #login_form #login_real_passwd').blur(function(){
		if($(this).val() == '') {
			$(this).addClass('dn');
			$('.login-on #login_form #login_passwd').removeClass('dn').removeClass('input-focus');
		}
	});

	$('#block_markedgoods .remove').live('click', function(e){
		e.preventDefault();
		$('#block_markedgoods_body').load(site_url + '/goods/marked_delete', {
			good_id: $(this).attr('rel')
		});
	});

	$('.add-to-marked').click(function(e){
		e.preventDefault();
		if($('#block_markedgoods').length > 0){
			$('#block_markedgoods_body').load(
				site_url+'/goods/set_marked', {
					mark_good_id: $(this).attr('rel'),
					update_display:1
				},
				function(data){});
		}
	});

	$('.compare-link').click( function () {
		mark_good($(this).attr('rel'));
		return false;
	});

	var login_is_busy = false;

	$('#login_fly_on').live('mouseover', function(){
		open_login(true);
	});
	$('#login_fly_off, #login_flyout').live('mouseout', function(){
		start_login_timer();
	});
	$('#login_fly_off, #login_flyout, #login_flyout *').live('mouseover', function(){
		stop_login_timer();
	});
	$('#login_flyout input').live('focus', function(){
		login_is_busy = true;
	});
	$('#login_flyout input').live('blur', function(){
		login_is_busy = false;
	});

	function start_login_timer(){
		stop_login_timer();
		$('#top-line').oneTime(2000, 'login_close_timer', close_login);
	}

	function stop_login_timer(){
		$('#top-line').stopTime('login_close_timer');
	}

	function open_login(dont_start_timer){
		dont_start_timer = dont_start_timer == true ? true : false;
		stop_login_timer();
		if($('#top-line').hasClass('login-is-open') || login_is_busy){
			return false;
		} else {
			login_is_busy = true;
			stop_login_timer();
			$('#login_on').addClass('dn');
			$('#login_off').removeClass('dn');
			var h = $('#login_flyout').height();
			$('#login_flyout').height('0px');
			$('#login_flyout').addClass('vv');
			$('.show-hide').addClass('vv');
			$('#login_flyout').animate({
				'height' : h+'px'
			},
			500,
			'easeOutBounce',
			function(){
				$('#top-line').addClass('login-is-open');
				if(dont_start_timer != true)
					start_login_timer();
				login_is_busy = false;
			});
		}
	};

	function close_login(){
		stop_login_timer();
		if($('#top-line').hasClass('login-is-open') && !login_is_busy){
			login_is_busy = true;
			var h = $('#login_flyout').height();
			$('#login_flyout').animate({
				'height' : '0px'
			},
			250,
			'easeInOutElastic',
			function(){
				$('#login_flyout').height(h+'px');
				$('#login_on').removeClass('dn');
				$('#login_off').addClass('dn');
				$('.show-hide').removeClass('vv');
				$('#top-line').removeClass('login-is-open');
				login_is_busy = false;
			});
		} else return false;
	};

	var currency_is_busy = false;

	$('#currency_fly_on').live('mouseover', function(){
		open_currency(true);
	});
	$('#currency_fly_off, #currency_flyout').live('mouseout', function(){
		start_currency_timer();
	});
	$('#currency_fly_off, #currency_flyout, #currency_flyout *').live('mouseover', function(){
		stop_currency_timer();
	});

	function start_currency_timer(){
		stop_currency_timer();
		$('#top-line').oneTime(2000, 'currency_close_timer', close_currency);
	}

	function stop_currency_timer(){
		$('#top-line').stopTime('currency_close_timer');
	}

	function open_currency(dont_start_timer){
		dont_start_timer = dont_start_timer == true ? true : false;
		stop_currency_timer();
		if($('#top-line').hasClass('currency-is-open') || login_is_busy){
			return false;
		} else {
			currency_is_busy = true;
			stop_currency_timer();
			$('#currency_on').addClass('dn');
			$('#currency_off').removeClass('dn');
			var h = $('#currency_flyout').height();
			$('#currency_flyout').height('0px');
			$('#currency_flyout').addClass('vv');
			$('.show-hide').addClass('vv');
			$('#currency_flyout').animate({
				'height' : h+'px'
			},
			500,
			'easeOutBounce',
			function(){
				$('#top-line').addClass('currency-is-open');
				if(dont_start_timer != true)
					start_currency_timer();
				currency_is_busy = false;
			});
		}
	};

	function close_currency(){
		stop_currency_timer();
		if($('#top-line').hasClass('currency-is-open') && !login_is_busy){
			currency_is_busy = true;
			var h = $('#currency_flyout').height();
			$('#currency_flyout').animate({
				'height' : '0px'
			},
			250,
			'easeInOutElastic',
			function(){
				$('#currency_flyout').height(h+'px');
				$('#currency_on').removeClass('dn');
				$('#currency_off').addClass('dn');
				$('.show-hide').removeClass('vv');
				$('#top-line').removeClass('currency-is-open');
				currency_is_busy = false;
			});
		} else return false;
	}

	var account_is_busy = false;

	$('#account_fly_on').live('mouseover', function(){
		open_account(true);
	});
	$('#account_fly_off, #account_flyout').live('mouseout', function(){
		start_account_timer();
	});
	$('#account_fly_off, #account_flyout, #account_flyout *').live('mouseover', function(){
		stop_account_timer();
	});

	function start_account_timer(){
		stop_account_timer();
		$('#top-line').oneTime(2000, 'account_close_timer', close_account);
	}

	function stop_account_timer(){
		$('#top-line').stopTime('account_close_timer');
	}

	function open_account(dont_start_timer){
		dont_start_timer = dont_start_timer == true ? true : false;
		stop_account_timer();
		if($('#top-line').hasClass('account-is-open') || login_is_busy){
			return false;
		} else {
			account_is_busy = true;
			stop_account_timer();
			$('#account_on').addClass('dn');
			$('#account_off').removeClass('dn');
			var h = $('#account_flyout').height();
			$('#account_flyout').height('0px');
			$('#account_flyout').addClass('vv');
			$('.show-hide').addClass('vv');
			$('#account_flyout').animate({
				'height' : h+'px'
			},
			500,
			'easeOutBounce',
			function(){
				$('#top-line').addClass('account-is-open');
				if(dont_start_timer != true)
					start_account_timer();
				account_is_busy = false;
			});
		}
	};

	function close_account(){
		stop_account_timer();
		if($('#top-line').hasClass('account-is-open') && !login_is_busy){
			account_is_busy = true;
			var h = $('#account_flyout').height();
			$('#account_flyout').animate({
				'height' : '0px'
			},
			250,
			'easeInOutElastic',
			function(){
				$('#account_flyout').height(h+'px');
				$('#account_on').removeClass('dn');
				$('#account_off').addClass('dn');
				$('.show-hide').removeClass('vv');
				$('#top-line').removeClass('account-is-open');
				account_is_busy = false;
			});
		} else return false;
	}

	var cart_is_busy = false;
	
	$('#close_cart_link').live('click', function(e){
		e.preventDefault();
		close_cart();
	});
	$('#cart-list').live('mouseover', function(){
		stop_cart_timer();
	});
	$('#cart-list').live('mouseout', function(){
		start_cart_timer();
	});
	$('#cart-link').live('mouseout', function(){
		start_cart_timer();
	});
	$('#cart-link').live('mouseover', function(){
		open_cart(true);
	});
	$('#cart-link').live('click', function(e){
		e.preventDefault();
		if($('#cart').hasClass('cart-is-open')) close_cart();
		else open_cart(true);
	});
	$('#cart-title').live('click', function(e){
		e.preventDefault();
	});

	function start_cart_timer(){
		stop_cart_timer();
		$('#cart').oneTime(2000, 'cart_close_timer', close_cart);
	}

	function stop_cart_timer(){
		$('#cart').stopTime('cart_close_timer');
	}

	function open_cart(dont_start_timer){
		dont_stat_timer = dont_start_timer == true ? true : false;
		stop_cart_timer();
		if($('#cart').hasClass('cart-is-open') || cart_is_busy){
			$('#cart-list .valign').vAlign();
			return false;
		} else {
			if($.browser.msie)
				$('#sortGoodsBy').hide();
			cart_is_busy = true;
			stop_cart_timer();
			$('#cart-list').removeClass('dn');
			$('#cart-list .valign').vAlign();
			var h = $('#c-list-inner').height();
			$('#c-list-inner').height('0px');
			
			$('#cart-top').addClass('vv');
			$('.show-hide').addClass('vv');
			$('#c-list-inner').animate({
				'height' : h+'px'
			},
			500,
			'easeOutBounce',
			function(){
				$('#cart').addClass('cart-is-open');
				if(dont_start_timer != true)
					start_cart_timer();
				cart_is_busy = false;
			});
		}
	}

	function close_cart(){
		stop_cart_timer();
		if($('#cart').hasClass('cart-is-open') && !cart_is_busy){
			cart_is_busy = true;
			var h = $('#c-list-inner').height();
			$('#c-list-inner').animate({
				'height' : '0px'
			},
			250,
			'easeInOutElastic',
			function(){
				$('#c-list-inner').height(h+'px');
				$('#cart-top').removeClass('vv');
				$('.show-hide').removeClass('vv');
				$('#cart').removeClass('cart-is-open');
				$('#cart-list').addClass('dn');
				if($.browser.msie)
					$('#sortGoodsBy').show();
				cart_is_busy = false;
			});
		} else return false;
	}
	
	$(".addCart").click(function(e){
		e.preventDefault();
		pid = $(this).attr('rel');
		imgid = (current_good_image) ? current_good_image+'_'+pid : pid ;
		//if options exists
		if ( $('#goods_option_id').length > 0 ) {
			if ( ! ( $('#goods_option_id').val( ) > 0 ) ) {
				alert(global_msg_choose_options);
				return false;
			}
		}

		if(redirect_after_tocart == 0){
			if ($('#item_image_'+imgid).offset() != null) {
				var productX = $('#item_image_'+imgid).offset().left;
				var productY = $('#item_image_'+imgid).offset().top;
				if( $("#cart_item_image_" + pid).length > 0){
					var basketX = $('#cart_item_image_'+pid).offset().left;
					var basketY = $('#cart_item_image_'+pid).offset().top;
				} else {
					var basketX = $('#cart-title').offset().left;
					var basketY = $('#cart-title').offset().top;
				}
				var gotoX = basketX - productX;
				var gotoY = basketY - productY;
				var newImageWidth 	= $('#cart'+pid).width() / 4;
				var newImageHeight	= $('#cart'+pid).height() / 4;
				$('#item_image_'+imgid)
				.clone()
				.appendTo('#item_image_wrap_'+pid)
				.css({
					'position' : 'absolute',
					'z-index' : '999999'
				})
				.removeAttr('id')
				.animate({
					opacity: 0.1,
					marginLeft: gotoX,
					marginTop: gotoY,
					width: newImageWidth,
					height: newImageHeight
				}, 500, function() {
					$(this).remove();
					var post_data = {
						cart_is_open:$('#cart').hasClass('cart-is-open') ? 1 : 0,
						cart_id:$('#brief_cart_id').val(),
						good_id:$('#add_to_cart_good_id_'+pid).val(),
						quantity:$('#add_to_cart_qty_'+pid).val(),
						option_id: $('#goods_option_id').val()
					};
					$('div#cart').load(site_url+'/carts/update_qty', post_data, function(data){
						open_cart();
					});
				});
			} else {
				var post_data = {
					cart_is_open:$('#cart').hasClass('cart-is-open') ? 1 : 0,
					cart_id:$('#brief_cart_id').val(),
					good_id:$('#add_to_cart_good_id_'+pid).val(),
					quantity:$('#add_to_cart_qty_'+pid).val()
				};
				$('div#cart').load(site_url+'/carts/update_qty', post_data, function(data){
					open_cart();
				});
			}
		} else $('form#add_to_cart_'+pid).submit();
	});

	if($('#contact_form2')){
		$('#contact_form2').validate();
		$('#contact_m').click(function(e){
			e.preventDefault();
			$('#contact_form2').submit();
		});
	}

	$('#mark_good').click(function(e){
		e.preventDefault();
		$(this).hide();
		mark_good($(this).attr('rel'));
	});

	if($("#single_carousel").length > 0 && ($('#single_carousel_thumbs').length > 0)){
		var current_good_image = 1;
		$("#single_carousel").jCarouselLite({
			btnPrev: '#left',
			btnNext: '#right',
			circular: false,
			visible: 1,
			easing: 'easeInOutBack',
			speed: 500,
			btnGo: single_button_go
		});
		$('#single_carousel_thumbs img').click(function(){
			id = $(this).attr('id');
			num = id.substr((id.length - 1), id.length); //ie fix
			current_good_image = parseInt(num) + 1;
		});
	}

	if($("#main_carousel").length > 0){
		$("#main_carousel").jCarouselLite({
			btnPrev: '#main_carousel_left',
			btnNext: '#main_carousel_right',
			visible: 1,
			scroll: 1,
			easing: 'easeInOutBack',
			speed: 500,
			auto: 5000
		});
	}

	if($("#block_carousel_top").length > 0){
		$('#block_carousel_top').jCarouselLite({
			vertical: true,
			btnPrev: '#block_carousel_top_up',
			btnNext: '#block_carousel_top_down',
			start: 0,
			visible: carousel_top_count,
			scroll: carousel_top_count,
			easing: 'easeInOutBack',
			speed: 500,
			auto: (carousel_top_count*2000)
		});
	}

	if($("#block_carousel_promo").length > 0){
		$('#block_carousel_promo').jCarouselLite({
			vertical: true,
			btnPrev: '#block_carousel_promo_up',
			btnNext: '#block_carousel_promo_down',
			start: 0,
			visible: carousel_promo_count,
			scroll: carousel_promo_count,
			easing: 'easeInOutBack',
			speed: 500,
			auto: (carousel_promo_count*2000)
		});
	}

	//cart step 1
	update_cart_qtys = function() {
		$.post("/carts/update_qtys", $('#step1_form').serialize(),
			function(data){
				$('#content').html(data);
				$('div#cart').load(site_url + '/carts/cart_brief', {
					cart_id:$('#cart_id').val()
				});
			});
	}

	$('#step1_form .update_cart_qtys').live('click', function(e){
		e.preventDefault();
		update_cart_qtys();
	});

	$('#step1_form .items-number').live('keyup', function(e) {
		e.preventDefault();
		if(e.keyCode == 13)
			update_cart_qtys();
	});

	$('#step1_next').live('click', function(e){
		e.preventDefault();
		$('#step1_form2').submit();
	});

	$("#recall_coupon").live('click', function(e){
		e.preventDefault();
		$('#step1_wrap').load(site_url+"/carts/update_coupon", {
			coupon_action:'recall_coupon',
			cart_id:$('#cart_id').val()
		});
	});

	

	$('#coupon_code').live('keyup', function(e) {
		e.preventDefault();
		if(e.keyCode == 13)
			set_coupon_code();
	});

	$("#set_coupon").live('click', function(e){
		e.preventDefault();
		set_coupon_code(e);
	});

	change_delivery = function(e){ //ie not support jq change event :|
		$('#content').load(site_url+"/carts/update_delivery", {
			delivery_method_id:$(e).val(),
			cart_id:$('#cart_id').val()
		});
	}

	$('.delete_good').live('click', function(e){
		e.preventDefault();
		delete_good($(this).attr('rel'), $('#cart_id').val(), '');
	});

	$('.delete_good_brief').live('click', function(e){
		e.preventDefault();
		delete_good($(this).attr('rel'), $('#brief_cart_id').val(), 'brief');
	});

	//cart step 3
	if($('#step3_form').length) {
		//load if f5 pressed
		if($('#needpvm_checkbox').is(':checked'))
			$('#need_pvm').removeClass('dn');
		else
			$('#need_pvm').addClass('dn');
		
		//needPvm show/hide
		$('#needpvm_checkbox').change(function(){
			if($(this).is(':checked')) $('#need_pvm').removeClass('dn');
			else $('#need_pvm').addClass('dn');
		});

		//cart step 3 validation
		$('#step3_form').validate({
			rules: {
				company: {
					required: "div#need_pvm:visible"
				},
				ccode: {
					required: "div#need_pvm:visible"
				},
				cvat: {
					required: "div#need_pvm:visible"
				},
				caddress: {
					required: "div#need_pvm:visible"
				}
			},
			submitHandler: function(form){
				form.submit();
			}
		});

	}
	//tabs ( html syntax as in jquery ui )
	$('.tabs').click( function () {
		$(this).parent().find('.tabs').removeClass('active');
		$(this).parent().find('.tabs input:radio').removeAttr('checked');
		$(this).addClass('active');
		$(this).find('input:radio').attr('checked', 'checked');
		if($(this).find('input:radio').attr('id') == 'radio_legal'){
			$('#fragment-1').removeClass('dn');
			$('#fragment-2').addClass('dn');
		}else{
			$('#fragment-1').addClass('dn');
			$('#fragment-2').removeClass('dn');
		}
	})

	delete_good = function(cart_good_id, cart_id, brief) {
		var wrap = brief ? 'div#cart' : '#content';
		$(wrap).load(site_url+"/carts/delete_good/" + brief, {
			//	    good_id:good_id,
			cart_id:cart_id,
			cart_good_id:cart_good_id
		},
		function(){
			if(!brief)
				$('div#cart').load(site_url + '/carts/cart_brief', {
					cart_id:cart_id
				});
		});
	};

	$('#change-color ul li a').click( function () {
		cookie('colour_style', $(this).attr('rel'), 360);
	})


	//    window.option_copy = $('select[name=templ_field_value_id[]]').eq(0).clone();
	window.option_price_copy = $('#price').html();
	//options
	$('select[name=templ_field_value_id[]]').change ( function () {
		goods_options( this )
	});

    
	//reset option
	/*$('#goods_options_clear').click( function () {
	var i = 0;
	$('select[name=templ_field_value_id[]]').each( function () {
	    if ( i++ != 0) {
		$(this).attr('disabled', 'disabled');
		$(this).empty();
	    }

	});

	if ( window.option_copy  ) {
//	    console.log(window.option_copy.attr('id'), window.option_copy, $('#'+window.option_copy.attr('id')));
	    $('#'+window.option_copy.attr('id')).replaceWith(window.option_copy);
	    window.option_copy = $('#'+window.option_copy.attr('id')).clone();
	    $('#price').html(window.option_price_copy);
	    $('select[name=templ_field_value_id[]]').change ( function () { goods_options( this ) });
	}

	$('#goods_option_id').parent().find('.options-dn').addClass('dn');


    })*/


	/**
	 * @todo adopt javascript option upateing for new design
	 * 
	 * moved from item2 view. need to be updated for new design
	 */
	$('table.template select[name^="option1_<?php echo $good->id?>"]').change(function() {
		if ($(this).val() != 0)
			get_additional_option('/goods/get_additional', good_id , this);
		get_price_inlist('/goods/get_price_option', good_id, this);
	});

	$('table.template select[name^="option_<?php echo $good->id?>"]').live("change", function() {
		get_price_inlist('/goods/get_price_option', good_id, this);
	});
/* /todo */
});

