Ajax.Responders.register({
  onCreate: function() {
  	 Ajax.activeRequestCount++;
    $('ajaxstatusbar').update('Proszę czekać, trwa wczytywanie danych');
    $('ajaxstatusbar').show();
  },
  onComplete: function() {
  	Ajax.activeRequestCount--;
    $('ajaxstatusbar').update('');
    $('ajaxstatusbar').hide();
    if(Ajax.activeRequestCount>0){
    	$('ajaxstatusbar').update('Proszę czekać, trwa odświerzanie');
   		$('ajaxstatusbar').show();
    }
  }
});
function galleryreload(galleryid){
  	new Ajax.Updater('gallery-list', '/gallery/indexreload/id/'+galleryid, {
				method: 'get',
				asynchronous: false
			});
	Sortable.create('draglist',{onChange : function(){showsavebutton()},ghosting:true,constraint:false});
  }
function showsavebutton(){
 	$("savebutton").show()
}  
 function gallerysavesortReturn(response){
		try{
			var responseObject = response.responseText.parseJSON();
		}catch(e){
			alert('Prosimy o kontakt z administratorem serwisu. Blad 303');
		}
		
		if(responseObject.result==0){
			alert(responseObject.message);
		}else{
			alert(responseObject.message);
			galleryreload(responseObject.active_gallery_id);
		}
  }
  function gallerysavesort(galleryid){
  	var newTable = new Array();
  	var i =0;
	children = $('draglist').immediateDescendants();
	children.each(function(childElement) {
 		 newTable[i] = childElement.id;
 		 i++;
	});
	
	params = 'order='+newTable;
	new Ajax.Request('/gallery/sort/id/'+galleryid, {
		method: 'post',
		postBody: params,
		onSuccess: gallerysavesortReturn
	});
  	
  }  
  
function deletePhotoFromGalleryReturn(response){
		try{
			var responseObject = response.responseText.parseJSON();
		}catch(e){
			alert('Prosimy o kontakt z administratorem serwisu. Blad 303');
		}
		
		if(responseObject.jsonresult==0){
			/*alert(responseObject.message);*/
		}else{
			/*alert(responseObject.message);*/
			galleryreload(responseObject.active_gallery_id);
		}
	}
function deletePhotoFromGallery(galleryid,imageid){
	params = "imageid="+imageid+"&galleryid="+galleryid;
	new Ajax.Request('/gallery/delete/', {
		method: 'post',
		postBody: params,
		onSuccess: deletePhotoFromGalleryReturn
	});
}
function deleteGalleryReturn(response){
		try{
			var responseObject = response.responseText.parseJSON();
		}catch(e){
			alert('Prosimy o kontakt z administratorem serwisu. Blad 303');
		}
		
		
		window.location='/gallery';
	}
function deleteGallery(galleryid){
	new Ajax.Request('/gallery/deletegallery/galleryid/'+galleryid, {
		method: 'get',
		onSuccess: deleteGalleryReturn
	});
}
function addNewGalleryReturn(response){
		try{
			var responseObject = response.responseText.parseJSON();
		}catch(e){
			alert('Prosimy o kontakt z administratorem serwisu. Blad 303');
		}
		
		if(responseObject.jsonresult==0){
			//alert(responseObject.message);
		}else{
			//alert(responseObject.message);
			//galleryreload(responseObject.active_gallery_id);
		}
		window.location='/gallery';
	}
function addNewGallery(){
	params = "gallery_name="+$F('gallery_name')+"&gallery_description="+$F('gallery_description');
	new Ajax.Request('/gallery/addnewgallery/', {
		method: 'post',
		postBody: params,
		onSuccess: addNewGalleryReturn
	});
}
/**************PRODUKTY******************/
function productImportReturn(response){
	alert(response.responseText);
/*	try{
		var responseObject = response.responseText.parseJSON();
	}catch(e){
		alert('Prosimy o kontakt z administratorem serwisu. Blad 305');
	}*/
	
	
}
function productImport(){
	productList = new Array();
	i=0;
	elementList = document.getElementsByClassName('checkbox_product');
	elementList.each(function (element){
		if(element.checked==true){
			productList[i] = element.value;
			i++;
		}
	});
	
	params = "productlist="+productList;
	
	new Ajax.Request('/import/importNewProduct', {
		method: 'post',
		postBody: params,
		onSuccess: productImportReturn
	});
}
function genproductsymbol(){
  	new Ajax.Updater('product_symbol_text', '/product/genproductsymbol', {
			method: 'get'
		});
}
function updateProductStatus(productId,statusId){
		url = '/product/changestatus/productid/'+productId+'/statusid/'+statusId;
		params = '&value='+$F('productStatus_'+productId+'_'+statusId);
		
		new Ajax.Updater('error', url, {
				method: 'post',
				postBody: params
			});
		}
function checkproductextrainfo(){
  	new Ajax.Updater('checkproductextrainfo', '/product/checkproductextrainfo/product_extra_info/'+$F('product_extra_info'), {
			method: 'get'
		});
}

function productChangeCategoryReturn(response){
	try{
		var responseObject = response.responseText.parseJSON();
	}catch(e){
		alert('Prosimy o kontakt z administratorem serwisu. Blad 303');
	}
	
	if(responseObject.result==0){
		alert(responseObject.message);
	}else{
		//alert(responseObject.message);
		productreload(responseObject.categoryid, 1);
	}
}
function productChangeCategory(){
	productList = new Array();
	i=0;
	elementList = document.getElementsByClassName('checkbox_product');
	elementList.each(function (element){
		if(element.checked==true){
			productList[i] = element.value;
			i++;
		}
	});
	
	params = "productlist="+productList+"&newcategoryid="+$F('product_categorychange')+"&currentcategoryid="+$F('current_category_id');
	
	new Ajax.Request('/product/changeproductcategory/', {
		method: 'post',
		postBody: params,
		onSuccess: productChangeCategoryReturn
	});
}
function productreload(categoryid, page){
  	new Ajax.Updater('product-list', '/product/indexreload/id/'+categoryid+'/page/'+page, {
				method: 'get'
			});
  }

  function searchproductsymbol(){
  	params = 'search-product-symbol='+$F('search-product-symbol');
  	new Ajax.Updater('product-list', '/product/searchproduct', {
				method: 'post',
				postBody: params
			});
  }
   function searchproductname(){
  	params = 'search-product-name='+$F('search-product-name');
  	new Ajax.Updater('product-list', '/product/searchproduct', {
				method: 'post',
				postBody: params
			});
  }
  function searchproductquantity(){
  	params = 'search-product-quantity='+$F('search-product-quantity');
  	new Ajax.Updater('product-list', '/product/searchproduct', {
				method: 'post',
				postBody: params
			});
  } 
  function searchproductvisibility(){
  	params = 'search-product-visibility='+$F('search-product-visibility');
  	new Ajax.Updater('product-list', '/product/searchproduct', {
				method: 'post',
				postBody: params
			});
  }
  function deleteproduct(productId, categoryId, page){
  		new Ajax.Updater('error', '/product/delete/productid/'+productId,{
  				method: 'get',
  				asynchronous: false
  			});
  			
  		if(categoryId==null){
  			radioElement = document.getElementsByName('changeSearch');
  			if(radioElement[0].checked){
  				searchproductsymbol();
  			}else if(radioElement[1].checked){
  				searchproductname();
  			}else if(radioElement[2].checked){
  				searchproductquantity();
  			}else if(radioElement[3].checked){
  				searchproductvisibility();
  			}
  		}else{
  			productreload(categoryId, page);
  		}
  }
  
  function changeProductSearch(layer){
  	$('sl-product-symbol').hide();
  	$('sl-product-name').hide();
  	$('sl-product-quantity').hide();
  	$('sl-product-visibility').hide();
  	$(layer).show();
  }


/*******************/

function checkAll(tagname, check){
	checkboxlist = document.getElementsByName(tagname);
	
	for(i=0; i<checkboxlist.length; i++){
		checkboxlist[i].checked=check;
	}
}


function popupWindow(url, width, height) {
 	window.open(url,'popupWindow',"toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width="+width+",height="+height+",screenX=150,screenY=150,top=60,left=150")
}


function answer(msg){
	var answer=window.confirm(msg);
	if (answer){
		return true;
	}
	else{
		return false;
	}
}

/**
* funkcja pokazuje lub chowa warste w zaleznosci od jej obecnego stanu
*/
function displayLayer(id){
	element = document.getElementById(id);
	if(element.style.display=="none"){
		element.style.display="block";
		document.cookie=id+"=1";
	}else{
		element.style.display="none";
		document.cookie=id+"=0";
	}
}

/**
* funkcja zmienia warstwy
*/
function changeLayer(toShow, toHide){
	elementToShow = document.getElementById(toShow);
	elementToHide = document.getElementById(toHide);
	
	
	elementToShow.style.display="block";
	elementToHide.style.display="none";
	
		
}




function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=")
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1 
    c_end=document.cookie.indexOf(";",c_start)
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end))
    } 
  }
return null
}

