/**
* Funkce z templates
*/

// mění parametry v url
// @from product_list.tpl
function changeParam(paramname, mode) {
	var location = new String(window.location)
	if (mode != -1) {
		if (location.match(paramname)) {
			var replaceexp = new RegExp ('(' + paramname + '=)[0-9]*', 'g')
			location = location.replace(replaceexp, '$1' + mode)
		} else {
			if (location.match(/\?/)) {
				location += '&'
			} else {
				location += '?'
			}
			location = location + paramname + '=' + mode;
		}

		if (paramname == 'producer' || paramname == 'sort' || paramname == 'expedition' || paramname == 'limit') {
			location = firstPage(location)
		};

		window.location = location;
	}
}

// @from product_list.tpl
function firstPage(location) {
	return location.replace(/pageno=[0-9]+&?/, '')
}

// parametr funkce je id šipky (vzestupne[0] nebo sestupne[1])
// @from product_list.tpl
function sortMode(button) {
	var form = document.product_list_bar
	changeParam('sortmode', parseInt(form.sortmode.value) + parseInt(button))
}

// @from multimenu.tpl
function startMenu() {
	if (document.all && document.getElementById) {
		var multimenu = document.getElementById('multimenu')
		for (i = 0; i < multimenu.childNodes.length; i++) {
			var ul = multimenu.childNodes[i]
			if (ul.nodeName == 'UL') {
				for (j = 0; j < ul.childNodes.length; j++) {
					var node = ul.childNodes[j]
					if (node.nodeName == 'LI') {
						node.onmouseover = function() {this.className += ' over'}
						node.onmouseout = function() {this.className = this.className.replace(' over', '')}
					}
				}
			}
		}
	}
}

if (window.attachEvent) {
	window.attachEvent('onload', startMenu)
} else {
	window.onload = startMenu;
}

// @from categories.tpl
function depShowHide(id) {
	var dep = document.getElementById('depId' + id)
	var img = document.getElementById('depImgId' + id)
	var imgsrc = new String(img.src)

	if(dep.style.display == '') {
		dep.style.display = 'none'
		img.src = imgsrc.replace(/_open\.gif$/, '_close.gif')
	} else {
		dep.style.display = ''
		img.src = imgsrc.replace(/_close\.gif$/, '_open.gif')
	}
}

function depGet(url) {
	document.location = url
}

// @from download_categories.tpl
function depDownloadShowHide(id) {

	var dep = document.getElementById('depIdDown' + id)
	var img = document.getElementById('depImgIdDown' + id)
	var imgsrc = new String(img.style.backgroundImage)

	if(dep.style.display == '') {
		dep.style.display = 'none'
		img.style.backgroundImage = imgsrc.replace(/_open\.gif/, '_close.gif')
	} else {
		dep.style.display = ''
		img.style.backgroundImage = imgsrc.replace(/_close\.gif/, '_open.gif')
	}
}

// @from basket.tpl
//function basket_clear_url() {
//	var location = new String(window.location)
//	var newlocation = location.replace(/[\?&]addproduct=[0-9]*/, '')
//	newlocation = newlocation.replace(/[\?&]count=[0-9]*/, '')
//	newlocation = newlocation.replace(/[\?&]jettison=[0-9]*/, '')
//	newlocation = newlocation.replace(/[0-9]+-.*/, 'kosik/')
//	newlocation = newlocation.replace(/([\?&]page=)[a-zA-Z_]*/, '$basket')

//	if (newlocation != location) 
//	{
//		window.location = newlocation;
//	}
//}

// @from order.tpl
var selected_tran = -1;
var service_value = new Array();

function inArray(needle, haystack) {
    var length = haystack.length;
    for(var i = 0; i < length; i++) {
        if(haystack[i] == needle) return true;
    }
    return false;
}

function radio_value(el) {
	var len = el.length
	for (i = 0; i < len; i++) {
		var radio = el[i]
		if (radio.checked) {
			return radio.value
		}
	}
	return false
}

function recalc_total(curr, before) {
	var trans_value = radio_value(document.getElementsByName('transport'));
	var paym_value = radio_value(document.getElementsByName('payment'));

	var price = 0
	if (trans_value) {
		price += trans_prices[trans_value];
		if (service_value[trans_value]) {
			price += service_value[trans_value]
		}
	}
	if (paym_value) {
		price += paym_prices[paym_value]
	}

//	alert('trans: '+price+'\r\ntotal: '+base_price);

		document.getElementById('price-transport').innerHTML = format_currency2(price,curr, before)
		document.getElementById('price-total').innerHTML = format_currency2(price + base_price,curr, before)
}

function radio_label_state(prefix, id, enabled) {
	if (enabled) {
		document.getElementById(prefix + '_' + id).disabled = ''
		removeClassName(document.getElementById(prefix + '_' + id + '_label'), 'disabled')
	} else {
		document.getElementById(prefix + '_' + id).disabled = 'disabled'
		addClassName(document.getElementById(prefix + '_' + id + '_label'), 'disabled')
		document.getElementById(prefix + '_' + id).checked = false
	}
}

function transport_payment_change(prefix, ids,curr, before) {
	//var form_el = document.order.elements
	//var re = new RegExp('^' + prefix + '_(\\d+)')
	if (prefix == 'payment') {
		for (i in paym_ids) {
			radio_label_state(prefix, paym_ids[i], inArray(paym_ids[i], ids));
		}
	}

	recalc_total(curr, before)
}

function transport_change(id,curr, before) {
	if (loyalty != 1) {
	transport_payment_change('payment', tp_enable[id],curr, before)
	}

	if(selected_tran != -1) {
		elem = document.getElementById('services_'+selected_tran);
		if(elem){
			addClassName(elem, 'hidden');
		}
	}

	elem = document.getElementById('services_'+id);
	if(elem){
		removeClassName(elem, 'hidden');
	}

	selected_tran = id;
}

function payment_change(id,curr, before) {
	transport_payment_change('transport', pt_enable[id],curr, before)
}

function service_change(elem, id_tran, id_serv, curr, before) {
	if (!service_value[id_tran]) service_value[id_tran] = 0;

	if (elem.checked) {
		service_value[id_tran] += service_price[id_serv];
	} else {
		service_value[id_tran] -= service_price[id_serv];
	}

	recalc_total(curr, before);
}

function add_gallery(id) {
	$('#user-info').load(SHOP_URL+'ajax.php',{
		'page':'ajax_add_gallery',
		'gal_item_id':id
	})
	// toggle buttons (cannot use toggle() because of a bug)
	$('.agal-'+id).removeClass('visible').addClass('hidden');
	$('.dgal-'+id).removeClass('hidden').addClass('visible');
}

function del_gallery(id) {
	$('#user-info').load(SHOP_URL+'ajax.php',{
		'page':'ajax_add_gallery',
		'gal_del_item_id':id
	})
	// toggle buttons (cannot use toggle() because of a bug)
	$('.dgal-'+id).removeClass('visible').addClass('hidden');
	$('.agal-'+id).removeClass('hidden').addClass('visible');
}

function show_alert(id){
	$('#alert_window').load(SHOP_URL+'ajax.php',{'page':'ajax_alert','id':id},function() {
		$('#alert_window').show('slow');
	});
}

function close_alert() {
	$('#alert_window').hide('slow',function(){
		$('#alert_window').html('');
	});
}

function send_postcard(id) {
	$('#alert_window').load(SHOP_URL+'ajax.php',{'page':'ajax_postcard','id':id},function() {
		$('#alert_window').show('slow');
	});
}

function count_goods(id,ram,color) {
	$('#addproduct').val(id);
 //$('#addram').val(ram);

	var count = $('#countajax').val();

	$('#vyskakovaciokno').load(SHOP_URL+'ajax.php',{'page':'ajax_detail_okno','id':id,'ram':ram,'color':color, 'count':count});

}

function submit_postcard(id) {
	// ZDE loader
	x = $('#postcard').serialize();
	$('#alert_window').load(SHOP_URL+'ajax.php',{'page':'ajax_postcard','id':id,'data':x});
	
}

function addition_registry(id,oid) {
	var ps = $('#pass').val();
	$('#register').load(SHOP_URL+'ajax.php',{'page':'ajax_order_register','id':id,'oid':oid,'ps':ps});
}


function choice_provedeni(id,ram) {
	$("#filter-provedeni a").click(function () {
	 $("#filter-provedeni a").removeClass("select-button-active2", 1);
	 $(this).addClass("select-button-active2", 1);
	});
	$('#addproduct').val(id);
	$('#price_content').load(SHOP_URL+'ajax.php',{'page':'ajax_detail_price','id':id,'ram':ram});
	$('#info_content').load(SHOP_URL+'ajax.php',{'page':'ajax_detail_info','id':id,'ram':ram});
	$('#vyskakovaciokno').load(SHOP_URL+'ajax.php',{'page':'ajax_detail_okno','id':id,'ram':ram});
	$('#descript_content').load(SHOP_URL+'ajax.php',{'page':'ajax_detail_descript','id':id,'ram':ram});	
	$('#ram_menu_content').load(SHOP_URL+'ajax.php',{'page':'ajax_menu_ram','id':id,'ram':ram});
	$('#pictrue_content').load(SHOP_URL+'ajax.php',{'page':'ajax_detail_pictrue','id':id},function(){
			$("#pictrue_content a.zoom").fancybox({
	  				'imageScale'	:	true,
	          'overlayOpacity' : 0.8
	  			});  
	 });	


}

function choice_ram(id,ram,color,where) {
	$("#filter-frames a").click(function () {
	 $("#filter-frames a").removeClass("select-button-active", 1);
	 $(this).addClass("select-button-active", 1);
	});
	$('#addproduct').val(id);
	//$('#addram').val(color);
//	$('#price_content').load(SHOP_URL+'ajax.php',{'page':'ajax_detail_price','id':id,'ram':ram,'color':color});
	if(where){
	    $('#ram_menu_content').load(SHOP_URL+'ajax.php',{'page':'ajax_menu_ram','id':id,'ram':ram,'color':color});

	}

	$('#ram_content').load(SHOP_URL+'ajax.php',{'page':'ajax_detail_ram','id':id,'ram':ram,'color':0});
	$('#color_content2').load(SHOP_URL+'ajax.php',{'page':'ajax_detail_color','id':id,'ram':ram,'color':color});
}


function delete_ram(id) {
	$('#addproduct').val(id);
	$('#addram').val(0);
 	var count = $('#count').val();
	$('#vyskakovaciokno').load(SHOP_URL+'ajax.php',{'page':'ajax_detail_okno','id':id,'ram':0,'color':0, 'count':count});
	$('#price_content').load(SHOP_URL+'ajax.php',{'page':'ajax_detail_price','id':id,'ram':0, 'color':0});
	$('#pictrue_content').load(SHOP_URL+'ajax.php',{'page':'ajax_detail_pictrue','id':id,'ram':0, 'color':0});
	$('#info_content').load(SHOP_URL+'ajax.php',{'page':'ajax_detail_info','id':id,'ram':0,'color':0});
	$('#ram_content').load(SHOP_URL+'ajax.php',{'page':'ajax_detail_ram','id':id,'ram':0,'color':0});
	$('#color_content2').load(SHOP_URL+'ajax.php',{'page':'ajax_detail_color','id':id,'ram':0,'color':0});
  $('#ram_menu_content').load(SHOP_URL+'ajax.php',{'page':'ajax_menu_ram','id':id,'ram':0,'color':0});

}

function choice_color(id,ram,color) {
 

	$('#addproduct').val(id);
	$('#addram').val(color);	
	$('#pictrue_content').load(SHOP_URL+'ajax.php',{'page':'ajax_detail_pictrue','id':id,'ram':ram, 'color':color},function(){
			$("#pictrue_content a.zoom").fancybox({
	  				'imageScale'	:	true,
	          'overlayOpacity' : 0.8
	  			});  
	 });		
	$('#price_content').load(SHOP_URL+'ajax.php',{'page':'ajax_detail_price','id':id,'ram':ram, 'color':color});
	$('#color_content2').load(SHOP_URL+'ajax.php',{'page':'ajax_detail_color','id':id,'ram':ram,'color':color});
	$('#info_content').load(SHOP_URL+'ajax.php',{'page':'ajax_detail_info','id':id,'ram':ram,'color':color});
	$('#ram_content').load(SHOP_URL+'ajax.php',{'page':'ajax_detail_ram','id':id,'ram':ram,'color':color});
	$('#vyskakovaciokno').load(SHOP_URL+'ajax.php',{'page':'ajax_detail_okno','id':id,'ram':ram,'color':color});
	  

	

}


/*$(function() {
    $(document).ready(function() {	
	$('.products-table .ram_select').live('change', function () {
	    var parent = $(this).parents('.products-table');
	    parent.find('.basket_color').load(SHOP_URL+'ajax.php',{
		'page':'ajax_basket_ram',
		'id':parent.find('input[name=product_id]').val(),
		'ram':$(this).val()
	    },function(){
		$("select").uniform();
	    });	
	});
    });
});*/

function basket_ram(el) {
    var parent = $(el).parents('.products-table');
    parent.find('.basket_color').load(SHOP_URL+'ajax.php',{
	'page':'ajax_basket_ram',
	'id':parent.find('input[name=product_id]').val(),
	'ram':$(el).val()
    },function(){
		var colorSelect = $('.basket_color', parent);
		// reset selectu s farbami ramu
		$('span', $(colorSelect).val('- vyberte barvu').parent()).html('- vyberte barvu');
		// zakazanie a povolenie selectu s farbami ramu podla toho ci je vybrany typ ramu
		if ($(el).attr('selectedIndex') == 0) {
			$(colorSelect).attr('disabled','disabled').parent().addClass('disabled');
		}
		else {
			$(colorSelect).attr('disabled','').parent().removeClass('disabled');
		}
    });	
    //$('#basket_color').load(SHOP_URL+'ajax.php',{'page':'ajax_basket_ram','id':id,'ram':ram});
	
	
}

function set_col(id) {
	if($('#color_'+id).val()==0) {
		$('#color_'+id).val(id);
		$('#col_chck_'+id).addClass('chckd');
	} else {
		$('#color_'+id).val(0);
		$('#col_chck_'+id).removeClass('chckd');	
	}
	$('#id_param_search').submit();
}

function del_sizes() {
	$('#del_sizes').hide();
	$('#use_sizes').val('0');
    $('#id_param_search').submit();
}

function set_sizes() {
	$('#use_sizes').val('1');
}


function add_to_basket(id) {

	 $('#basket-info').load(SHOP_URL+'ajax.php',{'page':'ajax_basket_add','id':id,'var':tmp,'tp':'1','potisk':pot},function(){
			$.ajax({
	     		url: SHOP_URL+'ajax.php',
	      		global: false,
	      		data: ({'page':'ajax_basket_checksum','private':'1'}),
	      		dataType: "html",
	      		async:false,
	      		success: function(msg){
	       	  		if(msg==1){
	       	  			show_alert(10);
	       	  		} else if(msg==2) {
	       	  			show_alert(11);
	       	  		} else {
						show_alert(2);
	       			}
	      		}
	   		});
		});




}

