	/*function prod_option(id){
		
		var sel=$('prod_option'+id).getSelected();
		var total=parseFloat(sel.get('title'+id));
		$('pprice'+id).set('text',total.toFixed(2)+' лв.');
	}*/


window.addEvent('domready', function() {
	
	

	
	if($('prod_option')){
		$('prod_option').addEvent('domready',function(){
				var sel=this.getSelected();
				var total=parseFloat(this.getSelected().get('title'));
				$('pprice').set('text',total.toFixed(2)+' лв.');
		});
		
		$('prod_option').addEvent('change',function(){
				var sel=this.getSelected();
				var total=parseFloat(this.getSelected().get('title'));
				$('pprice').set('text',total.toFixed(2)+' лв.');
		});
	}	
	
	
	
	//counters	
	if($('counter')){ 
	
		dd = $('counter').get('enddeal');
		fdate = new Date(dd*1000);    
		day = fdate.getDate();
		month = fdate.getMonth()+1;
		year = fdate.getFullYear();
		timestamp = (dd*1000);
		setTimeout("countdown(day, month, year, timestamp)", 1000);
	
	}  
	
	if($('counter2')){ 
		
		dd2 = $('counter2').get('enddeala');
		fdate2 = new Date(dd2*1000);   
		day2 = fdate2.getDate();
		month2 = fdate2.getMonth()+1;
		year2 = fdate2.getFullYear();
		timestamp2 = (dd2*1000);
		setTimeout("countdown2(day2, month2, year2,timestamp2)", 1000);
	}
	
	
	
	
	// popup form deal
	if($('freedeal')){
		$('freedeal').setStyles({
			opacity:0,
			display:'block'
		});
		/* hiders */
		$('close').addEvent('click',function(e) { $('freedeal').fade('out'); });
		window.addEvent('keypress',function(e) { if(e.key == 'esc') { $('freedeal').fade('out'); } });
		$(document.body).addEvent('click',function(e) { 
			if($('freedeal').get('opacity') == 1 && !e.target.getParent('.generic_dialog')) { 
				$('freedeal').fade('out'); 
			} 
		});
	
		/* click to show */
		if($('login')){
			$('login').addEvent('click',function() {
				$('freedeal').fade('in');
			});
		}
	}


	//send popup form
	if($('senddeal')){ 
		 $('senddeal').addEvent('submit', function(e) {
			 
		  	 
		  //Prevents the default submit event from loading a new page.
		  e.stop();
		  //Empty the log and show the spinning indicator.
		  var log = $('logresult2').addClass('ajax-loading');
		  //Set the options of the form's Request handler. 
		  //("this" refers to the $('myForm') element).
		  this.set('send', {onComplete: function(response) { 
		   log.removeClass('ajax-loading');
		   if(response == 'success')
		   {
					$('freedeal').fade('out'); 
					$('freedeal').setStyle('display','none'); 
					$('freedeal').setStyle('opacity',0); 
					
					var box=$("resultdeal")
					box.set('html', '<div align="center"><img src="img/preloader.gif" alt="" /></div>')
					
					var req = new Request({
					url: 'dealbuy.php',
					method:'post',
					data: {
						success: true
					},
			
					onSuccess: function(txt){
						box.set('html', txt);
						//fade in
						box.get('tween', {property: 'opacity', onComplete : function(){ 
						}})
					},
			
			
					onFailure: function(){
						box.set('text', 'The request has failed')
						box.get('tween', {property: 'opacity', onComplete : function(){ 
						}})
					}
					});												
					req.send();

		   }
		   else
		   {
				 log.set('html', response);
		   }
		  }});
		  
		  //Send the form.
		  this.send();
		  return false;
		 });
	}

	
	
	
	

	//brands function
	if ($('brandbtn')){
		var brandul=$('brandul');
		var startheight=brandul.getSize().y;
		if(startheight<=130){
		$('brandbtn').setStyle('display','none');
		$('hideall').setStyle('display','none');	
		}else{
		brandul.setStyle('height',130);
	//	brandul.setStyle('overflow','hidden');

//		alert(startheight);
		$('brandbtn').addEvent('click', function(){
			var el=brandul;
			var current=el.getStyle('height');
			if(current=='130px'){
				var myEffect = new Fx.Morph(el, {duration: 'long', transition: Fx.Transitions.Sine.easeOut}); 
				myEffect.start({
					'height': [130, startheight] //Morphs the 'height' style from 10px to 100px.
	//				'width': [900, 300]  //Morphs the 'width' style from 900px to 300px.
				});
				$('viewall').set('text','Скрий');	
			}else{
				var myEffect = new Fx.Morph(el, {duration: 'long', transition: Fx.Transitions.Sine.easeOut}); 
				myEffect.start({
					'height': [startheight,130] //Morphs the 'height' style from 10px to 100px.
	//				'width': [900, 300]  //Morphs the 'width' style from 900px to 300px.
				});
				$('viewall').set('text','Виж Всички');	
			}
			return false;
			
		});
		}
	}
	
	


	if($("slider")){
			
		//slider variables for making things easier below
		var itemsHolder = $('container');
		var myItems = $$(itemsHolder.getElements('.item'));
		
		//controls for slider
		var theControls = $('controls1');
		var numNavHolder = $(theControls.getElement('ul'));
	//	var thePlayBtn = $(theControls.getElement('.play_btn'));
	//	var thePrevBtn = $(theControls.getElement('.prev_btn'));
	//	var theNextBtn = $(theControls.getElement('.next_btn'));
	    var thePrevBtn = $('sback');
        var theNextBtn = $('snext'); 
		
		
		//create instance of the slider, and start it up		
		var mySlider = new SL_Slider({
			slideTimer: 12000,
			orientation: 'none',      //vertical, horizontal, or none: None will create a fading in/out transition.
			fade: false,                    //if true will fade the outgoing slide - only used if orientation is != None
			isPaused: false,
			container: itemsHolder,
			items: myItems,
			numNavActive: true,
			numNavHolder: numNavHolder
			
		});
		mySlider.start();

	}
	
	
	
	
	if($('bulletin')){ 
		 $('bulletin').addEvent('submit', function(e) {
		  //Prevents the default submit event from loading a new page.
		  e.stop();
		 $('logres').setStyle('display','block');
		 //Empty the log and show the spinning indicator.
		  var log = $('logres').set('html', '').addClass('ajax-loading');
		  //Set the options of the form's Request handler. 
		  //("this" refers to the $('myForm') element).
		  this.set('send', {onComplete: function(response) { 
		  
		 
		  log.removeClass('ajax-loading');
			log.set('html', response);
		 
		  }});
		  //Send the form.
		  this.send();
		 });
    }

	if($('checkadr')){
	$('checkadr').addEvent('change',function(){
	    var pa=$('payment_address');
	    if(pa.getStyle('display')=="none"){
	    pa.setStyle('display','table');
	    }else{
	    pa.setStyle('display','none');
	        
	    }
	});
	}	

	
	if(document.getElementById('shiping_method')){
		$('shiping_method').addEvent('domready',function(){
				var sel=this.getSelected();
				var total=parseFloat(this.getSelected().get('title'))+parseFloat($('end_price').get('value'));
				$('total').set('text',total.toFixed(2));
		});
		
		$('shiping_method').addEvent('change',function(){
				var sel=this.getSelected();
				var total=parseFloat(this.getSelected().get('title'))+parseFloat($('end_price').get('value'));
				$('total').set('text',total.toFixed(2));
		});
	}



	$$('#field1, #field2, #field3, #field4, #field5, #field6').addEvents({

		'keydown':function(){
			name=this.id;
			newname=this.id+"-2";
		},

		'keyup':function(){
			name=this.id;
			newname=this.id+"-2";
			$(newname).value=this.value;
		},

		'blur':function(){
			name=this.id;
			newname=this.id+"-2";
			$(newname).value=this.value;
			$(newname).setStyle('color','#000000');
		},

		'focus':function(){
			name=this.id;
			newname=this.id+"-2";
			$(newname).setStyle('color','#2a9fd6');
		}
	});


	/*if($('nav')){
		var myMenu = new MenuMatic(
		{
			orientation:'vertical', 
			id:'nav'
		});			
	}   */     	

	/****** FILTER BULL SHITS ************/

	var callJax = function(){
		var cat=$('caturl').get('value');

		cat=encodeURIComponent(cat).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
		var brand=$('brand').get('value');
		var pmin=$('min').get('value');
		var pmax=$('max').get('value');
		var regular=$('regular').get('value');
		var filter=$('filter').get('value');
		var page=$('page').get('value');
		var s=$('s').get('value');
		//
		var url='http://'+window.location.hostname+'/results.php?caturl='+cat+'&brands='+brand+'&price_range=true&min='+pmin+'&max='+pmax+'&regular='+regular+'&page='+page+'&s='+s;
			if(filter!=''){
				url+='&filter='+filter;
			}
			
			var request = new Request({
			url:url,
			method:'post',
			onRequest: function(){
				$('results').set('html','');
				$('results').addClass('loading');
			},
			onComplete: function(response) { 
				$('results').removeClass('loading');
				if(response){
					$('results').set('html',response);
				}else{
					$('results').innerHTML = response;
				}
				
				
				//PAGES
				$$('.paging li a').addEvent('click',function(){
					$('page').set('value',this.get('title'));
					var myFx = new Fx.Scroll(document).toElement('pagetop');
					callJax();
					return false;
				});
				
			}
		}).send();
	}



	//sort select
	$$('.sort_select').addEvent('change',function(){
		var val=$('sortselect').get('value')+'-'+$('perpageselect').get('value');
		$('s').set('value',val);
		callJax();
		return false;
	});
	
	
	
	//DISCOUNT CODE AJAX
	if($('discount_code') && $('discount_submit')){
		var discount_submit=$('discount_submit');
		var discount_code=$('discount_code');
		discount_code.addEvent('focus',function(){
			if(this.value=='Въведете код за отстъпка'){
				this.value='';
			}
	});
	
	discount_code.addEvent('blur',function(){
		if(this.value==''){
			this.value='Въведете код за отстъпка';
		}
	});
	
	discount_submit.addEvent('click',function(){
		//discount
		var url = '/shoppingajax.php?type=discount&code=' + discount_code.value+'&sum=' +discount_submit.alt;
		var request = new Request({
			url:url,
			method:'post',
			onComplete: function(response) { 
				$('off_res').set('text',response);
				$('discount').set('value',0);
				$('discount_id').set('value',0);
				if(res=JSON.decode(response)){
						text='<span>'+res.name+"</span> "+" <strong>- "+res.minus+"лв.</strong>"
						$('discount').set('value',res.minus);
						$('discount_id').set('value',res.code);
						
						//alert(res.code);
						$('discount_code').set('readonly','true');
						$('discount_code').setStyle('opacity',0.5);
						//
						var total=(parseFloat($('end_price').get('value'))-parseFloat(res.minus))+parseFloat($('shiping_method').getSelected().get('title'));
						$('total').set('text',total.toFixed(2));
						//
						$('off_res').set('html',text);
				}
			} 
		}).send();
		return false;

	});
}







});


/**********************************************End domready**************************************************************/

function countdown(yr,m,d,ts){

	var current="Expired";
	var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
    
	theyear=yr;themonth=m;theday=d;thets=ts;
	var today=new Date();
	var todayy=today.getYear();
	if (todayy < 1000)
	    todayy+=1900;
	var todaym=today.getMonth();
	var todayd=today.getDate();
	var todayh=today.getHours();
	var todaymin=today.getMinutes();
	var todaysec=today.getSeconds();
	var todaystring=montharray[todaym]+" "+todayd+", "+todayy+" "+todayh+":"+todaymin+":"+todaysec;
	
	//futurestring=montharray[m-1]+" "+d+", "+yr;
	//dd=Date.parse(futurestring)-Date.parse(todaystring);
	futurestring=montharray[m-1]+" "+d+", "+yr;
	dd=Date.parse(thets)-Date.parse(todaystring);
	
	dday=Math.floor(dd/(60*60*1000*24)*1);
	dhour=Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1);
	dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1);
	dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1);
	if(dday==0&&dhour==0&&dmin==0&&dsec==1){
		//document.getElementById('counter').style.display='none';
		//document.getElementById('expired').style.display='block';
		return;
	}
	else{
		//document.getElementById('countdown_day').innerHTML=dday;
		if(dday>0) { dhours = (dday*24)+dhour } else { dhours = dhour }
		document.getElementById('countdown_hour').innerHTML=dhours;
		document.getElementById('countdown_min').innerHTML=dmin;
		document.getElementById('countdown_sec').innerHTML=dsec;
		setTimeout("countdown(theyear,themonth,theday,thets)",1000);
	}
}

function countdown2(yr,m,d,ts){

	var current="Expired";
	var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
    
	theyear2=yr;themonth2=m;theday2=d;thets2=ts;
	var today=new Date();
	var todayy=today.getYear();
	if (todayy < 1000)
	    todayy+=1900;
	var todaym=today.getMonth();
	var todayd=today.getDate();
	var todayh=today.getHours();
	var todaymin=today.getMinutes();
	var todaysec=today.getSeconds();
	var todaystring=montharray[todaym]+" "+todayd+", "+todayy+" "+todayh+":"+todaymin+":"+todaysec;
	
	//futurestring=montharray[m-1]+" "+d+", "+yr;
	//dd=Date.parse(futurestring)-Date.parse(todaystring);
	futurestring=montharray[m-1]+" "+d+", "+yr;
	dd=Date.parse(thets2)-Date.parse(todaystring);
	
	dday=Math.floor(dd/(60*60*1000*24)*1);
	dhour=Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1);
	dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1);
	dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1);
	if(dday==0&&dhour==0&&dmin==0&&dsec==1){
		//document.getElementById('counter').style.display='none';
		//document.getElementById('expired').style.display='block';
		return;
	}
	else{
		//document.getElementById('countdown_day').innerHTML=dday;
		if(dday>0){ dhours = (dday*24)+dhour } else { dhours = dhour }
		document.getElementById('countdown_hour2').innerHTML=dhours;
		document.getElementById('countdown_min2').innerHTML=dmin;
		document.getElementById('countdown_sec2').innerHTML=dsec;
		setTimeout("countdown2(theyear2,themonth2,theday2,thets2)",1000);
	}
}
