window.addEvent('domready', function() {
	$$('.searchInput').addEvent('click', function() {
		this.value = '';
	})
})




/**
 * @author iplus
 */
var busy = false;
var root = '/admin/';
var imgRoot = '/';
var changeDiv = 'contentShop';

function active(site, model, actPage, tp, filter ) {
	var res=null;
	res=$$('input[class=act]');
	res.each(function(item,i){
		item.onclick=function() {
			var val;
			if(item.checked)
				val=1;
			else	
				val=0;
			var id=item.getProperty('id').replace("act",'');
			var req = new Request.JSON({
				url: site + model+'/active/'+id+'/'+val,
				onRequest: function() {
					$('loading').style.display = 'block';
					$('act'+id).disabled = true;
				},
				onSuccess: function() {
					news(site, 'products', actPage, tp, filter );
					active(site, 'products', actPage, tp, filter);
					next(site, actPage, tp);
					prev(site, actPage, tp);
					search( site, 'products', actPage, tp, filter );
					del(site, 'products', actPage);
					gotopage( site, actPage, tp );
					pro( site, 'products', actPage, tp, filter );
					pri(site, 'products', actPage, tp, filter);
					filterSubCat(site, actPage, tp, search);
					$('loading').style.display = 'none';
					$('act'+id).disabled = false;
				}
			});		
			req.send();
		}				
	})   
}



function news(site, model, actPage, tp, filter ) {		
	var res=null;
	res=$$('input[class=news]');
	res.each(function(item,i){
		item.onclick=function() {
			var val;
			if(item.checked)
				val=1;
			else	
				val=0;
			var id=item.getProperty('id').replace("news",'');
			var req = new Request.JSON({
				url: site + model+'/news/'+id+'/'+val,
				onRequest: function() {
					$('loading').style.display = 'block';
					$('news'+id).disabled = true;
				},
				onSuccess: function() {
					news(site, 'products', actPage, tp, filter );
					active(site, 'products', actPage, tp, filter);
					next(site, actPage, tp);
					prev(site, actPage, tp);
					search( site, 'products', actPage, tp, filter );
					gotopage( site, actPage, tp );
					pro( site, 'products', actPage, tp, filter );
					del(site, 'products', actPage, tp, filter);
					pri(site, 'products', actPage, tp, filter);
					filterSubCat(site, actPage, tp, search);
					$('loading').style.display = 'none';
					$('news'+id).disabled = false;
				}
			});		
			req.send();
		}				
	})   
}




function sure() {
	var check = window.confirm("Jesteś pewien/pewna?");
	if( check == true ) {
		return true;
	}
	else if ( check == false ) {
		return false;
	}
}

var tp;

function search( site, controller, actPage, tp, filter ){
	if (filter == undefined || filter == 'none') {
		var url = site + controller + '/totalPagesFromSearch/';
		var urlSearch = site + controller + '/search/' + actPage;
	}
	else {
		var url = site + controller + '/totalPagesFromSearch/' + filter;
		var urlSearch = site + controller + '/search/' + actPage + '/' + filter;
	}
	$('name_search_sub').onclick = function(){	
		var param = $('name_search').value;
		var myHTMLRequest = new Request.HTML({
			url: url,
			onSuccess: function(first, second, responseHTML, responseJavaScript){
				tp = responseHTML;
				var myHTMLRequest = new Request.HTML({
					url: urlSearch,
					onSuccess: function(first, second, responseHTML, responseJavaScript){
						$(changeDiv).innerHTML = responseHTML +'<script>'+ responseJavaScript + '</scrip' + 't>';
						filterSubCat(site, 1, tp, search);
						search( site, controller, actPage, tp, filter );
						active(site, 'products', actPage, tp, filter);
						news(site, 'products', actPage, tp, filter );
						gotopage( site, actPage, tp, param );
						del(site, 'products', actPage, tp, filter);
						active(site, 'products', actPage);
						pri(site, 'products', actPage, tp, filter);
						next(site, actPage, tp, param);
						pro( site, 'products', actPage, tp, filter);
						prev(site, actPage, tp, param);
					}
				}).get({
					'name': param
				});
			}
		}).get({
			'name': param
		});
	}
}

function prev( site, actPage, tp, searchName, filter ) {
	if ($('prv') != null) {
		$('prv').addEvent('click', function(){
			if (busy == false) {
				busy = true;
				if (searchName != undefined) {
					var myHTMLRequest = new Request.HTML({
						url: site + 'products/search/' + (actPage.toInt() - 1),
						onRequest: function(){
							$('loading').style.display = 'block';
						},
						onSuccess: function(first, second, responseHTML, responseJavaScript){
							busy = false;
							$('loading').style.display = 'none';
							$(changeDiv).innerHTML = responseHTML + '<script>' + responseJavaScript + '</scrip' + 't>';
							actPage -= 1;
							news(site, 'products', actPage, tp );
							del(site, 'products', actPage, tp);
							active(site, 'products', actPage, tp);
							pri(site, 'products', actPage, tp);
							gotopage( site, actPage, tp, searchName );
							search( site, 'products', actPage, tp );
							filterSubCat(site, actPage, tp, searchName);
							pro( site, 'products', actPage, tp );
							if (actPage < tp) 
								next(site, actPage, tp, searchName);
							if (actPage > 1) 
								prev(site, actPage, tp, searchName);
						}
					}).get({
						'name': searchName
					});
				}
				else {
					var url;
					if( filter != undefined )
						url = site + 'products/categoryAjax/' + (actPage.toInt() - 1) + '/' + filter;
					else 
						url = site + 'products/categoryAjax/' + (actPage.toInt() - 1);
					var myHTMLRequest = new Request.HTML({
						url: url,
						onRequest: function(){
							$('loading').style.display = 'block';
						},
						onSuccess: function(first, second, responseHTML, responseJavaScript){
							busy = false;
							$('loading').style.display = 'none';
							$(changeDiv).innerHTML = responseHTML + '<script>' + responseJavaScript + '</scrip' + 't>';
							actPage = actPage.toInt() - 1;
							news(site, 'products', actPage, tp );
							del(site, 'products', actPage);
							active(site, 'products', actPage);
							gotopage( site, actPage, tp, searchName, filter );
							pri(site, 'products', actPage);
							search( site, 'products', actPage, tp );
							filterSubCat(site, actPage);
							pro( site, 'products', actPage, tp );
							if (actPage < tp) 
								next(site, actPage, tp, searchName, filter);
							if (actPage > 1) 
								prev(site, actPage, tp, searchName, filter);
						}
					}).send();
				}
			}
		})
	}
}

function next( site, actPage, tp, searchName, filter ) {
	if ($('nxt') != null) {
		$('nxt').addEvent('click', function(){
			if (busy == false) {
				busy = true;
				if (searchName != undefined) {
					var myHTMLRequest = new Request.HTML({
						url: site + 'products/search/' + (actPage + 1),
						onRequest: function(){
							$('loading').style.display = 'block';
						},
						onSuccess: function(first, second, responseHTML, responseJavaScript){
							busy = false;
							$('loading').style.display = 'none';
							$(changeDiv).innerHTML = responseHTML + '<script>' + responseJavaScript + '</scrip' + 't>';
							actPage += 1;
							news(site, 'products', actPage, tp );
							active(site, 'products', actPage);
							gotopage( site, actPage, tp, searchName, filter );
							del(site, 'products', actPage, tp);
							search( site, 'products', actPage, tp );
							filterSubCat(site, actPage);
							pro( site, 'products', actPage, tp );
							if (actPage < tp) 
								next(site, actPage, tp, searchName, filter);
							prev(site, actPage, tp, searchName, filter);
						}
					}).get({
						'name': searchName
					});
				}
				else {
					if( filter != undefined )
						url = site + 'products/categoryAjax/' + (actPage.toInt() + 1) + '/' + filter;
					else 
						url = site + 'products/categoryAjax/' + (actPage.toInt() + 1);
					var myHTMLRequest = new Request.HTML({
						url: url,
						onRequest: function(){
							$('loading').style.display = 'block';
						},
						onSuccess: function(first, second, responseHTML, responseJavaScript){
							busy = false;
							$('loading').style.display = 'none';
							$(changeDiv).innerHTML = responseHTML + '<script>' + responseJavaScript + '</scrip' + 't>';
							actPage = actPage.toInt() + 1;
							news(site, 'products', actPage, tp );
							del(site, 'products', actPage, tp);
							gotopage( site, actPage, tp, searchName, filter);
							active(site, 'products', actPage, tp);
							search( site, 'products', actPage, tp );
							pri(site, 'products', actPage, tp);
							filterSubCat(site, actPage, tp);
							pro( site, 'products', actPage, tp );
							if (actPage < tp) 
								next(site, actPage, tp, searchName, filter);
							prev(site, actPage, tp, searchName, filter);
						}
					}).send();
				}
			}
		});
	}
}
function del( site, controller, actPage, tp, filter ) {
		res = $$('img[class=deleteClass]');
		res.each(function(item, i){
			item.onclick = function(){
				var ans = confirm("Napewno skasować?");
				if (ans) {
					if (busy == false) {
						busy = true;
						var id = item.getProperty('id').replace("deleteButton", '');
						var myHTMLRequest = new Request.HTML({
							url: site + controller + '/delete/' + id + '/' + actPage,
							onRequest: function(){
								$('loading').style.display = 'block';
							},
							onSuccess: function(first, second, responseHTML, responseJavaScript){
								busy = false;
								$('loading').style.display = 'none';
								$(changeDiv).innerHTML = responseHTML + '<script>' + responseJavaScript + '</scrip' + 't>';
								del(site, controller, actPage, tp, filter);
								news(site, 'products', actPage, tp, filter);
								active(site, controller, actPage, tp, filter);
								if ($('addButton') != undefined) {
									addCat(site, controller);
									editCat(site, controller);
								}
								else {
									filterSubCat(site, actPage);
								}
								pri(site, controller, actPage, tp, filter);
								pro(site, 'products', actPage, tp, filter);
								gotopage( site, actPage, tp );
								active(site, 'products', actPage, filter);
								search( site, 'products', actPage, tp, filter );
								next(site, actPage, tp);
								prev(site, actPage, tp);
							}
						}).send();
					}
				}
			}
		})
}

function filterSubCat( site, actPage, tp ) {
	$('selectCat').addEvent('change', function() {
		if (busy == false) {
			busy = true;
			actPage = 1;
			var param = $('selectCat').value;
			var myHTMLRequest = new Request.HTML({
				url: site + 'products/filterBySubCat/' + param + '/' + 1,
				onRequest: function() {
					$('loading').style.display = 'block';
					$('selectCat').disabled = true;
				},
				onSuccess: function(first, second, responseHTML){
					busy = false;
					$('selectCat').disabled = false;
					$('loading').style.display = 'none';
					$(changeDiv).innerHTML = responseHTML;
					filterSubCat(site, 1, tp, search);
					active(site, 'products', actPage, tp, param);
					news(site, 'products', actPage, tp, param );
					gotopage( site, actPage, tp, undefined, param );
					del(site, 'products', actPage, tp, param);
					search( site, 'products', actPage, tp, param );
					active(site, 'products', actPage);
					pri(site, 'products', actPage, tp, param);
					next(site, actPage, tp, undefined, param);
					pro( site, 'products', actPage, tp, param);
					prev(site, actPage, tp, undefined, param);
				//del( catId, actPage, tp, search );
				//next( '/rondo/admin/products/category', catId, actPage, tp, search );
				//prev( '/rondo/admin/products/category', catId, actPage, tp, search );
				}
			}).send();
		}
	})
}

function addCat( site, controller, actPage ) {
	if(!actPage) actPage = false;
	$('addButton').addEvent('click', function() {
		if ($('cat').value != '') {
			if (busy == false) {
				busy = true;
				var catName = $('cat').value;
				var parentName = $('selectCat2').value;
				var myHTMLRequest = new Request.HTML({
					url: site + controller + '/addCategory/' + parentName,
					onRequest: function(){
						$('loading').style.display = 'block';
					},
					onSuccess: function(first, second, responseHTML){
						busy = false;
						$('loading').style.display = 'none';
						$(changeDiv).innerHTML = responseHTML;
						del(site, controller);
						addCat(site, controller);
						pri(site, controller, actPage);
					}
				}).send({
					method: 'post',
					data: 'name='+catName
				});
			}
		}
	})
}


function pri( site, controller, actPage, tp, filter ) {
		var res=$$('input[class=pri]');
		res.each(function(item,i){
			item.onkeyup=function() {
				if (busy == false) {
					busy = true;
					var id = item.getProperty('id').replace("pri", '');
					var url;
					if( filter == undefined )
						url = site + controller + '/pri/' + id + '/' + item.value + '/' + actPage;
					else
						url = site + controller + '/pri/' + id + '/' + item.value + '/' + actPage + '/' + filter;
					var myHTMLRequest = new Request.HTML({
						url: url,
						onRequest: function(){
							$('loading').style.display = 'block';
							item.disabled = true;
						},
						onSuccess: function(first, second, responseHTML, responseJavaScript){
							busy = false;
							item.disabled = false;
							$('loading').style.display = 'none';
							$(changeDiv).innerHTML = responseHTML + '<script>' + responseJavaScript + '</scrip' + 't>';
							pri(site, controller, actPage, tp, filter);
							active(site, controller, actPage, tp, filter);
							news(site, controller, actPage, tp, filter );
							del(site, controller, actPage, tp, filter);
							next(site, actPage, tp);
							gotopage( site, actPage, tp );
							prev(site, actPage, tp);
							pro( site, controller, actPage, tp );
							if ($('addButton') != undefined) {
								addCat(site, controller);
								editCat(site, controller);
							} else {
								filterSubCat(site, actPage, tp);
								search( site, 'products', actPage, tp, filter );
							}
						}
					}).send();
				}
			}				
		}
		)
	}

function pro( site, model, actPage, tp, filter ) {
	res = $$('input[class=pro]');
	res.each(function(item, i){
		item.onclick = function(){
			var id = item.getProperty('id').replace("pro", '');
			var td = $('proTd' + id);
			if(item.checked) {
				td.innerHTML = "<input class=\"newPrice\" type=\"text\" id=\"newPrice"+id+"\" />";
				addPromotion( site, model, actPage, tp );
			} else {
				td.innerHTML = "";
				delPromotion( id, site, model, actPage, tp, filter );
				active(site, 'products', actPage, tp, filter);
				news(site, 'products', actPage, tp, filter );
				next(site, actPage, tp);
				gotopage( site, actPage, tp );
				prev(site, actPage, tp);
				search( site, 'products', actPage, tp, filter );
				del(site, 'products', actPage, tp, filter);
				pri(site, 'products', actPage);
				filterSubCat(site, actPage, tp );
				pro( site, model, actPage, tp, filter );
			}
			}
		})
}
function delPromotion( id, site, model, actPage, tp, filter ) {
	var req = new Request.JSON({
				url: site + model + '/delPromotion/' + id,
				onRequest: function() {
					$('loading').style.display = 'block';
				},
				onSuccess: function() {
					news(site, 'products', actPage, tp, filter );
					next(site, actPage, tp);
					prev(site, actPage, tp);
					del(site, 'products', actPage, tp, filter);
					pri(site, 'products', actPage, tp, filter);
					active(site, 'products', actPage, tp, filter);
					gotopage( site, actPage, tp );
					search( site, 'products', actPage, tp, filter );
					filterSubCat(site, actPage, tp, search);
					pro( site, model, actPage, tp, filter );
					$('loading').style.display = 'none';
				}
			});		
	req.send();
}

function addPromotion( site, model, actPage, tp, filter ) {
	var res=null;
	res=$$('input[class=newPrice]');
	res.each(function(item,i){
		item.onkeyup=function() {
			var id=item.getProperty('id').replace("newPrice",'');
			var val = $('newPrice' + id).value;
			var req = new Request.JSON({
				url: site + model + '/addPromotion/' + id + '/' + val,
				onRequest: function() {
					$('loading').style.display = 'block';
				},
				onSuccess: function() {
					news(site, 'products', actPage, tp, filter );
					next(site, actPage, tp);
					prev(site, actPage, tp);
					gotopage( site, actPage, tp );
					del(site, 'products', actPage, tp, filter);
					active(site, 'products', actPage, tp, filter);
					pri(site, 'products', actPage, tp, filter);
					search( site, 'products', actPage, tp, filter );
					filterSubCat(site, actPage, tp, search);
					pro( site, model, actPage, tp, filter );
					$('loading').style.display = 'none';
				}
			});		
			req.send();
		}				
	})   
}

function editCat( site, controller ) {
	res = $$('img[class=editClass]');
	res.each(function(item, i){
		item.onclick = function(){
			if (busy == false) {
				busy = true;
				var id = item.getProperty('id').replace("editButton", '');
				var cat = $('nameCat' + id);
				var nameCat = cat.innerHTML;
				cat.innerHTML = "<input type='text' id='save" + id + "' value='" + nameCat + "' /> ";
				cat.innerHTML += "<img src=\"" + imgRoot + "media/images/admin/modify.png\" onclick=\"save('" + site + "', '" + controller + "', '" + id + "')\" />";
				cat.innerHTML += "<img src=\"" + imgRoot + "media/images/admin/cancel.png\" onclick=\"cancel('" + site + "', '" + controller + "', '" + id + "', '" + nameCat + "')\" />";
			}
		}
	})
}

function save( site, controller, id ) {
	var value = $('save'+id).value;
	var myHTMLRequest = new Request.HTML({
			url: site + 'categories/save/' + id,
			onRequest: function(){
				$('loading').style.display = 'block';
			},
			onSuccess: function(first, second, responseHTML, responseJavaScript){
				busy = false;
				$('loading').style.display = 'none';
				$(changeDiv).innerHTML = responseHTML + '<script>' + responseJavaScript + '</scrip' + 't>';
				addCat( site, controller );
				del( site, controller );
				pri( site, controller );
				editCat( site, controller );
			}
	}).send({
		method: 'post',
		data: 'val=' + value
	});
}

function cancel( site, controller, id, val) {
	busy = false;
	$('nameCat' + id).innerHTML = val;
}

function showSubCat( child ) {
	var res=null;
	res=$$('tr[class='+child+']');
	res.each(function(item,i){
		var id=item.getProperty('class').replace("child",'');
		if (item.style.display == 'table-row') {
			item.style.display = 'none';
			$(id).innerHTML = '<img class="plus" src="/annawrona/media/images/admin/plus.png" />';
		}
		else {
			item.style.display = 'table-row';
			$(id).innerHTML = '<img class="plus" src="/annawrona/media/images/admin/minus.png" />';
		}
	})
}

function createHtmlToMsg(msg){
	var html = '<table cellpadding="0" cellspacing="0">' +
	'<tr>' +
	'<td class="leftUp"></td><td class="middleUp"></td><td class="rightUp"></td>' +
	'</tr>' +
	'<tr>' +
	'<td class="leftMiddle"></td><td class="bodyMsg">' +
	msg +
	'</td><td class="rightMiddle"></td>' +
	'</tr>' +
	'<tr>' +
	'<td class="leftDown"></td><td class="middleDown"></td><td class="rightDown"></td>' +
	'</tr>' +
	'</table>';
	return html;
}


function showMsg( msg ) {
		var fx = new Fx.Tween('box');
		$('box').style.display="block";
		$('box').innerHTML = createHtmlToMsg(msg);
		setTimeout("$('box').fade('out');", 2500);
}


function gotopage( site, actPage, tp, searchName, filter ) {
	res=$$('span[class=goto]');
	res.each(function(item,i){
		
		item.onclick = function(){
			if (busy == false) {
				busy = true;
				var actPage = item.getProperty('id').replace("goto",'');
				if (searchName != undefined) {
					var myHTMLRequest = new Request.HTML({
						url: site + 'products/search/' + actPage,
						onRequest: function(){
							$('loading').style.display = 'block';
						},
						onSuccess: function(first, second, responseHTML, responseJavaScript){
							busy = false;
							$('loading').style.display = 'none';
							$(changeDiv).innerHTML = responseHTML + '<script>' + responseJavaScript + '</scrip' + 't>';
							//actPage -= 1;
							news(site, 'products', actPage, tp );
							gotopage( site, actPage, tp, searchName );
							del(site, 'products', actPage, tp);
							active(site, 'products', actPage, tp);
							pri(site, 'products', actPage, tp);
							search( site, 'products', actPage, tp );
							filterSubCat(site, actPage, tp, searchName);
							pro( site, 'products', actPage, tp );
							if (actPage < tp) 
								next(site, actPage+1, tp, searchName);
							if (actPage > 1) 
								prev(site, actPage-1, tp, searchName);
						}
					}).get({
						'name': searchName
					});
				}
				else {
					if( filter != undefined )
						url = site + 'products/categoryAjax/' + actPage + '/' + filter;
					else 
						url = site + 'products/categoryAjax/' + actPage;
					var myHTMLRequest = new Request.HTML({
						url: url,
						onRequest: function(){
							$('loading').style.display = 'block';
						},
						onSuccess: function(first, second, responseHTML, responseJavaScript){
							busy = false;
							$('loading').style.display = 'none';
							$(changeDiv).innerHTML = responseHTML + '<script>' + responseJavaScript + '</scrip' + 't>';
							//actPage -= 1;
							news(site, 'products', actPage, tp );
							del(site, 'products', actPage);
							active(site, 'products', actPage);
							gotopage( site, actPage, tp, searchName, filter );
							pri(site, 'products', actPage);
							search( site, 'products', actPage, tp );
							filterSubCat(site, actPage);
							pro( site, 'products', actPage, tp );
							if (actPage < tp) 
								next(site, actPage, tp, searchName, filter);
							if (actPage > 1) 
								prev(site, actPage, tp, searchName, filter);
						}
					}).send();
				}
			}
		}
	})
}
