function mouseOver(e) {
    if (e && e.className.indexOf('-over') < 0) {
		e.oldClassName=e.className;
		e.className+='-over';
	}
}
function mouseOut(e) {
    if (e && e.oldClassName) e.className=e.oldClassName;
}

var mouseClick_CLICKED = false;

function mouseClick(e, event) {
	var i,a;
	if (mouseClick_CLICKED) return;
	mouseClick_CLICKED = true;
	setTimeout('mouseClick_CLICKED = false;', 300);
	a = getChild(e,'a');
	if (a) {
		document.location = a.getAttribute('href');
		return;
	}
	
	/*if (e && e.hasChildNodes) {
		for (i=0; i<e.childNodes.length; i++) {
			if (e.childNodes[i].tagName == 'A') {
				document.location = e.childNodes[i].getAttribute('href');
				return;
			}
		}
	}*/
}

function mouseOverImg(e) {
	var img = getChild(e,'img');
	if (img) {
		var dot = img.src.lastIndexOf('.');
		var src_ext = img.src.substring(dot);
		var src_name = img.src.substr(0,dot);
		if(src_name.substring(src_name.length-5) != '-over') {
			img.oldSrc = img.src;
			img.src = src_name+"-over"+src_ext;
		}
	}
}

function mouseOutImg(e) {
	var img = getChild(e,'img');
	if (img && img.oldSrc) img.src = img.oldSrc;
}

function mouseOverSelfImg(e) {
	if (e) {
		var dot = e.src.lastIndexOf('.');
		var src_ext = e.src.substring(dot);
		var src_name = e.src.substr(0,dot);
		if(src_name.substring(src_name.length-5) != '-over') {
			e.oldSrc = e.src;
			e.src = src_name+"-over"+src_ext;
		}
	}
}

function mouseOutSelfImg(e) {
	if (e && e.oldSrc) e.src = e.oldSrc;
}


function getChild(e,tag) {
	return (e.getElementsByTagName(tag).length > 0) ? e.getElementsByTagName(tag)[0] : false;
}

function getAncestorByName(element, ancestorName) {
	if (element.parentNode.tagName) {
		return (element.parentNode.nodeName == ancestorName) ? element.parentNode : getAncestorByName(element.parentNode, ancestorName);
	}
}

function setAttribute(obj,attrName,attrValue) {
	if (!obj) return;
	if (document.all) obj[attrName] = attrValue;
	obj.setAttribute(attrName,attrValue);
}

function getAlpha(obj) {
	if (!obj) return -1;
		var browser = navigator.appName;
	if (browser == "Microsoft Internet Explorer") {
		var re=/(alpha)(.{1})(opacity=)(\d{1,})(.{1})/;
		var result = re.exec(obj.style.filter);
		return (result != null)  ? parseFloat(result[4]) : 100;
	} else {
		if (obj.style.MozOpacity != undefined) return (obj.style.opacity == '') ? 100 : parseFloat(obj.style.opacity)*100;
		if (obj.style.MozOpacity != undefined) return (obj.style.MozOpacity == '') ? 100 : parseFloat(obj.style.MozOpacity)*100;
		if (obj.style.KhtmlOpacity != undefined) return (obj.style.KhtmlOpacity == '') ? 100 : parseFloat(obj.style.KhtmlOpacity)*100;
	}
	return -1;
}
 
function setAlpha(obj,alpha) {
	if (!obj) return;
	var browser = navigator.appName;
	if (browser == "Microsoft Internet Explorer") {
		obj.style.filter = "alpha(opacity="+alpha+")";
	} else {
		obj.style.opacity = alpha*0.01;
		obj.style.MozOpacity = alpha*0.01;
		obj.style.KhtmlOpacity = alpha*0.01;
	}
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function language(lang) {
  langRE = /\/bg\/|\/en\/|\\bg\\|\\en\\/;
  document.location.replace(document.location.href.replace(langRE,'/'+lang+'/'));
}

function nav_toggle(id, prefix){
	var obj;
	var img = MM_findObj('img_'+id);
	if (obj = MM_findObj('nav_'+id)) {
		if (obj.style) {
			if (obj.style.visibility == 'visible') {
				obj.style.visibility = 'hidden';
				obj.style.display = 'none';
				if (img) img.src = prefix+'images/arrow-right.gif';
			} else {
				obj.style.visibility = 'visible';
				obj.style.display = 'block';
				if (img) img.src = prefix+'images/arrow-down.gif';
			}
		}
	}
}

function toggleLayersClass(layerID,haystack,class1,class2) {
	var layers = layerID.split(',');
	var i,obj;
	for (i=0; i<haystack.length; i++) {
		obj = document.getElementById(haystack[i]);
		if (obj) obj.className = (in_array(obj.id,layers)) ? class1 : class2;
	}
}

function toggleLayersStyle(layerID,haystack,styleName,style1,style2) {
	var i,obj,styleValue;
	for (i=0; i<haystack.length; i++) {
		obj = document.getElementById(haystack[i]);
		if (obj) {
			styleValue = (obj.id == layerID) ? style1 : style2;
			if (styleName == 'alpha') {
				setAlpha(obj,styleValue);
			}
			else obj.style[styleName] = styleValue;
		}
	}
}

function changeTab(obj) {
	if (!obj) return;
	toggleLayersClass(obj.id,tabs,"product-header-over","product-header");
	toggleLayersClass(obj.id.split('tab_')[1],descriptions,"visible","hidden");
}

/* Gallery */
function galleryScroll(step) {
	var tmbContainer = document.getElementById('thumbnails');
	var tmbContainerWidth = parseInt(tmbContainer.style.width,10);
	var thumbnails = tmbContainer.getElementsByTagName('img');
	var thumbnailsParent = getChild(tmbContainer,"table");
	
	var i=0;
	thumbnailsWidth = 0;
	if (tmbContainer.intervalID) return;
	for (i=0; i<thumbnails.length;i++) thumbnailsWidth += parseInt(thumbnails[i].width,10);
	thumbnailsWidth += (thumbnails.length-1)*15; //add space between images
	if (tmbContainerWidth >= thumbnailsWidth) return;
	
	if (step < 0) {
		if (parseInt(thumbnailsParent.style.left,10) >= (tmbContainerWidth-thumbnailsWidth)) moveContainer(tmbContainerWidth,thumbnailsWidth,step);
	} else {
		if (parseInt(thumbnailsParent.style.left,10) < 0) moveContainer(tmbContainerWidth,thumbnailsWidth,step);
	}
}

function moveContainer(tmbContainerWidth,thumbnailsWidth, step) {
	var tmbContainer = document.getElementById('thumbnails');
	var thumbnails = getChild(tmbContainer,"table");
	
	thumbnails.style.left = parseInt(thumbnails.style.left,10)+step+"px";
	
	if (step < 0)  {
		if (parseInt(thumbnails.style.left,10) >= (tmbContainerWidth-thumbnailsWidth)) {
			tmbContainer.intervalID = setTimeout ("moveContainer("+tmbContainerWidth+","+thumbnailsWidth+","+step+")", 30);
			return;
		}
	} else {
		if (parseInt(thumbnails.style.left,10) < 0)  {
			tmbContainer.intervalID = setTimeout ("moveContainer("+tmbContainerWidth+","+thumbnailsWidth+","+step+")", 30);
			return;
		}
	}
	tmbContainer.intervalID = null;
}

function stopScroll() {
	var tmbContainer = document.getElementById('thumbnails');
	if (tmbContainer && tmbContainer.intervalID) {
		clearTimeout (tmbContainer.intervalID);
		tmbContainer.intervalID = null;
	}
}

function setActiveTab(e,activeStyle, regularStyle) {
	var table = getAncestorByName(e, "TABLE");
	var active_tab;
	var div;
	if (table) {
		active_tab = (table.getAttribute('activeTab')) ? document.getElementById('tab_label_'+table.getAttribute('activeTab')) : false;
		pos = parseInt(e.id.substring(e.id.lastIndexOf('_')+1),10);
		if (table.getAttribute('activeTab') == pos) return;
		setAttribute(table,'activeTab',pos);
		if (active_tab) {
			if (div = getChild(active_tab,'div')) div.style.background = div.oldBackground; // resrore on previous active tab
			active_tab.oldClassName = regularStyle;
			mouseOut(active_tab);
			tabOutShadow(active_tab);
		}
		if (div = getChild(e,'div')) { // remove self delimiter
			div.oldBackground = div.style.background;
			div.style.background = '';
		}
		tabOverShadow(e);
		e.className = activeStyle;
		mouseOver(e);
	}
}

function tabOverShadow(e) {
	var i;
	var tds;
	var div;
	var pos, curr_pos;
	var table = getAncestorByName(e, "TABLE");
	var active_tab = (table.getAttribute('activeTab')) ? document.getElementById('tab_label_'+table.getAttribute('activeTab')) : false;
	if (!table) return;
	curr_pos = parseInt(e.id.substring(e.id.lastIndexOf('_')+1),10);
	tds = table.getElementsByTagName('td');
	for (i=0; i<tds.length; i++) {
		pos = parseInt(tds[i].id.substring(e.id.lastIndexOf('_')+1),10);
		if (pos == curr_pos) {
			if (i) {
				tds[i-1].style.backgroundImage = "url('images/icons/icon-tab-l-over.gif')";
				tds[i-1].style.backgroundPosition = "100% 100%";
				tds[i-1].style.backgroundRepeat = "no-repeat";
				if (div = getChild(tds[i-1],'div')) {
					div.oldBackground = div.style.background;
					div.style.background = '';
				}
			}
			if (i<tds.length) {
				tds[i+1].style.backgroundImage = "url('images/icons/icon-tab-r-over.gif')";
				tds[i+1].style.backgroundPosition = "0 100%";
				tds[i+1].style.backgroundRepeat = "no-repeat";
			}
			break;
		}
	}
}

function tabOutShadow(e) {
	var i;
	var div;
	var tds, curr_pos;
	var table = getAncestorByName(e, "TABLE");
	if (!table) return;
	curr_pos = parseInt(e.id.substring(e.id.lastIndexOf('_')+1),10);
	tds = table.getElementsByTagName('td');
	for (i=0; i<tds.length; i++) {
		pos = parseInt(tds[i].id.substring(e.id.lastIndexOf('_')+1),10);
		if (pos == curr_pos) {
			if (i) {
				tds[i-1].style.backgroundImage = "";
				if (div = getChild(tds[i-1],'div')) div.style.background = div.oldBackground; // resrore delimiter
			}
			if (i<tds.length) tds[i+1].style.backgroundImage = "";
			break;
		}
	}
}

function toggleFeature(id) {
	var tween = new Tween(id,'alpha',0,100,0.4,20,null);
	tween.animate();
}

function toggleSearch(e,id) {
	var start;
	var end;
	return;
	if (e && e.getAttribute('searchVisible') == 'yes') {
		setAttribute(e,'searchVisible','no');
		end = 0;
	} else {
		setAttribute(e,'searchVisible','yes');
		end = 160;
	}
	var tween = new Tween(id,'width',null,end,0.3,20,null);
	/*tween.onMotionFinished = function(property) {
		//document.getElementById('search-form').className = 'visible';
		//document.getElementById(id).style.marginRight = '0px;';
	}*/
	tween.animate();
}

function listPage(id,page) {
	var obj = document.getElementById(id);
	if (obj) {
		obj.value = page;
		obj.form.submit();
	}
}

function resizeWindowHeight(h) {
	var flash_object;
	flash_object = document.getElementById("products_object");
	if (flash_object) {
		if (!document.all) flash_object = getChild(flash_object,'embed');
		flash_object.height = Math.round(h);
		flash_object.width = 960;
	}
}


function fixHeight() {
	var cont = document.getElementById("content_container");
	var top =  document.getElementById("top");
	var dimensions = getBrowserSize();
	var h;
	var paddT = 0;
	var paddB = 0;
	if (cont.clientHeight < dimensions[1]) {
		if (document.all) {
			paddT = isNaN(parseInt(cont.style.paddingTop,10)) ? 0 : parseInt(cont.style.paddingTop,10);
			paddB = isNaN(parseInt(cont.style.paddingBottom,10)) ? 0 : parseInt(cont.style.paddingBottom,10);
		}
		h = dimensions[1]-top.clientHeight-paddT-paddB;
		cont.style.height = h+'px';
	}
}

function getBrowserSize() {
	var w = 0;
	var h = 0;
	if(typeof( window.innerWidth ) == 'number') {
	    //Non-IE
		w = window.innerWidth;
		h = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		w = document.documentElement.clientWidth;
		h = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	}
	return new Array(w,h);
}

function in_array(needle,haystack) {
	for (var i in haystack) {
		if (haystack[i] == needle) return true;
	}
	return false;
}

function calcConfiguration() {
	var price, products, frm, p, a;
	var format = '9,999,999.00';
	products = document.getElementsByName("product[base_price]");
	
	for (var i=0; i<products.length; i++) {
		price=0;
		frm = products[i].form;
		for (var j=0; j<frm.elements.length; j++) {
			if (frm.elements[j].name == 'product[base_price]' || frm.elements[j].name.substring(0,29) == 'product[configuration][price]') {
				switch (frm.elements[j].type) {
					case 'hidden' : 
						if (a = isNumber(format, Math.round(frm.elements[j].value*100)/100)) {
							price += a[1];
						}
					break;
					case 'select-one' :
						if (a = isNumber(format, Math.round(frm.elements[j].options[frm.elements[j].selectedIndex].value*100)/100)) {
							price += a[1];
							frm.elements[frm.elements[j].name.replace(/\[price\]/,'[title]')].value = frm.elements[j].options[frm.elements[j].selectedIndex].text;
						}
					break;
				}
			}
		}

		p = document.getElementsByName("price_"+(i+1));
		if (p.length != 0) {
			price=Math.round(price*100)/100;
			if (a = isNumber(format, price.toString())) {
				p[0].replaceChild(document.createTextNode(a[0]), p[0].firstChild);
			}
		}
	}
}

var ACTIVE_IMAGE_ID = 'tmb_-1';
function OLDzoomImage(obj, id, src) {
	if (obj.id == ACTIVE_IMAGE_ID) return;
	document.getElementById(id).src = src;
	if (ACTIVE_IMAGE_ID != 'tmb_-1') document.getElementById(ACTIVE_IMAGE_ID).style.border = '1px solid #959595';
	obj.style.border = '1px solid #006CD2';
	ACTIVE_IMAGE_ID = obj.id;
}

function zoomImage(obj, id, src, tmb) {
	var obj = document.getElementById(id);
	var def_width = 320;
	var def_height = 240;
	
	if (!obj) return;
	if (src.search(/\.swf$|\.swf\?/i) != -1) {
		obj.innerHTML = ""+
			"<object width='"+def_width+"' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0'>"+
				"<param name='movie' value='"+src+"' />"+
				"<param name='quality' value='high' />"+
				"<embed width='"+def_width+"' src='"+src+"' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash'></embed>"+
			"</object>";
		return;
	}
	if (src.search(/\.flv$|\.flv\?/i) != -1) {
		obj.innerHTML = ""+
			"<object width='"+def_width+"' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0'>"+
				"<param name='movie' value='/images/player/player.swf?src=../../"+src+"&thumb=../../"+tmb+"' />"+
				"<param name='quality' value='high' />"+
				"<param name='width' value='"+def_width+"' />"+
				"<param name='height' value='"+def_height+"' />"+
				"<embed width='"+def_width+"' height='"+def_height+"' src='/images/player/player.swf?src=../../"+src+'&thumb=../../"'+tmb+"' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash'></embed>"+
			"</object>";
		return;
	}
	if (src.search(/\.mpg$|\.mpg\?/i) != -1 || 
		src.search(/\.avi$|\.avi\?/i) != -1 ||
		src.search(/\.wmv$|\.wmv\?/i) != -1) {
		obj.innerHTML = ""+
			"<object width='"+def_width+"' classid='CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95' codebase='http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701' standby='Loading Microsoft Windows Media Player components...' type='application/x-oleobject'>"+
				"<param name='FileName' value='"+src+"' />"+
 				"<param name='ShowControls' value='true' />"+
 				"<param name='EnableContextMenu' value='true' />"+
				"<param name='ShowStatusBar' value='true' />"+
 				"<param name='AutoRewind' value='false' />"+
				"<param name='DefaultFrame' value='mainFrame' />"+
				"<embed width='"+def_width+"' type='application/x-mplayer2' pluginspage = 'http://www.microsoft.com/Windows/MediaPlayer/' src='"+src+"' ShowControls='true' EnableContextMenu='true' ShowStatusBar='true'  AutoRewind='false' defaultframe='mainFrame'></embed>"+
			"</object>";
		return;
	}
	
	obj.innerHTML = "<img src='"+src+"' width='"+def_width+"'/>";
}

