function load_url(url){
    if (url) 
        document.location.href = url;
}

function drag_and_drop_categories(){
	$('div.cat_parent').droppable(
			{
				accept			: 'div.cat_parent span.title',
				hoverclass		: 'dropOver',
				activeclass		: 'fakeClass',
				tollerance		: 'pointer',
				ondrop			: function(dropped)
				{
					root = $(this).attr('id').substr(1);
					child = $(dropped).attr('id').substr(1);
					alert(root);
				}
			}
		);
		$('div.cat_parent span.title').draggable(
			{
				revert		: true,
				autoSize	: true,
				ghosting	: true,
				cursor		: 'move',
				onDrag		: function(){
				}
			}
		);
}


function float(el){
	var float=/^[0-9\.]*$/i;
	var not_float=/[^0-9\.]+/g;
	var value = $(el).val();
	if(value.match(float)){$(el).val(value);
	}else{$(el).val(value.replace(not_float,""));}
}

function integer(el){
	var format=/^[0-9]*$/i;
	var not_format=/[^0-9]+/g;
	var value = $(el).val();
	if(value.match(format)){$(el).val(value);
	}else{$(el).val(value.replace(not_format,""));}
}

function clear_input(arg1,arg2){
	if(arg1){$('#'+arg1).val('');}
	if(arg2){$('#'+arg2).val('');}
}

function Move(){
	this.categories = function(URL_MOVE){
			$("div.cat_2").sortable({ opacity: 0.6, cursor: 'move',handle: 'span.move',  update: function() {
				var data = $(this).sortable("serialize"); 
				notice_load();
				$.post(URL_MOVE, data, function(theResponse){remove_notice_load();}); 															 
			}								  
			});
			$("div#sortable").sortable({ opacity: 0.6, cursor: 'move',handle: '.cat_parent span.move',  update: function() {
				var data = $(this).sortable("serialize"); 
				notice_load();
				$.post(URL_MOVE, data, function(theResponse){remove_notice_load();}); 															 
			}								  
			});
	};
	this.products = function(URL_MOVE){
		$("table tbody").sortable({ opacity: 0.6, cursor: 'move',handle: 'td.move',  update: function() {
				var data = $(this).sortable("serialize"); 
				notice_load();
				$.post(URL_MOVE, data, function(theResponse){remove_notice_load();}); 															 
			}								  
			});
	}

}

var Categories = {
	SetLang: function(URL_LANG,el,value,lang){
		if(el){
		var data = "id="+el+"&value="+value+'&lang='+lang;
			notice_load();
			$.post(URL_LANG, data, function(theResponse){remove_notice_load()}); 		
		}
	}
}



var Product= {
	SetNew: function(URL_NEW,el,value){
		if(el){
		var data = "id="+el+"&value="+value;
			notice_load();
			$.post(URL_NEW, data, function(theResponse){remove_notice_load();}); 		
		}
	},
	SetPopular: function(URL_POPULAR,el,value){
		if(el){
		var data = "id="+el+"&value="+value;
			notice_load();
			$.post(URL_POPULAR, data, function(theResponse){remove_notice_load();}); 		
		}
	},
	SetRecommended: function(URL_RECOMMENDED,el,value){
		if(el){
		var data = "id="+el+"&value="+value;
			notice_load();
			$.post(URL_RECOMMENDED, data, function(theResponse){remove_notice_load();}); 		
		}
	},
	show: function(id){
		$('.categories_child[id!="child_'+id+'"]').slideUp('fast');
		$('#child_'+id).slideToggle('fast');
	},
	SetLang: function(URL_LANG,el,value,lang){
		if(el){
		var data = "id="+el+"&value="+value+'&lang='+lang;
			notice_load();
			$.post(URL_LANG, data, function(theResponse){remove_notice_load();}); 		
		}
	},
	
	change_status: function(element,URL_CHANGE_STATUS){
		if(element){
		var id = $(element).attr('id').replace('status_','');
		var val = $(element).val();
		var data = "id="+id+"&val="+val;
			notice_load();
			$.post(URL_CHANGE_STATUS, data, function(theResponse){
				if(val == 2){
					$('tr[id^="recordsArray_'+id+':"]').attr('class','tr_red');
				}else{
					$('tr[id^="recordsArray_'+id+':"]').removeAttr('class');
				}
				remove_notice_load();
			});
		}
	}
}

var Articles={
	Type: function(){
		if($('#type').val() == "plik"){
			$('.tresc').hide();
			$('.plik').show();
		}else{
			$('.plik').hide();
			$('.tresc').show();
		}
	}
}

var Basket={
	set_value: function(return_val,id,action){
		var b_count = return_val.count;
		var b_price = $.sprintf("%.2f",return_val.price_brutto);
		var b_transport = $.sprintf("%.2f",return_val.transport);
		var b_vat = $.sprintf("%.2f",(parseFloat(return_val.price_brutto)-parseFloat(return_val.price_netto)));
		var b_netto = $.sprintf("%.2f",return_val.price_netto);
		
		$('#count b').html((b_count)?b_count:"0");
		$('#value b').html((b_price && b_price != ".0")?b_price:"0.00");
		$('#transport b').html((b_transport && b_transport != ".0")?b_transport:"0.00");
		
		b_price =$.sprintf("%.2f",parseFloat(b_price)+parseFloat(b_transport));				
		$('#b_price').html((b_price && b_price != ".0")?b_price:"0.00");
		$('#b_vat').html((b_vat && b_vat != ".0")?b_vat:"0.00");
		$('#b_price_netto').html((b_netto && b_netto != ".0")?b_netto:"0.00");   
		$('#b_transport').html((b_transport && b_transport != ".0")?b_transport:"0.00");
						
		/*records*/
		if(action == "change"){
			/*$.each(return_val.products,function(key,value){
				if(id == parseInt(key)){
					var vat = $.sprintf("%.2f",parseFloat(value.item.vat)*parseFloat(value.item.price_pl)*parseInt(value.count)/100);
					var price = $.sprintf("%.2f",parseFloat(value.item.price_pl)*parseInt(value.count));
					$('#b_price_'+id).html((price && price != ".0")?price:"0.00");
					$('#b_vat_'+id).html((vat && vat != ".0")?vat:"0.00");
				}
			});*/
			var vat = $.sprintf("%.2f",parseFloat(return_val.vat_new));
			var price = $.sprintf("%.2f",parseFloat(return_val.price_new));
			$('#b_price_'+id).html((price && price != ".0")?price:"0.00");
			$('#b_vat_'+id).html((vat && vat != ".0")?vat:"0.00");
		}
		if(action == "delete"){
			$('#tr_'+id).remove();
			if(b_count == 0){
				$('.summary').remove();
			}
		}
	},
	change: function(id,element,URL_CHANGE){
		if(id,element){
			notice_load();
			integer(element);
			if($(element).val() == '00' || $(element).val() == '000' || $(element).val() == '0000'){$(element).val('0');}
			//if($(element).val() == ''){$(element).val('1');}
			if(id){
				var data = "id="+id+"&val="+$(element).val();
				$.post(URL_CHANGE, data, function(theResponse){
					if(theResponse){
						var return_val = JSON.parse(theResponse);
						Basket.set_value(return_val,id,"change");
						Basket.updateTransport();
					}
				});
			}
			
		}
	},
	updateTransport: function(){
		
		$.post('update_transport.html', '', function(theResponse){
			$('#consignment').empty();
			$('#consignment').html(theResponse);
			remove_notice_load();
		});
	},
	/*delete: function(id,URL_DELETE){
		if(id && URL_DELETE){
			var data = "id="+id;
			$.post(URL_DELETE, data, function(theResponse){
				if(theResponse){
						var return_val = JSON.parse(theResponse);
						Basket.set_value(return_val,id,"delete");
				}
			});
		}
	}*/
	ChangeConsignment : function(URL_CHANGE,value){
		if(URL_CHANGE){
			$.cookie('consignment',value);
			load_url(URL_CHANGE);
		}
	
	},
	clear: function(URL_CLEAR){
		notice_load();
		var data = "";
		$.post(URL_CLEAR, data, function(theResponse){
				remove_notice_load();
				$('#payform').submit();
			});
	}

}

var Order={
	show_address: function(){
		if($('#checkbox_address').attr('checked')){
			$('#shipping_address').slideDown('fast');
		}else{
			$('#shipping_address').slideUp('fast');
		}	
	},
	hide_address: function(){
		$('#shipping_address').hide();
	},
	SetHandlowy: function(URL_HANDLOWY,el,value){
		if(el){
		notice_load();
		var data = "id="+el+"&value="+value;
			$.post(URL_HANDLOWY, data, function(theResponse){
			$('#handlowy_'+el).attr('disabled',true);
			remove_notice_load();
			}); 		
		}
	},
	SetKsiegowa: function(URL_KSIEGOWA,el,value){
		if(el){
		notice_load();
		var data = "id="+el+"&value="+value;
			$.post(URL_KSIEGOWA, data, function(theResponse){
			$('#ksiegowa_'+el).attr('disabled',true);
			remove_notice_load();
			}); 		
		}
	}
}



var loader_count = 0;
function notice_load(){
	if($('#body_black_white').css('display') != 'block' && $('#body_black').css('display') != 'block'){
		$('body').prepend('<div id="body_black_white" class="body_black"></div><div id="loader"></div>');
		$('#body_black_white').css('height',''+$(document).height()+'px');
		$('#body_black_white').css('display','block');
		$width = $(window).width()/2;
		$height = $(window).height()/2;
		$('#loader').css({'left':$width+'px', 'top':$height+'px'});
	}
	loader_count++;
}

function remove_notice_load(){
	if(loader_count == 1){
	$('#body_black_white').remove();
	$('#loader').remove();
	}
	loader_count--;
}



