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

//en of autocomplete
if(document.getElementById('nav')){
var myMenu = new MenuMatic(
		{
		orientation:'vertical', 
		id:'nav'
		});			
}        
	if(document.getElementById("bigimg")){
		//var bigh=$("bigimg").getSize().y;
		//$("bigimg").setStyle("max-height",bigh);
		$$('.smallimg').addEvent('mouseenter',function(){
			$("bigimg").set("src",this.src);
		});
	}
	///////////////////////////////////////////////////////////
	if(document.getElementById('search')){
	new Autocompleter.Ajax.Json('search', base_url + 's.php', {
		'postVar': 'q'
	});
	} 
	///////////////////////////////////////////////////////////
	
	if(document.getElementById("selecttype")){
      $('selecttype').addEvent('change',function(){
	    var pa=$('notf');
	    var pa2 = $('f');
	    if(this.value == "2"){
	        pa.setStyle('display','table');
	        pa2.setStyle('display','none');
	    }else{
	        pa.setStyle('display','none');
	        pa2.setStyle('display','table');
	    }
	});
	}	
	
	$$('.banlink').addEvent('mouseover',function(){

		var ban="ban"+this.rel;

		var rel=this.rel;

		$$('.banlink').each(function(a,i){

			if(a.rel==rel){

				a.addClass("curr");	

			}else{

				a.removeClass("curr");	

			}

		});

		$$('.slideimg').each(function(a,i){

			if(a.id!=ban){

				a.setStyle("display","none");

			}

		});

		if($(ban).getStyle("display")=="none"){

			$(ban).setStyle("opacity",0);

			$(ban).fade('in');

			$(ban).setStyle("display","block");

		}

		

	});	
	
	if(document.getElementById('checkadr')){
	$('checkadr').addEvent('change',function(){
	    var pa=$('payment_address');
	    if(pa.getStyle('display')=="none"){
	    pa.setStyle('display','table');
	    }else{
	    pa.setStyle('display','none');
	        
	    }
	});
	}	
	
	$$('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;
	});
	}
	$$('.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 (document.getElementById('brandbtn')){
		var brandul=$('brandul');
		var startheight=brandul.getSize().y;
		brandul.setStyle('height',550);
		brandul.setStyle('overflow','hidden');

//		alert(startheight);
		$('brandbtn').addEvent('click', function(){
			var el=brandul;
			var current=el.getStyle('height');
			if(current=='550px'){
				var myEffect = new Fx.Morph(el, {duration: 'long', transition: Fx.Transitions.Sine.easeOut}); 
				myEffect.start({
					'height': [550, 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,550] //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 (document.getElementById('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 (document.getElementById('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('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','#2a9fd6');



		}



	



	});

/*

doRating(2, true, { 
    // use some default value you have read from the DB that represents your start / current rating
    targetObject: $("durability"),
    maxStars: 5,
    tipShown: true,
    background: '#010',
    colourBase: '#335200',
    colourTarget: '#00cc00',
    starWidth: 22,
    border: 0,
    tipPadding: 0,
    starSpacing: 1,
    starHeight: 10,
    readOnly: true,
    imageURL: base_url + 'images/stereo.gif'
    clickEvent: function(ratingOptions) { // save the vote
        new Request({
            url: "ratinger.php",
            method: "get",
            onComplete: function() {
                alert("saved via ajax!");
            }
        }).send("a=vote&ratingID="+ratingOptions.ratingID+"&rating="+ratingOptions.newRating);
    } 
});
	*/

});

function show_rating(id, start_value,read_only) {
    if(! read_only) read_only = false;
        doRating(start_value, true, {
        // use some default value you have read from the DB that represents your start / current rating
        targetObject: $(id),
        maxStars: 5,
        tipShown: true,
        background: '#EEE', 
     //   colourBase: '#335200',
      //  colourTarget: '#00cc00',
        starWidth: 22,
        border: 0,
        tipPadding: 0,
        starSpacing: 1,
        readOnly: read_only,
        starHeight: 10,
        imageURL: base_url + 'images/stereo.gif',
        clickEvent: function(ratingOptions) { // save the vote
          /*  new Request({
                url: "ratinger.php",
                method: "get",
                onComplete: function() {
                    alert("saved via ajax!");
                }
            }).send("a=vote&ratingID="+ratingOptions.ratingID+"&rating="+ratingOptions.newRating);
            */
          var input_id = id + "_input";
          $(input_id).set('value',ratingOptions.newRating);
        }
    });

}


function review_vote(id,vote,current_value) {
    new Request({
      method: 'post',
      url: base_url + "ajax/review_votes.php",
      data: { 'review_id' : id, 'vote': vote },
      onRequest: function() {},
      onComplete: function() {
          if(vote==1) {
                $('rev_voteup_'+id).addClass("up_clicked");
                $('up_number'+id).set('html', "+"+(current_value+1));
          }

          if(vote==2) {
                $('rev_votedown_'+id).addClass("down_clicked");
                $('down_number'+id).set('html', "-"+(current_value+1));
          }
      }
    }).send();
}


function login_register_popup() {
 
 new Request.HTML({
        method: 'get',
        data: {redirect: document.URL + "#reviews"},
        url: base_url + 'ajax/login_reg_form.php',
        onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
            modal = new FbModal({
                'parentEl': 'content',
                'title': 'Вход / Регистрация',
                'subTitle': 'Вход в Parfium.bg',
                'content': responseHTML,
               // 'content': "<div>aaaaa</div>",
                'height' : 380,
                'width' : 720
            });

           //validation of the form
           var fields = {
                email:          'Required Email',
                password:       'Required'
            };

            var val = new validate('login_form', fields, {
                useAjaxSubmit:false,
                AjaxSubmitOptions: {
                    evalScripts: false,

                    onComplete: function() {
                         $('login_form').submit();
                    }
                }
            });

            var fields2 = {
                name:          'Required',
                familyName:     'Required',
                reg_email:          'Required Email',
                reg_password:       'Required',
                reg_repassword: "Required"
            };

            var val_reg = new validate('register_form', fields2, {
                useAjaxSubmit:false,
                AjaxSubmitOptions: {
                    evalScripts: false,

                    onComplete: function() {
                         $('register_form').submit();
                    }
                } 
            });

        }
        


     }).send();
 
   


}

function submitLogin() {
  

}

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