var site = 'http://www.horecaexpert.ro';

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

function set_search(frm){
  	if (typeof frm == "object" && frm != null){
          	if (frm.elements["search_key"]){
                  	if (frm.elements["search_key"].value != '' && frm.elements["search_key"].value != 'Cuvant cautare'){
                          	var val = frm.elements["search_key"].value;
                          	val = val.replace(/-/g,"");
                          	val = val.replace(/\//g,"");
                          	window.location.href = site+'/produse/Cautare/'+val.trim().replace(/[^a-z|0-9]/gi,"-")+'.html';
                   	}
                }
   	}
}

var prod_imgs = new Array();
var first_img_load = true;
var img_ind = 0;
var img_array = new Array();
var prod_video = new Object;
var static_video_width = 480;
var static_video_height = 360;


function show_picture_galery(pic_ind){
  	if (first_img_load == true){
          	first_img_load = false;
		for (var i=0; i<img_array.length; i++){
                	var img = new Image();
                	img.src = img_array[i].src;

                  	prod_imgs.push({el:img,width:img_array[i].width,height:img_array[i].height});
  		}
   	}
   	if (!isNaN(pic_ind) && pic_ind>=0 && prod_imgs[img_ind]) img_ind = pic_ind;
   	put_picture();
}

function put_picture(){
   	if (prod_imgs.length){
        	var psize = getPageSize();
        	var pscroll = getPageScroll();
          	var a;
          	if (a = document.getElementById("litebox")){
                  	if (a.style.display == "none"){
        			show_litebox(undefined, 0);
        		}
                  	var b;
                  	if (b = document.getElementById("prod_img_litebox_container")){
                          	var img;
                          	var img_width = 0;
                          	var img_height = 0;
                          	if (img = document.getElementById("prod_img_litebox")){
                                  	img.src = prod_imgs[img_ind].el.src;
                                  	img_width = parseInt(prod_imgs[img_ind].width);
                                  	img.style.width = img_width+"px";
                                  	img_height = parseInt(prod_imgs[img_ind].height);
                                  	img.style.height = img_height+"px";

                           	}
                           	a.appendChild(b);
                           	put_galery_buttons(img_ind, img_array, "prev_pic", "next_pic");
                           	b.style.display = "";

                        	a.style.width = img_width+20 + "px";

                        	a.style.height = "0px";
                       		a.style.height = "auto";
                        	a.style.left = (psize[0]-img_width)/2+"px";
                        	a.style.right = a.style.left;
                        	a.style.top = (pscroll[1]+100)+"px"
                                a.style.height = (img_height-100)+"px";
                                if (document.all){
        				document.getElementById("litebox").style.width = img.width+20+"px";
				}
				size_litebox();
                   	}
         	}
   	}
}

function put_galery_buttons(ind, arr, but_prev_id, but_next_id){
  	var a;
  	if (a = document.getElementById(but_prev_id)){
  		if (ind == 0){
                  	a.style.visibility = "hidden";
                  	document.getElementById(but_prev_id+"_bottom").style.visibility = "hidden";
           	}else{
                  	a.style.visibility = "visible";
                  	document.getElementById(but_prev_id+"_bottom").style.visibility = "visible";
            	}
   	}
   	if (a = document.getElementById(but_next_id)){
	   	if (ind == arr.length -1){
                  	a.style.visibility = "hidden";
                  	document.getElementById(but_next_id+"_bottom").style.visibility = "hidden";
           	}else{
                  	a.style.visibility = "visible";
                  	document.getElementById(but_next_id+"_bottom").style.visibility = "visible";
            	}
    	}
}

function next_picture(){
  	img_ind++;
  	put_picture();
}

function previous_picture(){
  	img_ind--;
  	put_picture();
}

function hide_picture_galery(){
  	var a;
  	if (a = document.getElementById("prod_img_litebox_container")){
          	a.style.display = "none";
          	document.body.appendChild(a);
          	hide_litebox();
   	}
}

function show_prod_video(movie, preview){
    	var psize = getPageSize();
    	var pscroll = getPageScroll();
      	var a;
      	if (a = document.getElementById("litebox")){
              	if (a.style.display == "none"){
    			show_litebox(undefined, 0);
    		}
              	var b;
              	if (b = document.getElementById("prod_video_litebox_container")){
        		var FO = {
                		movie:site+"/flvplayer.swf",width:static_video_width,height:static_video_height,majorversion:"7",build:"0",
				bgcolor:"#FFFFFF",allowfullscreen:"true",
                		flashvars:"file="+movie+"&image="+preview
			};
        		UFO.create(FO, "prod_video");

                       	a.appendChild(b);

                	a.style.width = static_video_width+20 + "px";

                	a.style.height = "0px";
               		a.style.height = "auto";
                	a.style.left = (psize[0]-static_video_width)/2+"px";
                	a.style.right = a.style.left;
                	a.style.top = (pscroll[1]+100)+"px"
                        a.style.height = static_video_height+40+"px";
                        
                        b.style.display = "";

			size_litebox();
               	}
     	}
}

function hide_prod_video(){
  	var a;
  	if (a = document.getElementById("prod_video_litebox_container")){
          	a.style.display = "none";
          	document.body.appendChild(a);
          	hide_litebox();
   	}
}

function put_product_interested(id_prod){
    	var psize = getPageSize();
    	var pscroll = getPageScroll();
    	var static_ifr_width = 800;
      	var a;
      	if (a = document.getElementById("litebox")){
              	if (a.style.display == "none"){
    			show_litebox(undefined, 0);
    		}
    		var b;
    		var frm = window.frames[0];
    		if (b = document.getElementById("prod_list_iframe")){
                        frm.location.replace(site+"/lib/cart.php?action=add_prod&id_prod="+id_prod+"&rndval="+Math.random()*1000);
                        b.src=site+"/lib/cart.php?action=add_prod&id_prod="+id_prod+"&rndval="+Math.random()*1000;
                        b.frm = frm;
                        a.appendChild(b);

                	a.style.height = '0px';
               		a.style.height = '100px';
                	a.style.left = Math.round((psize[0]-static_ifr_width)/2)+'px';
                	a.style.top = (pscroll[1]+100)+'px';

                        b.style.display = "";

                        if (document.all){
                          	setTimeout('var psize = getPageSize();'+
				  		'document.getElementById("litebox").style.left = Math.round((psize[0]-800)/2)+\'px\'',100);
                        }
      		}
   	}
}

function hide_product_interested(){
  	var a;
  	if (a = document.getElementById("prod_list_iframe")){
          	a.style.display = "none";
          	a.src = "";
          	document.body.appendChild(a);
   	}
   	hide_litebox();
}

function fir_iframe(){
  	if (window.parent != window){
          	var w = window.parent;
          	w.document.getElementById("prod_list_iframe").style.height = "0px";
        	var psize = getPageSize();
        	w.document.getElementById("litebox").style.height = psize[1]+"px";
        	w.document.getElementById("prod_list_iframe").style.height = psize[1]+"px";
  	}
}

function check_add_news(std){
	var a;
	if (a = document.getElementById('abonare_mail')){
		return ((a.value.trim() != '') && (a.value != std));
	}
	return false;
}

function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}
window.onload = externalLinks;
