var continut_oferta = Array();

var variante = Array();
var varianta_selectata = 0;
var varianta_selectata_text='';

var marimi = Array();
var marime_selectata = 0;
var marime_selectata_text='';

var modele = Array();
var model_selectat = 0;
var model_selectat_text='';

var text_cos_sus;

var poza_mare_temp;

function preview_image(old_size,new_size,id){

	var oldsrc,newsrc;
	if(id)
		oldsrc = document.getElementById(id).src;
	else
		oldsrc = document.getElementById("imagine_mare").src;
	if (oldsrc.indexOf('no_image')>0){

		return false;

	}
	else{

		newsrc = oldsrc.replace("_"+old_size+".jpg","_"+new_size+".jpg");


	window.open(sitepath+'Preview/?img='+newsrc,'zoom','scrollbars=yes,height=100,width=200,status=no,toolbar=no,menubar=no,location=no');


	}

}

function make_oferta(id){

	if (id){
		document.getElementById('oferta').innerHTML=continut_oferta[id];
	}

}

function change_image(old_size,new_size,obj){
	var oldsrc;

	oldsrc = obj.src;

	if (oldsrc.indexOf('no_image')>0){

		return false;

	}
	else{

		newsrc = oldsrc.replace("_"+old_size+".jpg","_"+new_size+".jpg");

		document.getElementById("imagine_mare").src = newsrc;
	}
}

function select_varianta(varianta){


	if(varianta_selectata!=varianta){


		if(document.getElementById('small_image_'+varianta))
		change_image(1,3,document.getElementById('small_image_'+varianta));

		if(varianta_selectata!=0){
			if(document.getElementById('div_'+varianta_selectata))
			document.getElementById('div_'+varianta_selectata).className='small_thumbnail';
		}
		varianta_selectata = varianta;
		varianta_selectata_text = variante[varianta];

		if(document.getElementById('div_'+varianta))
		document.getElementById('div_'+varianta).className='small_thumbnail_selected';

	}
		//alert(varianta_selectata_text);
}


function select_marime(marime){

	if(marime_selectata!=marime){

		if(document.getElementById('marime_'+marime))
		document.getElementById('marime_'+marime).checked=true;

		marime_selectata = marime;
		marime_selectata_text = marimi[marime];

	}
	//alert(marime_selectata_text);
}















var IE = (document.all) ? 1 : 0;
function caracter(ch){
	if ( (ch>=65 && ch <=90) || (ch>=97 && ch<=122) || (ch == 32) ||(ch == 39) ||(ch == 45) ||(ch == 0)||(ch == 8)||(ch == 222))
		return true;

	return false;
}

function cifra(ch){
	if ((ch>=48 && ch <=57) ||(ch == 0)||(ch == 8))
		return true;
	return false;
}

function sector(ch){
	if ((ch>=49 && ch <=54) ||(ch == 0)||(ch == 8))
		return true;
	return false;
}

function validate(name,e){
if (IE) {e=window.event;
var ch=e.keyCode;
}
else
{
ch=e.which;
}

if (name=='numai_alfanumeric' && !(caracter(ch) || cifra(ch)))
	return false;

if (name=='numai_caractere' && !caracter(ch))
	return false;


if (name=='numai_cifre' && !cifra(ch))
	return false;



if (name=='numai_sector' && !sector(ch))
	return false;

	return true;
}


function make_filter(obj,disc){

	var url;

	show_loading();

	if(obj)
		url = sitepath+"rezultat-filtre/?category="+$('url_categorie').value+"&subcategory="+$('url_subcategorie').value+"&filter="+obj.name+"&value="+obj.value+"&disc="+disc+"&add="+obj.checked;
	else
		url = sitepath+"rezultat-filtre/?category="+$('url_categorie').value+"&subcategory="+$('url_subcategorie').value+"&disc="+disc;

	new Ajax.Updater("product_holder", url,{evalScripts: true});

}


function sterge_filtru(filter,disc){

	var url;

	show_loading();


	url = sitepath+"rezultat-filtre/?category="+$('url_categorie').value+"&subcategory="+$('url_subcategorie').value+"&filter="+filter+"&delete=true&disc="+disc;

	var i=0;

	elements = document.getElementsByTagName('input');

	for(i=0;i<elements.length;i++){
		if(elements[i].type=="checkbox" && elements[i].parentNode.parentNode.parentNode.id == "filter_expand_"+filter){
			elements[i].checked=0;
		}
	}



	new Ajax.Updater("product_holder", url,{evalScripts: true});

}



function change_page(nr,disc){

		show_loading();

		new Ajax.Updater("product_holder", sitepath+"rezultat-filtre/?category="+$('url_categorie').value+"&subcategory="+$('url_subcategorie').value+"&disc="+disc+"&pag="+nr,{evalScripts: true});
}

function show_loading(){


	if($('product_holder')){
		$('products_loading').style.height=$('product_holder').offsetHeight + 'px';
		//$('products_loading').style.top=$('product_holder').offsetTop + 'px';
		//$('products_loading').style.left=$('product_holder').offsetLeft + 'px';
	}
	$('products_loading').style.display='block';
}

function hide_loading(){
	$('products_loading').style.display='none';
}


function update_cantitate(product,cantitate){

		//show_loading();

		new Ajax.Updater("page_holder", sitepath+"cos-ajax/?modifica&id="+product+"&cantitate="+cantitate,{evalScripts: true});

}

function sterge_produs(product){

		//show_loading();

		new Ajax.Updater("page_holder", sitepath+"cos-ajax/?modifica&id="+product+"&cantitate=0",{evalScripts: true});

}

function refresh_cos(){

		new Ajax.Updater("page_holder", sitepath+"cos-ajax/",{evalScripts: true});

}

function make_tip_entitate(){
    var form = $('form_comanda').elements;

    if(form['tip_entitate'][0].checked){
        $('persoana_juridica').style.display='none';
        $('persoana_fizica').style.display='block';
    }

    else{
        $('persoana_juridica').style.display='block';
        $('persoana_fizica').style.display='none';
    }


}


var clicked = new Array();

function star_over(unique,obj){
    var i;
    var current = obj.id.replace(unique+'_star_','');

    for (i=1;i<=5;i++){

        if (i<current)
            $(unique + '_star_' +i).src=sitepath+'images/stars/star_1.gif';

        if (i==current)
            $(unique + '_star_' +i).src=sitepath+'images/stars/star_1_selected.gif';

        if (i>current)
            $(unique + '_star_' +i).src=sitepath+'images/stars/star_0.gif';

    }
}

function star_out(unique,obj,mark){

    var i;

    var current;

    if (clicked[unique])
        current = clicked[unique];
    else
        current = mark;



    for (i=1;i<=5;i++){

        if (i<=current)
            $(unique + '_star_' +i).src=sitepath+'images/stars/star_1.gif';

        if (i>current)
            $(unique + '_star_' +i).src=sitepath+'images/stars/star_0.gif';

    }

}


function star_click(unique,obj){

    var current = obj.id.replace(unique+'_star_','');

    clicked[unique] = current;

    $(unique+'_value').value=current;

}

 function make_link(url)
{
    wind = window.open();
    wind.document.location.href='http://'+url.replace("|",".").replace("|",".").replace("|",".").replace("|",".").replace("|",".").replace("|",".").replace("|",".").replace("|",".");
}

function open_prezentare(){
    if($('descriere_marca').offsetHeight<100)
        $('descriere_marca').style.height='auto';

    $('link_mai_multe').style.display='none';

    return false;
}

function open_prezentare2(j){
    if(!j) j=='';
    if($('descriere_marca'+j).offsetHeight<100)
        $('descriere_marca'+j).style.height='auto';

    $('link_mai_multe'+j).style.display='none';

    return false;
}

function addCompare(productId,obj){

	new Ajax.Request(sitepath, {
  		method: 'post',
  		onSuccess: function(transport) {
  			
    	},
    	parameters: { 
    		add_compare:'true',
    		product_id: productId,
    		add:obj.checked 
    	}
	});
}

function addToCompare(productId,categoryId,obj){

	new Ajax.Request(sitepath, {
  		method: 'post',
  		onSuccess: function(transport) {
  			
  			response = transport.headerJSON;
  			var anchors = document.getElementsByClassName('compare_link');
  			var anchor;
  			var currentId;
  			var anchorIds  = Array();
  			
  			//alert(response.anchorIds);
  			
			for (var i=0;i<anchors.length;i++){
				anchor = anchors[i];
				
				currentId = anchor.id.replace('productId_','');
				
				//anchor.innerHTML = response.anchorText;
				
				
//				if(response.anchorUrl[response.anchorUrl.length-1] == '=')
//					anchor.href = response.anchorUrl + anchor.id.replace('productId_','');
//				else
				if(response.anchorIds)
					anchorIds = response.anchorIds.clone();
				else
					anchorIds  = Array();
				
				anchorIds.push(currentId);
				anchorIds = anchorIds.uniq()
				//alert(anchorIds.inspect());
			
				anchor.href = response.anchorUrl.replace('&amp;','&') + anchorIds;
					
			}

    	},
    	parameters: { 
    		add_to_compare:'true',
    		product_id: productId,
    		category_id: categoryId,
    		add:obj.checked 
    	}
	});
}


function compareThisProduct(){

	alert(this);

}

function getCompareText(category){
	var text;
	new Ajax.Request(sitepath, {
  		method: 'post',
  		asynchronous: false,
  		onSuccess: function(transport) {
  			text = transport.responseText;
    	},
    	parameters: { 
    		check_compare:'true',
    		category: category
    	
    	}
	});	
	return text;

}



function filterProducts(){

	var produse = document.getElementsByClassName('product_check');
	var deScos = new Array();
	
	
	for (i=0;i<produse.length;i++){
		
		if(produse[i].checked)
			deScos += (produse[i].id.replace('product_',''))+',';
		
	}
	
	
	
	new Ajax.Request(sitepath, {
  		method: 'post',
  		onSuccess: function(transport) {
  			document.location.href='.';
    	},
    	parameters: { 
    		delete_compare:'true',
    		de_scos: deScos
    	
    	}
	});


}

var autocompleter;

function submitSearch() {
	autocompleter.hide();
   $('topSearchForm').submit();
}



function displayCategories(){

	
	if($('allCategoriesDiv') && $('allCategoriesDiv').innerHTML.length){
		//$('allCategoriesDiv').style.display = 'block';
		return true;
	}
	
	
	
	
}


function productsTabsSelectTab(liObject){

	var ulObject = liObject.parentNode;
	var otherLis = ulObject.childNodes;
	var otherLi;
	
	for(var i=0;i<otherLis.length;i++){
		otherLi = otherLis[i];
		if(otherLi.id==liObject.id){
			otherLi.className = 'selected';
			$(otherLi.id.replace('productTabButton_','productTabContent_')).style.display = 'block';
		}
		else{
			otherLi.className = '';
			$(otherLi.id.replace('productTabButton_','productTabContent_')).style.display = 'none';
		}
		
	}

}


function showActiveTab(){

	var segment = document.location.hash.replace('#','');
	if($('productTabButton_' + segment))
		productsTabsSelectTab($('productTabButton_' + segment));

}

function addHoverClass(objectName,hideSelects){
	isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
	var selects,i;
	if(isIE6 && $(objectName)){
		$(objectName).onmouseenter=function(){ $(objectName).addClassName('hover'); 
		
			if(hideSelects){
				selects = document.getElementsByTagName('select');
				for(i=0;i<selects.length;i++){
					select = selects[i];
					select.style.visibility = 'hidden';
				}
			}
		};
		$(objectName).onmouseleave=function(){ $(objectName).removeClassName('hover');
			
			if(hideSelects){
				selects = document.getElementsByTagName('select');
				for(i=0;i<selects.length;i++){
					select = selects[i];
					select.style.visibility = 'visible';
				}
			}
		
		};
	}
}

var maxGalleryPages = Array();
var galleryPages = Array();


function galleryGoRight(numericId){
	
	if(galleryPages[numericId] != maxGalleryPages[numericId] -1)
		galleryPages[numericId]++;
	else
		galleryPages[numericId] = 0;
		
	galleryGotoPage(numericId,galleryPages[numericId]);
	
}

function galleryGoLeft(numericId){
	
	if(galleryPages[numericId] != 0)
		galleryPages[numericId]--;
	else
		galleryPages[numericId] = maxGalleryPages[numericId] -1;
		
	galleryGotoPage(numericId,galleryPages[numericId]);
	
}



function galleryGotoPage(numericId,page){
	
	var offset = -660 * page * 1;
	
	for(var i=0; i < maxGalleryPages[numericId];i++){
			if(i==page)
			$('galleryDisplayerPage'+numericId+'_'+i).addClassName('active');
		else
			$('galleryDisplayerPage'+numericId+'_'+i).removeClassName('active');
	
	}
	
	new Effect.Move($('galleryDisplayer'+numericId), {duration:0.3, x: offset, y: 0, mode: 'absolute'});


}


function getBlogZoneContent(categoryId){

	new Ajax.Updater("blogZoneContent",sitepath + 'blog-zone',{parameters: {categoryId: categoryId},evalScripts: true});
	
}


function scrollMenuBox(){

	if(!$('scrollMenuBox'))
		return;
	var scrollOffsets = document.viewport.getScrollOffsets();
	
	if(scrollOffsets.top  > 150){
		$('scrollMenuBox').style.position='absolute';
		$('scrollMenuBox').style.top = scrollOffsets.top + 10 + 'px';
	}
	else{
		$('scrollMenuBox').style.position='static';
		$('scrollMenuBox').style.top = 'auto';
	}

}