// Created by VIRGOLEO in 2008

var menu = new Array("main.html", 				// 0
					 "routine.html", 			// 1
					 "empty.html", 				// 2					 
					 "key.html", 				// 3
					 "news.html",		// 4
					 "opinions.html", 			// 5
					 "contacts.html");			// 6


var homePageUrl = "http://www.templum.lv?pid=";
					 
//Path must be set from file 'index.html' location,
var pathToPages="pages/";

// This value will be used if it isn't possible to get screen resolution from client browser or client width is greater than normalScreenWidth. This value represents the maximum of globalTable
var normalScreenWidth=1023;

// This value represents critical screen resolution's width. If client width is less than this value, than criticalScreenWidth will use
var criticalScreenWidth=1000;

function fitPageToScreen(){	
	var globalTable=getElementById('globalTable');			
	var templumLogo=getElementById('templumLogo');			
	var templumTextLogo=getElementById('templumTextLogo');		
	var globalTableHeader=getElementById('globalTableHeader');		
	
	// Clinet screen resolution's width
	if (parent.clientScreenWidth == 0)
	{
		parent.clientScreenWidth = getScreenWidth();
	}
		
	if (globalTable!=null)
	{
		globalTable.style.width=calculateObjWidth(parent.clientScreenWidth, normalScreenWidth);
	}	
	
	// Mentioned values represent default widths of appropriate image
	if (templumLogo!=null)
	{
		templumLogo.style.width=calculateObjWidth(parent.clientScreenWidth, 521);
	}	
	
	if (templumTextLogo!=null)
	{
		templumTextLogo.style.width=calculateObjWidth(parent.clientScreenWidth, 416);
	}		
	
	if (globalTableHeader!=null)
	{
		globalTableHeader.style.height=calculateObjWidth(parent.clientScreenWidth, 25);
	}		
}

function getElementById(elementId) {
	if (document.getElementById) {
        return document.getElementById(elementId);
    }
    else if (document.all) {
		return document.all[elementId];
    }
    else return null;
} 

// Set the display property's value of the current control
function setDisplayProperty(control, val)
{
	var ctrl=getElementById(control);
	
	if (ctrl!=null && ctrl.style.display!=val)
	{
		ctrl.style.display=val;		
	}	
}

// Frame must be hidden, but another control has top be shown
function hideFrame(anotherControl)
{
	setDisplayProperty('iframename', 'none');
	setDisplayProperty(anotherControl, 'inline');
}

//Frame is shown, but all other controls are hidden
function showFrame()
{
	setDisplayProperty('studPhotoesTable', 'none');
	setDisplayProperty('iframename', 'inline');	
}

function changeContext(menuItem){ 
	// Context must be changed ONLY if another menu item was selected	
	//if (parent.activeMenuItem != menuItem)
	{
		showFrame();
	
		var frame=getElementById('iframename');		
		if (frame!=null)
		{					
			// This code is needed especially for Opera. 
			frame.src="";								
			fileName=menu[isItInRange(menuItem, 0, menu.length)];						
			frame.style.height="0%";
			frame.src=pathToPages + fileName;									
			//parent.activeMenuItem=menuItem;					
		}		
	}
}

function redirect(id)
{	
	window.location = homePageUrl + id;
}

function load() 
{
	p = getUrlParameter("pid", 0);
	p = (!isNaN(p)) ? p : 0;
	
	dolphintabs.init("dolphinnav", p);
	
	fitPageToScreen();
	
	if (p == 2) {
		// Fotoalbums
		hideFrame('studPhotoesTable'); 		
	}
	else {
		changeContext(p);			
	}	
}	

function isItInRange(num, min, max){      
   if (!isNaN(num)){
        if(num >= min && num < max) return num;
        else return 0;
   }
   else {return 0;}
}

function getUrlParameter(key, default_)
{
  if (default_==null) default_="";
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
} 

// Returns screen resolution's width
function getScreenWidth() {
	var screenW = normalScreenWidth;
	if (parseInt(navigator.appVersion)>3) {
		screenW = screen.width;
		//screenH = screen.height;
	}
	else if (navigator.appName == "Netscape" && parseInt(navigator.appVersion)==3 && navigator.javaEnabled()) 
	{
		var jToolkit = java.awt.Toolkit.getDefaultToolkit();
		var jScreenSize = jToolkit.getScreenSize();
		screenW = jScreenSize.width;
		//screenH = jScreenSize.height;
	}
	
	return screenW;
}

// Retuns width, which is possible to use with TEMPLUM Web site
function getAllowedWidth(currWidth) {	
	var currentWidth = currWidth-5;
	
	if (currentWidth < normalScreenWidth) {
		currentWidth = criticalScreenWidth;
	}
	else if (currentWidth>=normalScreenWidth)
	{
		currentWidth=normalScreenWidth;
	}
	
	return currentWidth;
}

// Calculates object width based on current client screen resolution's width
function calculateObjWidth(currentScreenWidth, normalObjWidth) {
	return Math.round((normalObjWidth * getAllowedWidth(currentScreenWidth))/normalScreenWidth);
}
