document.observe('dom:loaded', function() { 
	
    $$('.toggle').each(function(elm) {
        elm.observe('focus', function() {
            if (elm.value == elm.title) {
                elm.value = '';
            }
        }).observe('blur', function() {
            if (elm.value == '') {
                elm.value = elm.title;
            }
        });
    });
    
    $$('a[rel="external"]').each(function(link){
		if(link.readAttribute('href') != '' && link.readAttribute('href') != '#'){
				link.writeAttribute('target','_blank');
        }
    });	
    
    $$('ul.primary li a.trigger', 'ul.secondary li a.trigger').each(function(elm) {
		elm.observe('click', function(event) {
			if(elm.up('li').hasClassName('submenu')) {
				event.stop();
				var visible = $$('li.submenu ul').find(function (elm) { return elm.visible(); });
				var next	= elm.next();

				if (visible) {
					if (visible != next) {
						visible.up('li').removeClassName('open');
						elm.up('li').addClassName('open');
						visible.blindUp({ duration: 0.2 });
						next.blindDown({ duration: 0.2 });
                    } else {
                        visible.up('li').removeClassName('open');
                        visible.blindUp({ duration: 0.2 });
                    }
                } else {
                    elm.up('li').addClassName('open');
                    Effect.toggle(next, 'blind', { duration: 0.2 });
                }
            }
        });
	});
     
	$$('li.off').each(function (elm) {
		var subMenu = elm.down('div');
		
		elm.observe('mouseover', function (e) {
            var target = e ? e.relatedTarget : e.fromElement;
            if (target) {
                if (target == this || target.descendantOf(this)) {
                    return;
                }
            }
            elm.removeClassName('off').addClassName('on');
            subMenu.show();
            if(!elm.hasClassName('mini-cart')) {
            	$$('form.browse-filters').invoke('hide');	
            }
		}).observe('mouseout', function (e) {
            var target = e ? e.relatedTarget : e.toElement;
            if (target) {
                if (target == this || target.descendantOf(this)) {
                    return;
                }
            }
            elm.removeClassName('on').addClassName('off');
            subMenu.hide();
            $$('form.browse-filters').invoke('show');
		});
	});

    
    function matchColumns() {
    	var columns = $A($$('.browse-grid ol li'));
    	var column_height = 0;
    	var max_height = 0;
    	columns.each( function(column) {
    		column_height = column.getHeight();
    		max_height = ( column_height > max_height ) ? column_height : max_height;
    	});
    	columns.each(function(column) {
    		column.setStyle({
    			height: (max_height + 17) + 'px'
    		});
    	});
    }
    
    Event.observe(window, 'load', matchColumns);
	
	
});

function type_pers(tip) {
	if( tip == 'pf' ) { 
		$('pf').show();
		$('pj').hide();

		document.getElementById('pers_fizica').checked = 'checked';
		document.getElementById('pers_juridica').checked = '';
	} else if( tip=='pj' ) {
		$('pf').hide();
		$('pj').show();
		
		document.getElementById('pers_fizica').checked = '';
		document.getElementById('pers_juridica').checked = 'checked';
	}
}

function type_adress(tip){
	if(tip == 'pf'){
		if(document.getElementById('pf_new_adress').checked == false){
			$('livrare-new_adress').show();
			$('pf_text-new').replace('<em id="pf_text-new">Bifati acest camp in cazul in care adresa la care vor fi livrate produsele coincide cu adresa de facturare.</em>');
		}else{
			$('livrare-new_adress').hide();
			$('pf_text-new').replace('<em id="pf_text-new">Debifati acest camp in cazul in care adresa la care vor fi livrate produsele nu coincide cu adresa de facturare.</em>');
		}
	}else if(tip == 'pj'){
		if(document.getElementById('pj_new_adress').checked == false){
			$('livrare-new_adress').show();
			$('pj_text-new').replace('<em id="pj_text-new">Bifati acest camp in cazul in care adresa la care vor fi livrate produsele coincide cu adresa de facturare.</em>');
		}else{
			$('livrare-new_adress').hide();
			$('pj_text-new').replace('<em id="pj_text-new">Debifati acest camp in cazul in care adresa la care vor fi livrate produsele nu coincide cu adresa de facturare.</em>');
		}
	}
}

//function add_to_cart(id,pret,qty){
//	if(id != '' && pret !=''){
//		cant = $F('qty_'+id);
//		
//		new Ajax.Request(', {
//			  method: 'post',
//			  evalJSON: true,
//			  parameters: {'id':id,'pret':pret,'qty':cant},
//			  onSuccess: function(data){
//				 var json = data.responseText.evalJSON();
//				 new  Dialog({
//				        handle:'a.handle',
//				        title:'Dialog #2',
//				        content:'Some content (#2)'
//				});
//				 reload_minicart();
//			  }
//		});
//
//	}
//}

function reload_forum(id_produs){
	
}

function reload_cart(){
	$('reload-cart').insert({top:'<span class="loader-big"></span>'});
	new Ajax.Updater('reload-cart', APP.path.root+'comenzi/cart/reload_cart', {});
}

function reload_minicart(){
	$('number-products').replace('<span class="loader-min"></span>');
	new Ajax.Request(APP.path.root+'comenzi/cart/reload_mini_cart',{
		onSuccess: function(data){
			$('reload-mini-cart').replace(data.responseText);
		}
	});
	
}

function delete_by_licenta(id,id_produs){
	if(id != ''){
		new Ajax.Request(APP.path.root+'comenzi/cart/delete_by_licenta', {
			  method: 'post',
			  evalJSON: true,
			  parameters: {'id':id,'id_produs':id_produs},
			  onSuccess: function(data){
				  reload_cart();
				  reload_minicart();
			  }
		});	
	}
}

function delete_by_produs(id){
	if(id != ''){
		new Ajax.Request(APP.path.root+'comenzi/cart/delete_by_produs', {
			  method: 'post',
			  evalJSON: true,
			  parameters: {'id':id},
			  onSuccess: function(data){
				  reload_cart();
				  reload_minicart();
			  }
		});	
	}
}

function update_cart(id_form){
	new Ajax.Request(APP.path.root+'comenzi/cart/update', {
		  method: 'post',
		  evalJSON: true,
		  parameters: $(id_form).serialize(),
		  onSuccess: function(data){
		 	reload_cart();	 
			reload_minicart();
			
			var json = data.responseText.evalJSON();
		 				
		 	if(json.msg != ''){
		 		alert(json.msg);
		 	}
		 			 	
			
		  }
	});	
}

function toTop() {
    self.scrollTo(0, 0)
}

function sort_price(value,tip,id_produs,nume_produs,id_tab,tm){
	if(value != ''){
		new Ajax.Request(APP.path.root+'produse/sort_licente',{
			method: 'post',
			parameters: {'produs':nume_produs,'id_produs':id_produs,'id_tab':id_tab,'sort':value,'tip':tip,'tm':tm},
			onSuccess: function (data){
				$('licente-reload-'+id_tab+'-'+tm).replace(data.responseText);
			}
		});
	}
}

function sort_grup(value,tip,id_produs,nume_produs,id_tab,tm){
	if(value != ''){
		new Ajax.Request(APP.path.root+'produse/sort_licente',{
			method: 'post',
			parameters: {'produs':nume_produs,'id_produs':id_produs,'id_tab':id_tab,'grup':value,'tip':tip,'tm':tm},
			onSuccess: function (data){
				$('licente-reload-'+id_tab+'-'+tm).replace(data.responseText);
			}
		});
	}
}



