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


	if($('livein')){
		
		$('livein').addEvent('domready',function(){
				
			var naem = $('livein').get('value');
			
			if(naem == 2){
				
				$('rent').setStyle('display','block');
			}else{
				$('rent').setStyle('display','none');
			}
		});
			
		$('livein').addEvent('change',function(){
				
			var naem = $('livein').get('value');
			
			if(naem == 2){
				
				$('rent').setStyle('display','block');
			}else{
				$('rent').setStyle('display','none');
			}
		});
	}




	/* AUTO COMPLETER */
		new Autocompleter.Ajax.Json('search', '/get_terms.php', {
		'postVar': 'search'
	});


 
	

$$("#newsbody img").each(function(a,i){
	ow=a.get('width');
	if(ow>530){
	oh=a.get("height");
	prc=ow/oh;
	a.set("width",530);
	a.set("align","left");
	a.set("height",(530/prc));
	}
});


$$("#newsbody embed").each(function(a,i){
	ow=a.get('width');
	oh=a.get("height");
	prc=ow/oh;
	//alert("PRC "+prc);
	a.set("width",530);
	a.set("height",(530/prc));
});
$$("#newsbody object").each(function(a,i){
	ow=a.get('width');
	oh=a.get("height");
	prc=ow/oh;
	alert("b;aaa");
	a.set("width",530);
	a.set("height",(530/prc));
});

$$("#newsbody img").addEvent('click',function(){
	this.erase('height');
});




//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+'&orderid=' +discount_submit.alt;
//		alert(url);
		var request = new Request({
			url:url,
			method:'post',
			onComplete: function(response) { 
				if(response!='OK'){
					$('off_res').set('text',response);
				}else{
					window.location.reload();
				}
			} 
		}).send();
		return false;

	});
}

    if ($('wishpaycredit'))
	{
        $('wishpaycredit').addEvent('keydown',function(e){

            var charCode = e.which || e.keyCode;
            
            if(charCode == 46)
                return true;

            if (charCode > 31 && (charCode < 48 || charCode > 57))
                return false;

            return true;
            
        });
        
         $('wishpaycredit').addEvent('keyup',function(e){
            var max = $('maxcredit').value;
            
            var v = this.value * 1;
            
            if (v > max)
            {
                this.value = max;
                v = max;
            }   
            var res = $('totaldds').value - v;
                
           res=Math.round(res*Math.pow(10,2))/Math.pow(10,2);
           var str = res + '';
           if(str.substring(str.indexOf(".")).length < 3)
                str = str + "0";
                
            $('total_dds_credit').set("text", str + ' лв.');
        });
    }


/*
	if(document.getElementById("bigimg")){

       zoom=new Zoomer('bigimg',{ 
		    smooth: 10
	    });

		$$('.smallimg').addEvent('mouseenter',function(){
		    
		    //newsrc = this.src.replace("thumbs/", "");
		
			$("bigimg").set("src",this.src);
			
//			hrefsrc=this.src.replace("thumbs/", "")
//			$('bigurl').setProperty('href',hrefsrc);

            $('bigimg').set("big",this.src);
            $('guz').set("src",this.src);
            
		});
	}*/
	
	if($("ricci")){
	
	    zoom=new Zoomer('ricci',{ 
		    smooth: 10
	    });
        
        $$('.smallimg').addEvent('mouseenter',function(){
            $('ricci').set("src",this.src);
            $('ricci').set("big",this.src);
            $('guz').set("src",this.src);
            $('bigurl').set("index",this.get("index"));
        });
        
	}

	
	if($("zoomd")){
	
	    zoom=new Zoomer('zoomd',{ 
		    smooth: 10
	    });
        
        $$('.simg').addEvent('mouseenter',function(){
            $('zoomd').set("src",this.src);
            $('zoomd').set("big",this.src);
            $('dealb').set("src",this.src);
            $('bigurldeal').set("index",this.get("index"));
        });
        
	}
	
	
	//add deal ajax
	/*if($('buydeal')){
	$('buydeal').addEvent('click', function(){
	
		var buydeal = $('deal').get('deal')	
								
		var box=$("resultdeal")
	    box.set('html', '<div align="center"><img src="img/preloader.gif" alt="" /></div>')
		
		//fadeout
		box.get('tween', {property: 'opacity', onComplete : function(){ 
		}})

		var req = new Request({
		url: 'dealbuy.php',
        method:'post',
        data: {
        	buydeal: buydeal
        },

		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();
	});
	}*/
	

	// 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;
		 });
	}


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


	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'));
		
		
		//create instance of the slider, and start it up		
		var mySlider = new SL_Slider({
			slideTimer: 6000,
			orientation: 'none',      //vertical, horizontal, or none: None will create a fading in/out transition.
			fade: true,                    //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();
		
		
		//adding a little animated rollover highlight to the play and prev/next buttons
	//	var origBkgdColor = thePlayBtn.getStyle('background-color');
		var newBkgdColor = "#80301D";
		//var btnArray = new Array(thePlayBtn, thePrevBtn, theNextBtn);
	/*	
		btnArray.each(function(e, i){
			e.set('tween', {duration: 350, transition: 'cubic:out', link: 'cancel'});
			e.addEvents({ 
				'mouseenter' : function() {
					this.tween('background-color', newBkgdColor);
				},
				'mouseleave' : function() {
					this.tween('background-color', origBkgdColor);
				}
			});
		});					 */
	}
	
	
	$$('.subclick').addEvent('click',function(){
		var subel=$("sub"+this.title);
		if(subel.getStyle("display")=="none"){
			subel.setStyle("display","block");
			this.set("html","-");
		}else{
			this.set("html","+");
			subel.setStyle("display","none");
		}
		
		return false;
	});
	
	
		
	$$('.tab').addEvent('mouseover',function(){
			    $$('.tab').each(function(argument, index){
					argument.removeClass('current');
			    });
				this.addClass('current');
				$("showproducts").erase("class");
				$("showproducts").addClass(this.get('href'));
	});
	$$('.tab').addEvent('click',function(){
		return false;
	});
	$$('#anav a').addEvent('mouseover',function(){
		$$('#anav a').each(function(argument, index){
			argument.removeClass('active_hint');
		});
		this.addClass('active_hint');
		$("answers").erase("class");
		$("answers").addClass(this.get('href'));
		return false;
	});

	//brands function
	if ($('brandbtn')){
		var brandul=$('brandul');
		var startheight=brandul.getSize().y;
		brandul.setStyle('height',80);
		brandul.setStyle('overflow','hidden');

//		alert(startheight);
		$('brandbtn').addEvent('click', function(){
			var el=brandul;
			var current=el.getStyle('height');
			if(current=='80px'){
				var myEffect = new Fx.Morph(el, {duration: 'long', transition: Fx.Transitions.Sine.easeOut}); 
				myEffect.start({
					'height': [80, startheight] //Morphs the 'height' style from 10px to 100px.
	//				'width': [900, 300]  //Morphs the 'width' style from 900px to 300px.
				});
			}else{
				var myEffect = new Fx.Morph(el, {duration: 'long', transition: Fx.Transitions.Sine.easeOut}); 
				myEffect.start({
					'height': [startheight,80] //Morphs the 'height' style from 10px to 100px.
	//				'width': [900, 300]  //Morphs the 'width' style from 900px to 300px.
				});
			}
			return false;
			
		});

	}
	//
	
	
	
	var commentsStatus = false;
	var formStatus = false;
	if ($('allcomments'))
	{
		$('allcomments').setStyle('display', 'none');
		$('show_comments').addEvent('click', function(e){
			e.stop();
			if (commentsStatus == false)
			{
				$('allcomments').setStyle('display', 'block');
				$('show_comments').set('html', 'Скрий коментарите');
				commentsStatus = true;
			}
			else
			{
				$('allcomments').setStyle('display', 'none');
				$('show_comments').set('html', 'Покажи всички коментари');
				commentsStatus = false;
			}
		});	
	}



	if ($('comments_form'))
	{
		$('comments_form').setStyle('display', 'none');
		$('add_comment').addEvent('click', function(e){
			e.stop();
			if (formStatus == false)
			{
				$('comments_form').setStyle('display', 'block');
				formStatus = true;
			}
			else
			{
				$('comments_form').setStyle('display', 'none');
				formStatus = false;
			}
		});	
	}



	new SmartHoverBox({
		yOffset: -260, //set your options in here
		xOffset: -10,
		lockY:'bottom',
		lockX:'left'
	}); 


	$$('#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','#cc3333');
		}
	});


	$$('select.tbi').each(function(argument, index){
		var sel=argument.getSelected().get('value');
		argument.removeClass('current');
		if(sel==1){
			$(argument.name+'_info').setStyle('display','block');
		}else{
			$(argument.name+'_info').setStyle('display','none');
		}
	});



	$$('select.tbi').addEvent('change', function(){
		var sel=this.getSelected().get('value');
		if(sel==1){
			$(this.name+'_info').setStyle('display','block');
		}else{
			$(this.name+'_info').setStyle('display','none');
		}
//		alert(sel);
	});


	if(document.getElementById("cbtn")){	
	$('cbtn').addEvent('click',function(){
		$('cform').set("action","/credit.php");
		$('cform').submit();
		//alert("HA");return false;
	});
	}

	//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);
	}
	

});

/**********************************************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);
	}
}



function onlyNumbers(e)
{
    //var e = e || evt; // for trans-browser compatibility
    var charCode = e.which || e.keyCode;
    
    if(charCode == 46)
        return true;

    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

    return true;

}
