window.addEvent('load', function() {
	
	$(document.body).getElements('a[name=plink]').each(function(el){
		el.addEvent('click', function(ev) {
			new Event(ev).stop();
			new Request.JSON({url: "/req.php?r=promotion/", data: {href: el.get('href')}, onComplete: function(callback){
				window.location.href = el.get('href');
			}}).POST();
		});
	});
	
	$(document.body).getElements('select[name$=place]').addEvent('change', function() {
		new Request.JSON({url: "/req.php?r=smart/city/", data: {id: this.value}, onComplete: function(callback){
		    if(callback.status == 'ok'){
		    	$('city').set('html', callback.content);
		    }
		}}).POST();
	});
	
	$(document.body).getElements('a[name=nextsmart]').each(function(el){
		el.addEvent('click', function(){
			$('smart-step-form').set('send', {url: '/req.php?r=smart/', method: 'post', onSuccess: function(callback){
				var response = JSON.decode(callback);
				$('smart').set('html', response.content);
				el.addClass('hide');
				el.getParent().getElement('a[name=finishsmart]').removeClass('hide').addEvent('click', function(){
					this.addClass('hide');
					$('preloader').removeClass('hide');
					$('smart-form').set('send', {url: '/req.php?r=smart/send/', method: 'post', onSuccess: function(callback){
						var response = JSON.decode(callback);
						if(response.status == "ok")
						{
							$('smart').set('html', response.content);
							el.getParent().getElement('a[name=finishsmart]').addClass('hide');
							$('preloader').addClass('hide');
						}
						else {
							alert(response.content);
							el.getParent().getElement('a[name=finishsmart]').removeClass('hide');
							$('preloader').addClass('hide');
						}
					}});
					$('smart-form').send();
				});
			}});
			$('smart-step-form').send();
		});
	});
	
	$(document.body).getElements('a[name=pickdate]').each(function(el){
		el.addEvent('click', function(){
			el.getParent().getElement('div[name=pickdatebox]').toggleClass('hide');
		});
		el.getParent().getElement('div[name=pickdatebox]').getElement('a[name=close]').addEvent('click', function(){
			el.getParent().getElement('div[name=pickdatebox]').toggleClass('hide');
		});
	});
	
	$('slices').getElements('div.box').each(function(el){
		if($chk(el.getElement('a[name=more]')))
			el.getElement('a[name=more]').addEvent('click', function(ev) {
				if(el.getElement('div.morcuts').hasClass("hide"))
				{
					closeAll();
					el.getElement('div.morcuts').removeClass("hide");
					
					if(el.getElement('input[name=wopen]').value == 0)
					{
						el.getElement('input[name=wopen]').value = 1;
						new Request.JSON({url: "/req.php?r=engine/cuttings/", data: {id: el.getElement('input[name=group]').value, type: el.getElement('input[name=type]').value, L: window.location.pathname}, onComplete: function(callback){
						    el.getElement('div[name=fmorcuts]').set('html', callback.content);
						    el.getElement('div.loader').addClass('hide');
						    el.getElement('div[name=fmorcuts]').removeClass('hide');
						}}).POST();
					}
				}
				else
				{
					closeAll();
					el.getElement('div.morcuts').addClass("hide");
				}
				window.addEvent('keypress',function(e) { if(e.key == 'esc') { el.getElement('div.morcuts').addClass("hide"); } });
				$(document.body).addEvent('click',function(e) { 
					if(e.target != "") el.getElement('div.morcuts').addClass("hide");
				}.bind(this));
			});
		if($chk(el.getElement('div[name=closemore]')))
			el.getElement('div[name=closemore]').addEvent('click', function(ev) {
				closeAll();
				el.getElement('div.morcuts').addClass("hide");
			});
	});
	
	$('slices').getElements('div.multibox').each(function(el){
		if($chk(el.getElement('a[name=more]')))
			el.getElement('a[name=more]').addEvent('click', function(ev) {
				if(el.getElement('div.morcuts').hasClass("hide"))
				{
					closeAll();
					el.getElement('div.morcuts').removeClass("hide");
					
					if(el.getElement('input[name=wopen]').value == 0)
					{
						el.getElement('input[name=wopen]').value = 1;
						new Request.JSON({url: "/req.php?r=engine/cuttings/", data: {id: el.getElement('input[name=group]').value, type: el.getElement('input[name=type]').value, L: window.location.pathname}, onComplete: function(callback){
						    el.getElement('div[name=fmorcuts]').set('html', callback.content);
						    el.getElement('div.loader').addClass('hide');
						    el.getElement('div[name=fmorcuts]').removeClass('hide');
						}}).POST();
					}
				}
				else
				{
					closeAll();
					el.getElement('div.morcuts').addClass("hide");
				}
				window.addEvent('keypress',function(e) { if(e.key == 'esc') { el.getElement('div.morcuts').addClass("hide"); } });
				$(document.body).addEvent('click',function(e) { 
					if(e.target != "") el.getElement('div.morcuts').addClass("hide");
				}.bind(this));
			});
		if($chk(el.getElement('div[name=closemore]')))
			el.getElement('div[name=closemore]').addEvent('click', function(ev) {
				closeAll();
				el.getElement('div.morcuts').addClass("hide");
			});
	});
	
	$('slices').getElements('div.bigbox').each(function(el){
		if($chk(el.getElement('a[name=more]')))
			el.getElement('a[name=more]').addEvent('click', function(ev) {
				if(el.getElement('div.morcuts').hasClass("hide"))
				{
					closeAll();
					el.getElement('div.morcuts').removeClass("hide");
					
					if(el.getElement('input[name=wopen]').value == 0)
					{
						el.getElement('input[name=wopen]').value = 1;
						new Request.JSON({url: "/req.php?r=engine/cuttings/", data: {id: el.getElement('input[name=group]').value, type: el.getElement('input[name=type]').value, L: window.location.pathname}, onComplete: function(callback){
						    el.getElement('div[name=fmorcuts]').set('html', callback.content);
						    el.getElement('div.loader').addClass('hide');
						    el.getElement('div[name=fmorcuts]').removeClass('hide');
						}}).POST();
					}
				}
				else
				{
					closeAll();
					el.getElement('div.morcuts').addClass("hide");
				}
				window.addEvent('keypress',function(e) { if(e.key == 'esc') { el.getElement('div.morcuts').addClass("hide"); } });
				$(document.body).addEvent('click',function(e) { 
					if(e.target != "") el.getElement('div.morcuts').addClass("hide");
				}.bind(this));
			});
		if($chk(el.getElement('div[name=closemore]')))
			el.getElement('div[name=closemore]').addEvent('click', function(ev) {
				closeAll();
				el.getElement('div.morcuts').addClass("hide");
			});
	});
	
	$('slices').getElements('div.multibigbox').each(function(el){
		if($chk(el.getElement('a[name=more]')))
			el.getElement('a[name=more]').addEvent('click', function(ev) {
				if(el.getElement('div.morcuts').hasClass("hide"))
				{
					closeAll();
					el.getElement('div.morcuts').removeClass("hide");
					
					if(el.getElement('input[name=wopen]').value == 0)
					{
						el.getElement('input[name=wopen]').value = 1;
						new Request.JSON({url: "/req.php?r=engine/cuttings/", data: {id: el.getElement('input[name=group]').value, type: el.getElement('input[name=type]').value, L: window.location.pathname}, onComplete: function(callback){
						    el.getElement('div[name=fmorcuts]').set('html', callback.content);
						    el.getElement('div.loader').addClass('hide');
						    el.getElement('div[name=fmorcuts]').removeClass('hide');
						}}).POST();
					}
				}
				else
				{
					closeAll();
					el.getElement('div.morcuts').addClass("hide");
				}
				window.addEvent('keypress',function(e) { if(e.key == 'esc') { el.getElement('div.morcuts').addClass("hide"); } });
				$(document.body).addEvent('click',function(e) { 
					if(e.target != "") el.getElement('div.morcuts').addClass("hide");
				}.bind(this));
			});
		if($chk(el.getElement('div[name=closemore]')))
			el.getElement('div[name=closemore]').addEvent('click', function(ev) {
				closeAll();
				el.getElement('div.morcuts').addClass("hide");
			});
	});
	
	$('serachbox-form').addEvent('keydown', function(ev) {
		if(ev.key == 'enter')
		{
			new Event(ev).stop();
			window.location.href = "/" + $('serachbox').value + "/";
			//window.location.href = location.pathname + "" + $('serachbox').value + "/";
		}
	});
	
	$('serachbtn').addEvent('click', function() {
		window.location.href = "/" + $('serachbox').value + "/";
		//window.location.href = location.pathname + "" + $('serachbox').value + "/";
	});
	
	$(document.body).getElements('div.sale').each(function(sl){
		sl.addEvent('click', function(){
			window.open('/cupons/cupon/&id='+sl.getElement("input[name=sid]").value, 'cupon',"resizable,width=394,height=262,location=no, scrollbars=no"); 
  		});
	});
	
	$(document.body).getElements('a[name=dater]').each(function(sl){
		sl.addEvent('click', function(){
			if(sl.getParent().getElement("div[name=datebox]").hasClass("hide"))
			{
				sl.getParent().getElement("div[name=datebox]").removeClass("hide");
				sl.set('text', 'סגור זמינות');
			}
			else
			{
				sl.getParent().getElement("div[name=datebox]").addClass("hide");
				sl.set('text', 'זמינות');
			}
  		});
	});
	
	// calender
  	calenderNav();
	
	/* hide using opacity on page load */
	$('fb-modal').setStyles({
		opacity:0,
		display:'block'
	});
	
	$(document.body).getElements('a[name=contactus]').each(function(ct){
		ct.addEvent('click', function(){
			new Request.JSON({'url': '/req.php?r=/zimerim/contactus/', 'data': {id: ct.getElement("input[name=aid]").value}, 'onComplete': function(callback) {
				
				/* data */
				$('pop_content').set('html', callback.content);
				
				/* hiders */
				$('fb-close').addEvent('click',function(e) { $('fb-modal').fade('out'); });
				window.addEvent('keypress',function(e) { if(e.key == 'esc') { $('fb-modal').fade('out'); } });
				$(document.body).addEvent('click',function(e) { 
					if($('fb-modal').get('opacity') == 1 && !e.target.getParent('.generic_dialog')) { 
						$('fb-modal').fade('out'); 
					} 
				});
				
				$('fb-send').addEvent('click',function(e) { 
					$('contract-form').set('send', {url: '/req.php?r=zimerim/contactus/send', method: 'post', onSuccess: function(callback){
						var response = JSON.decode(callback);
						if(response.status == "ok")
						{
							$('pop_content').set('html', response.content);
							$('fb-close').addEvent('click',function(e) { $('fb-modal').fade('out'); });
								window.addEvent('keypress',function(e) { if(e.key == 'esc') { $('fb-modal').fade('out'); } });
								$(document.body).addEvent('click',function(e) { 
									if($('fb-modal').get('opacity') == 1 && !e.target.getParent('.generic_dialog')) { 
										$('fb-modal').fade('out'); 
									} 
								});
						}
						else if(response.status == "faild")
						{
							$('note-messege').setProperty('class', 'mfaild').setStyle('display', 'block').getElement('.txt').set('html', response.content); 
						}
					}});
					$('contract-form').send();
				});
				
			}}).send();
			$('fb-modal').fade('in');
  		});
	});
	
	if($chk($('olprice')))
	{
		$('olprice').addEvent('click', function(){
			new Request.JSON({url: "/req.php?r=engine/result/", method: 'post', data: {L: window.location.pathname, order: 'price', sc: 'asc'}, onComplete: function(callback){
				$('results').set('html', callback.data);
			}}).POST();
		});
	}
	
	if($chk($('ohprice')))
	{
		$('ohprice').addEvent('click', function(){
			new Request.JSON({url: "/req.php?r=engine/result/", method: 'post', data: {L: window.location.pathname, order: 'price', sc: 'desc'}, onComplete: function(callback){
				$('results').set('html', callback.data);
			}}).POST();
		});
	}
	
	if($chk($('orank')))
	{
		$('orank').addEvent('click', function(){
			new Request.JSON({url: "/req.php?r=engine/result/", method: 'post', data: {L: window.location.pathname, order: 'ranking', sc: 'desc'}, onComplete: function(callback){
				$('results').set('html', callback.data);
			}}).POST();
		});
	}
	
	// phone
	$(document.body).getElements("a[name=contacthide]").each(function(el){
		el.addEvent('click', function(){
			if($chk(el.getElement("input[name=number]").value))
			{
				new Request.JSON({url: "/req.php?r=zimerim/showphons/", method: 'post', data: {id: el.getElement("input[name=arid]").value, url: window.location.pathname}, onComplete: function(callback){}}).POST();
				el.set('html', el.getElement("input[name=number]").value/*+'<div class="showphone"></div>'*/);
				el.addClass('showen');
			}
		});
	});
	
});

function closeAll()
{
	$('slices').getElements('div.box').each(function(el){
		if($chk(el.getElement('div.morcuts')))
		{
			if(!el.getElement('div.morcuts').hasClass("hide"))
				el.getElement('div.morcuts').addClass("hide");
		}
	});
	$('slices').getElements('div.multibox').each(function(el){
		if($chk(el.getElement('div.morcuts')))
		{
			if(!el.getElement('div.morcuts').hasClass("hide"))
				el.getElement('div.morcuts').addClass("hide");
		}
	});
}

function calenderNav()
{
	date = new Date();
	thisMonth = date.getMonth() + 1;
	
	$(document.body).getElements("div.month").each(function(el){
		
		if(el.getProperty('id') == thisMonth)
			el.getElement("a[name=prev]").addClass('hide');
			
		if(el.getProperty('id') == 12)
			el.getElement("a[name=next]").addClass('hide');
		
		if($chk(el.getElement("a[name=next]")))
		el.getElement("a[name=next]").addEvent('click', function(){
			nextmonth = el.getProperty('id');
			nextmonth++;
			new Request.JSON({url: "/req.php?r=zimerim/calender/", method: 'post', data: {month: nextmonth, zid: el.getParent().getParent().getParent().getElement("input[name=zid]").value}, onComplete: function(callback){
				el.getParent().set('html', callback.data);
				calenderNav();
			}}).POST();
		});
		if($chk(el.getElement("a[name=prev]")))
		el.getElement("a[name=prev]").addEvent('click', function(){
			prevmonth = el.getProperty('id');
			prevmonth--;
			new Request.JSON({url: "/req.php?r=zimerim/calender/", method: 'post', data: {month: prevmonth, zid: el.getParent().getParent().getParent().getElement("input[name=zid]").value}, onComplete: function(callback){
				el.getParent().set('html', callback.data);
				calenderNav();
			}}).POST();
		});
	});
}