function menu_expand(mid){
	var oid = 'cat_menu'+mid;
	if(exists(oid)){
		if(is_hidden(oid)){show(oid);}
		else {hide(oid);}
	}
}


function show_variation(preid,foto,code){
	ge('product_img_'+preid).src=foto;
	html(ge('product_code_'+preid),code);
}


function ntab(t){
	if(exists('tab_naujienos')){hide('tab_naujienos');}
	if(exists('tab_duk')){hide('tab_duk');}
	if(exists('tab_atsiliepimai')){hide('tab_atsiliepimai');}
	show('tab_'+t);
}



function prefer(param, val){
	setCookie(param,val,10);
	setTimeout('doRefresh()',100);
}

function doRefresh(){
	location.reload(true);
}

function prefer_orderby(){
	prefer('pref_orderby',gv('pref_orderby'));
}

function prefer_perpage(){
	prefer('pref_perpage',gv('pref_perpage'));
}

function start_inc(oid,up,kiek,min,max,js) {
	var nv = 1;
	var val = parseInt(ge(oid).value);
	if(up){nv = val+kiek;}
	else {nv = val-kiek;}
	if(nv<min){nv = min;}
	if(nv>max){nv = max;}
	ge(oid).value = nv;
	if(!undef(js)){if(js!=''){eval(js);}}
}

function stop_inc(){
	return;
}


function catalogue_menu(nr){
	if(nr=='1'){hide('catalogue_menu_tab2'); hide('catalogue_menu_tr2'); show('catalogue_menu_tab1'); show('catalogue_menu_tr1');}
	else{hide('catalogue_menu_tab1'); hide('catalogue_menu_tr1'); show('catalogue_menu_tab2'); show('catalogue_menu_tr2');}
}


/* CART FUNCTIONS */
function add_to_cart_auto(){
	var keys = new Array('action','product','quantity','size','variation','lang');
	var vals = new Array('add_to_cart',gv('product_id'),gv('quantity'),gv('product_size'),gv('selected_color'),lang);
	ajax_siusti(keys,vals,'ajax_interface.php','ajax_to_cart_auto');
}

function ajax_to_cart_auto(rez){
	var r = parse_get(rez);
	hide_cart_errors();
	if(r['ok']=='0'){show('cart_error_'+r['error'],'block');}
	else {confirm_cart_addition();}
}

function hide_cart_errors(){
	hide('cart_error_zero'); hide('cart_error_product'); hide('cart_error_quantity'); hide('cart_error_variation'); hide('cart_error_size');
}

function confirm_cart_addition(){
	ge('cart_animation').src = ge('main_foto').src;
	copyProperty('main_foto','cart_animation','w');
	copyProperty('main_foto','cart_animation','h');
	
	show('cart_animation','block');
	
	
	fotox = getProperty('main_foto','x');
	fotoy = getProperty('main_foto','y');
	
	setProperty('cart_animation','x',fotox);
	setProperty('cart_animation','y',fotoy);
	
	targetx = getProperty('sidecart_contents','x')+50;
	targety = getProperty('sidecart_contents','y')+getProperty('sidecart_contents','h')-25;
	//if(is_hidden('cart_tr_tds')){targety+=50;}
	
	var movex = new Array(fotox,targetx,50);
	var movey = new Array(fotoy,targety,-50);
	var movea = new Array(100,0,0);
	var movew = new Array('',10,0);
	var moveh = new Array('',10,0);

	var act = new requiem_action();
	act.callback = 'hide("cart_animation");';
	act.go('cart_animation',movex,movey,movew,moveh,'');

	setTimeout('end_cart_addition()',500);
}

function end_cart_addition(){
	update_cart();
}

function remove_from_cart(id){
	if(confirm(jslang['cart_really_remove'])){
		if(exists('cart_row_'+id)){hide('cart_row_'+id);}
		var keys = new Array('action','id','lang');
		var vals = new Array('remove_from_cart',id,lang);
		ajax_siusti(keys,vals,'ajax_interface.php','end_remove_from_cart');
	}
}

function end_remove_from_cart(rez){
	if(exists('sidecart_contents')){update_cart();}
	if(exists('cart_discount_container')){change_cart_amount(0);}
}

function update_cart(){
	var keys = new Array('action','lang');
	var vals = new Array('get_cart',lang);
	ajax_siusti(keys,vals,'ajax_interface.php','end_update_cart');
}

function end_update_cart(rez){
	var r = parse_get(rez);
	var chtml = ajax_decode(r['html']);
	if(r['c']=='0'){ // cart empty
		hide('cart_tr_tds');
		hide('cart_bottom');
		html('sidecart_contents',chtml);
	}
	else {
		show('cart_tr_tds','');
		show('cart_bottom');
		html('sidecart_contents',chtml);
	}
}

function change_cart_amount(id){
	if(id!=0){var quantity = gv('quantity'+id);} else {quantity = 0;}
	var keys = new Array('action','cart_id','quantity','lang');
	var vals = new Array('update_cart_quantity',id,quantity,lang);
	ajax_siusti(keys,vals,'ajax_interface.php','end_update_cart_amount');
}

function end_update_cart_amount(rez){
	var r = parse_get(rez);
	if(r['ok']=='1'){
		var oid = r['row'];
		if(parseInt(oid)>0){
			html('single_new_price_'+oid, ajax_decode(r['p2_new']));
			html('sum_new_price_'+oid, ajax_decode(r['s2_new']));

			if(r['p_old']!=''){html('single_old_price_'+oid, '<span class="old_price">'+ajax_decode(r['p2_old'])+'</span>'); show('single_old_price_'+oid);} else {html('single_old_price_'+oid, ''); hide('single_old_price_'+oid);}
			if(r['s_old']!=''){html('sum_old_price_'+oid, '<span class="old_price">'+ajax_decode(r['s2_old'])+'</span>'); show('sum_old_price_'+oid);} else {html('sum_old_price_'+oid, ''); hide('sum_old_price_'+oid);}
		}
		
		html('cart_total_price', ajax_decode(r['t2_new']));
		if(r['discount']!=''){html('cart_old_price', ajax_decode(r['discount2'])); show('cart_discount_container','');} else {html('cart_old_price', ''); hide('cart_discount_container');}
	}
}


function order_show_login(){
	show('account_type1','');
	hide('account_type0');
	hide('order_block_1');
	if(exists('order_block_4')){hide('order_block_4');}
}



/* COUPONS */
function enter_coupon(){
	hide('coupon_error');
	var keys = new Array('action','coupon_nr','lang');
	var vals = new Array('add_coupon',gv('coupon_input'),lang);
	ajax_siusti(keys,vals,'ajax_interface.php','end_enter_coupon');
}

function end_enter_coupon(rez){
	var r=parse_get(rez);
	if(r['ok']==0){
		if(exists('coupon_error_nr')){html('coupon_error_nr',r['coupon_nr']);}
		show('coupon_error');
	}
	refresh_coupon_list();
}


function remove_coupon(rid){
	var keys = new Array('action','coupon_id','lang');
	var vals = new Array('remove_coupon',rid,lang);
	ajax_siusti(keys,vals,'ajax_interface.php','end_remove_coupon');
}

function end_remove_coupon(rez){
	refresh_coupon_list();
}


function refresh_coupon_list(){
	var keys = new Array('action','lang');
	var vals = new Array('refresh_coupons',lang);
	ajax_siusti(keys,vals,'ajax_interface.php','end_refresh_coupon_list');
}

function end_refresh_coupon_list(rez){
	html('coupon_list_container',ajax_decode(rez));
	update_price_table();
}

function use_order_bonuses(){
	var use = 0;
	if(ge('use_bonuses').checked){use = 1;}
	var keys = new Array('action','use','lang');
	var vals = new Array('use_bonuses',use,lang);
	ajax_siusti(keys,vals,'ajax_interface.php','end_use_order_bonuses');
}

function end_use_order_bonuses(rez){
	refresh_coupon_list();
}

function update_price_table(){
	var keys = new Array('action','lang');
	var vals = new Array('update_prices',lang);
	ajax_siusti(keys,vals,'ajax_interface.php','end_update_price_table');
}

function end_update_price_table(rez){
	html('order_price_summary',ajax_decode(rez));
}


function change_delivery_way(delivery_id){
	var keys = new Array('action','delivery','lang');
	var vals = new Array('update_delivery',delivery_id,lang);
	ajax_siusti(keys,vals,'ajax_interface.php','end_change_delivery_way');
}
function end_change_delivery_way(rez){
	update_price_table();
}




/* Search suggestions */
var max_suggestions = 16;
var suggestion_target;
var selected_suggestion = 0;

function start_suggestions(event,target,lang){
	var ckey = key(event);
	if(ckey==38){move_suggest_up();}
	else if(ckey==40){move_suggest_down();}
	else if(ckey==13 && selected_suggestion>0){dosuggest(ge('suggestion'+selected_suggestion).innerHTML);}
	else {
		suggestion_target = target;
		var keyword = gv(target);
		var keys = new Array('action','keyword','lang','max');
		var vals = new Array('suggest', keyword, lang, max_suggestions);
		ajax_siusti(keys,vals,'ajax_interface.php','get_suggestions');
	}
	return true;
}

function suggestions_form(){
	if(selected_suggestion>0){dosuggest(ge('suggestion'+selected_suggestion).innerHTML); return false;}
	else {return true;}
}

function get_suggestions(result){
	if(exists('suggestions') && result!='none' && result!=''){
		html('suggestions',unhtmlspecialchars(result));
		position_suggest_box();
		show('suggestions','block');
		selected_suggestion = 0;
	}
	else {hide_suggestions();}
}

function position_suggest_box(){
	var x = getProperty(suggestion_target,'x');
	var y = getProperty(suggestion_target,'y');
	var s = ge('suggestions');
	setProperty(s,'x',(x-3));
	setProperty(s,'y',(y+19));
}

function move_suggest_up(){
	var prev = selected_suggestion-1;
	if(!exists('suggestion'+prev)){
		for(var i=max_suggestions;i>0;i--){
			if(exists('suggestion'+i)){prev = i; break;}
		}
	}

	if(exists('suggestion'+prev)){
		selected_suggestion = prev;
		for(var i=1;i<=max_suggestions;i++){if(exists('suggestion'+i)){delcss('suggestion'+i,'selected');}}
		setcss('suggestion'+prev,'selected');
	}
}
function move_suggest_down(){
	var next = selected_suggestion+1;
	if(!exists('suggestion'+next)){
		for(var i=1; i<=max_suggestions;i++){
			if(exists('suggestion'+i)){next = i; break;}
		}
	}

	if(exists('suggestion'+next)){
		selected_suggestion = next;
		for(var i=1;i<=max_suggestions;i++){if(exists('suggestion'+i)){delcss('suggestion'+i,'selected');}}
		setcss('suggestion'+next,'selected');
	}
}

function dosuggest(txt){
	sv('quicksearch',strip_tags(txt));
	selected_suggestion = 0;
	hide_suggestions();
	ge('quicksearch_form').submit();
}

function blur_suggestions(){
	setTimeout('hide_suggestions()',50);
}

function hide_suggestions(){
	if(exists('suggestions')){
		hide('suggestions');
	}
}


var current_color=0;
function select_product_color(fotos,nr,code,vid){
	sv('selected_color',vid);
	sv('product_code',code);
	setcss('color_div'+nr, 'selected_img');
	delcss('color_div'+current_color, 'selected_img');
	current_color = nr;
	fotos.show_foto(nr);
}

function product_tab(nr){
	var i =0;
	while(i<10){i++; if(exists('product_tab'+i)){hide('product_tab'+i);}}
	show('product_tab'+nr,'');
}

function check_remaining(){
	code = gv('product_code');
	var keys = new Array('action','code','lang');
	var vals = new Array('check_remaining',code,lang);
	ajax_siusti(keys,vals,'ajax_interface.php','end_check_remaining');
	
	html('dialog_text','<div align="center"><img src="style/wait.gif"></div>');
	hideSelectBoxes();
	hideFlash();
	show('dialog_foreground');
	show('dialog_background');
	position_dialog();
}

function end_check_remaining(rez){
	show_dialog(ajax_decode(rez));
}

function show_dialog(txt){
	if(txt==''){return;}
	
	html('dialog_text',txt);
	hideSelectBoxes();
	hideFlash();
	show('dialog_foreground');
	show('dialog_background');
	position_dialog();
}

function hide_dialog(){
	showSelectBoxes();
	showFlash();
	hide('dialog_background');
	hide('dialog_foreground');
}

function position_dialog(){
	setProperty('dialog_background','h',getProperty('','py'));
	var x = round( (getProperty('','wx') - getProperty('dialog_foreground','w')) / 2);
	var y = round(( (getProperty('','wy') - getProperty('dialog_foreground','h')) / 2) + getProperty('','sy'));
	setProperty('dialog_foreground','x',x);
	setProperty('dialog_foreground','y',y);
}


// FOTOS
function Foto_changer(){
	this.list = new Array();
	this.current = 0;
	this.target = 'main_foto';
	
	this.next_foto = function(){
		this.current++;
		if(this.current>=this.list.length){this.current=0;}
		this.show_foto(this.current);
	}
	
	this.show_foto = function(nr){
		this.current = nr;
		img = ge(this.target);
		img.src=this.list[nr][0];
		img.alt=this.list[nr][1];
		img.title=this.list[nr][1];
	}
	
	this.zoom_foto = function(nr){
		if(undef(nr)){nr = this.current;}
		myLightbox.start(ge('lightboxlink'+nr));
	}
}

document.onmousemove = mouseMove;
var larger_foto_visible = false;

function show_larger_foto(e, foto, size_to_replace, size_to_show){
	if(undef(size_to_replace)){size_to_replace = 's';}
	if(undef(size_to_show)){size_to_show = 'l';}
	
	hideSelectBoxes();
	hideFlash();
	larger_foto_visible = true;
	
	var f = foto.src.replace('/'+size_to_replace+'/','/'+size_to_show+'/');
	f = f.replace('/perziura/','/'+size_to_show+'/');
	ge('larger_foto').src = f;
	
	show('larger_foto_container','block');
	var mp = mousePos(e);
	set_larger_foto_position(mp);
}

function mouseMove(e){
	if(larger_foto_visible){
		var mp = mousePos(e);
		set_larger_foto_position(mp);
	}
}

function set_larger_foto_position(mp){
	var c = ge('larger_foto_container');
	setProperty(c,'x',mp[0]+10);
	setProperty(c,'y',mp[1]+20);
	if(getProperty(c,'y') + getProperty(c,'h') > getProperty('','sy') + getProperty('','wy')){
		setProperty(c,'y',mp[1]-5-getProperty(c,'h'));
	}
}

function hide_larger_foto(){
	showSelectBoxes();
	showFlash();
	larger_foto_visible = false;
	ge('larger_foto').src = 'style/blank.gif';
	hide('larger_foto_container');
}



// TABS
function goto_negotiate(){
	product_tab(4);
	var y = getProperty(ge('product_tab4'),'y');
	scrollpage((y-30),80,15);
}


// FILTERS
function filter_go(){
	window.location = filter_generate();
	return false;
}

function filter_generate(){
	var out = clean_url+'-filter_';
	var fgs = filter_get_checked();
	filts1 = fgs[0];
	filts2 = fgs[1];
	for(var i=0; i<filts1.length; i++){
		if(i>0){out+= ';';}
		out+= filts1[i]+':';
		for(var j=0; j<filts2[i].length; j++){
			if(j>0){out+= ',';}
			out+= filter_encode(filts2[i][j]);
		}
	}
	return out;
}


function filter_get_checked(){
	var out = new Array();
	out[0] = new Array();
	out[1] = new Array();
	var n=-1;
	for(var i=0; i<filters1.length; i++){
		vals = filters2[i];
		var m=-1;
		for(var j=0; j<filters2[i].length; j++){
			var ch_id = 'filter_'+filters1[i]+'_'+filters2[i][j];
			if(exists(ch_id)){
				if(ge(ch_id).checked){
					m++; if(m==0){n++; out[1][n] = new Array();}
					out[0][n] = filters1[i];
					out[1][n][m] = filters2[i][j];
				}
			}
		}
	}
	return out;
}

function filter_encode(val){
	return val.replace('-','~');
}




// NEWS SUBSCRIPTION
function save_subscription(){
	var keys = new Array('action','email','lang');
	var vals = new Array('save_subscription',gv('subscription_email'),lang);
	ajax_siusti(keys,vals,'ajax_interface.php','saved_subscription');
}
function saved_subscription(rez) {
	var r = parse_get(rez);
	if(r['r']=='OK'){hide('news_header'); hide('news_error'); hide('news_input'); show('news_ok','block');}
	else{hide('news_header'); hide('news_ok'); show('news_error','block'); show('news_input','block');}
}


