var dropped=0;
var intID;
var hideinterval = 500;
var atmenu=false;
var newsInterval=9000;
var dataobj;
var thelength=280;
var scrollspeed=1;
var cache=1;
var cacheTop;
var initWait=5000;
var shiftY=0; //колко надолу да слезе подменюто

var bisOpera= ((navigator.userAgent.indexOf("Opera")>=0));
var bisIE = ((navigator.userAgent.indexOf("MSIE")>=0)&&(navigator.appVersion.substring(0,1)>=4) && (!bisOpera));
var currentPic = 0;

//--- news scroller - initialization ---
function initializeNews(){
	dataobj=document.getElementById("divnews");
	scrollnews();
}
//--- scroll the news box ---
function scrollnews() {
	dataobj.style.top=(parseInt(dataobj.style.top) - scrollspeed)+"px";
	if (parseInt(dataobj.style.top)<thelength*(-1)) {
		dataobj.style.top=(thelength-30)+"px";
	}
	setTimeout("scrollnews()",initWait);
	// reset the timeout after the initial interval has elapsed
	initWait=50;
}

function drop(n) {
	var sub = document.getElementById("submenu"+n);
	hide(dropped); //hide the already dropped submenu
	if (sub && n!=3)
	{

		var menu = document.getElementById("menu"+n);
		var cornerX = document.getElementById("menucontainer").offsetLeft;
		var cornerY = document.getElementById("menucontainer").offsetTop;
		//set submenu position
		sub.style.left = (menu.offsetWidth + cornerX)+"px";
		sub.style.top = (shiftY + menu.offsetTop+cornerY)+"px";

		dropped=n;
		if(!bisOpera) {
			var ifr = 	document.getElementById('ifr');
			// Init Iframe to have the same position and size as the div element and smaller zIndex
			ifr.style.width = sub.offsetWidth;
			ifr.style.height = sub.offsetHeight;
			ifr.style.top = sub.style.top;
			ifr.style.left = sub.style.left;
			ifr.style.zIndex = 899;
			ifr.style.display = "none";
			// make the div and the iframe visible

			ifr.style.display = "block";
		}
		sub.style.visibility = "visible";
		on();
		intID = window.setInterval("hidemenu()",hideinterval);
	}
}

function hide(n) {
	var omenu = document.getElementById("submenu"+n);
	if (omenu)
		{

		try {
			omenu.style.visibility="hidden";
			document.getElementById('ifr').style.display="none";

		}
		catch(e) {
		};
	}
	clearInterval(intID);
}

function hidemenu() {
	if (!atmenu)
	{
		hide(dropped);
	}
}

function on() {
	atmenu=true;
}

function off() {
	atmenu=false;
}


function doSearch() {
	document.getElementById("hiddenfld").value = document.getElementById("searchfld").value;// + " site:nortop.com";
	//document.getElementById("searchform").submit();
	return (true);
}

function checkSearch() {

}

function switchNews() {
	var container=document.getElementById("newscontainer");
	var contents = document.getElementById("divnews");
	//pause news switching
	window.clearInterval(intervalid);
	//apply transition filter
	if (currentnews==newscnt)
	{
		currentnews=0;
	} else {
		currentnews++;
	}
	s=navigator.userAgent;

	if (s.indexOf("MSIE")>-1 && s.indexOf("Opera")<0 && s.indexOf ("Firefox")<0)
	{
		container.filters[0].apply();
		contents.innerHTML = "<p>"+news[currentnews]+"</p>";
		container.filters[0].play();

	} else {
		contents.innerHTML = "<p>"+news[currentnews]+"</p>";
	}

	window.setTimeout("switchNews()",newsInterval);
}

function yourOpinion(pagetitle,lang) {
	window.open("opinion.php?lang="+lang+"&amp;w="+pagetitle,"_blank","width=450,height=350,status=1,toolbar=0,scrollbars=0,location=0");
}

//next functions are used to prevent comboboxes from overlapping
function showDiv(menuHandle){
	// Search for the div and the iframe elements
	var dv = document.getElementById(menuHandle);
	var ifr = 	document.getElementById('ifr');
	// Init Iframe to have the same position and size as the div element and smaller zIndex
	ifr.style.width = dv.offsetWidth;
	ifr.style.height = dv.offsetHeight;
	ifr.style.top = dv.style.top;
	ifr.style.left = dv.style.left;
	ifr.style.zIndex = dv.style.zIndex - 1;
	ifr.style.display = "none";
	// make the div and the iframe visible
	dv.style.visibility	= "visible";
	ifr.style.display  = "block";
 }
 function  hideDiv(){
	// Search for the div and the iframe elements
	var dv = document.getElementById('myDiv');
	var ifr = document.getElementById('myIframe');
	// hide the div and the iframe
	dv.style.visibility = "hidden";
	ifr.style.display = "none";
 }


 //// displays window size
 function showSize(size,scale) {
	try
	{
		var dims = size.split("x");
		size_w = parseInt(dims[0]);
		size_h = parseInt(dims[1]);
		var oSize = document.getElementById("winsize");
		oSize.style.width = scale*size_w+"px";
		oSize.style.height = scale*size_h+"px";
	}
	catch (e)
	{
	}

 }

 function goCatalog(section, lang) {
	var id = document.getElementById("productid").value;
	if(!lang || lang=="") lang="bg";
	if (id)
	{
		if(section>-1) {
			window.location = "?w="+section+"&id="+id+"&lang="+lang;
		} else {
			window.location = "?id="+id+"&lang="+lang;
		}
	}
 }

  function goGallery(lang) {
	var id = document.getElementById("productid").value;
	if(!lang || lang=="") lang="bg";
	if (id)
	{
		window.location = "?w="+id+"&id="+id+"&lang="+lang;
	}
 }


 function showElement(n) {
	var photo = document.getElementById("catalogphoto1");
	if (photo) {
		photo.src="images/stories/element"+n+".png";

	}
 }


function goNext() {
	currentPic++;
	showPic(currentPic);
}

function goPrev() {
	currentPic--;
	showPic(currentPic);
}

function showPic(n) {
	var phc=document.getElementById("photo_holder");
	var tmb=document.getElementById("thumbnail_holder");
	var snm=document.getElementById("snimka");
	var con=document.getElementById("container");
	//var tit=document.getElementById("snimka_title");
	var tid=document.getElementById("thumb"+n);

	//check if such thumbnail exists; if not, unpop
	if (!tid) //display the thumbnails again
	{
		unpopPic();
		return;
	}

	//get file path from alt text
	var imsrc = tid.alt;
	snm.src ="";
	if (bisIE)
	{
			con.filters.blendTrans.Apply();
			tmb.style.display="none";
			snm.src = imsrc;
			//tit.innerText = title[n];
			phc.style.display="block";
			con.filters.blendTrans.Play();
	} else {
			tmb.style.display="none";
			snm.src = imsrc;
			//tit.innerHTML = "<span>"+title[n]+"</span>";
			phc.style.display="block";
	}
	currentPic = n;
}

function unpopPic() {
	var phc=document.getElementById("photo_holder");
	var tmb=document.getElementById("thumbnail_holder");
	tmb.style.display="block";
	phc.style.display = "none";
}


function goPage(script) {
	var n = document.getElementById("page").value;
	window.location=script+"?page="+n;
}

function goSection(n, lang) {
	window.location="catalog.php?w=0&lang="+lang+"&section="+n;
}

$(document).ready(function() {

	$("#section").change(function() {
		goSection($(this).val(),$(this).attr("rel"));
	})

})
