function pageWidth() {
   return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth :   document.body != null ? document.body.clientWidth : null;
} 

function pageHeight() {
  return  window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ?  document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
} 

var st = 0;
function scroll(a) {
    var el = document.getElementById('subNavItemsList');
    if(!el) return;
    var cm = el.style.margin;
    var s = cm.split(" ");
    var m = 0;
    if(s.length == 1)
    	m = parseInt(s[0]);
    else
    	m = parseInt(cm.split(" ")[3]);
    st = 0;
    if(a>0 && m<0) {
        doScroll(m, m+a, a/10, 10);
    } else if(a<0) {
        var tlength = parseInt(el.style.width)-150;
        var visiblepart = 750;
        if(tlength > visiblepart && (Math.abs(m) + visiblepart) < tlength) {
            doScroll(m, m+a, a/10, 10);
        }
    }
}

function doScroll(start, end, dm, steps) {
    var el = document.getElementById('subNavItemsList');
    el.style.margin = "0px 0px 0px "+ (start + st*dm) + 'px';
    st += 1;
    if(st<steps) {
       window.setTimeout("doScroll("+start+","+end+","+dm+","+steps+")" , 50);
    } else {
        el.style.margin = "0px 0px 0px "+ end  + 'px';
    }
}

function setNavItems() {
    var el = document.getElementById('subNavItemsList');
    if(!el) return;
    var items = el.getElementsByTagName('li').length;
    if(items<6) {
    	el = document.getElementById('scrollLeftBlock');
    	el.parentNode.removeChild(el);
    	el = document.getElementById('scrollRight');
    	el.parentNode.removeChild(el);
    } else {
	    var tlength = 150 * (items+1);
		el.style.width = tlength+"px"
    } 
}

var maxFixBGCounter = 0;
function fixBGImage() {
	if(maxFixBGCounter++>50) return;
	var img = document.getElementById('bgimg');
	var iw = img.naturalWidth || img.width;
	if(img.complete || iw>0) {
		if(Ext.isIE) {
			 var ni = new Image();
	         ni.src = img.src;
	         iw = ni.width;
		}
		var w = pageWidth(), h = pageHeight();
		var marge = (w - iw) / 2;
		img.style.margin = "0px 0px 0px "+marge+'px';
		var bgc = document.getElementById('backgroundcontainer');
		bgc.style.display = 'block';
		
		// Fix tourbuttons location
		var btn = parent && parent.document.getElementById('prevbtn');
		
		if(btn) { 
			marge = marge > 75 ? marge - 75 : 0;
			
			btn.style.left = (marge+2)+'px';
			btn = parent.document.getElementById('nextbtn'); 
			btn.style.right = (marge-2)+'px';
				
			btn = parent.document.getElementById('endbtn');
			btn.style.right = (marge-2)+'px';
		}
	} else if(Ext.isIE) {
    	window.setTimeout("fixBGImage()", 500);
	} else {
		window.setTimeout("fixBGImage()", 250);
	}
}

function updatePreviewLinks() {
	for (i=0; i<document.links.length; i++) {
		var lnk = document.links[i];
		if(lnk.pathname !== undefined) {
			var l = lnk.pathname.length;
			if(l<=9 || (l>9 && lnk.pathname.substring(0,9) != '/preview/' )) {
				if(lnk.pathname.substring(0,1)!='/')
					lnk.pathname = '/'+lnk.pathname;
				if(l <= 8 || (l > 8 && lnk.pathname.substring(0,8) != '/images/')) {
					lnk.pathname   = '/preview' + lnk.pathname;
				}
			}
		}
	}
}

Ext.onReady(function() {
	setNavItems();
	//Ext.EventManager.onWindowResize(fixBGImage);
});
