/**
 *	Skandiabanken.se: Base functionality
 *	@author johan weitner at mogul
 */

var DEFAULT_TEXT;		// Holder for default text in input text field
var CURR_FIELD;			// Holds reference to current input text field



/**
	Initialize page
*/
window.onload = function() {
	initFormFields();			// Initialize event handlers for INPUT text fields
	initCollapseTables(false);	// Initialize event handlers for table collapsing. @param boolean radioBehavior
	
	//--------- Fixa supportmenyns position i IE5 och IE6 ----- //
	if (screen.width<1000) {
	document.getElementById('lurker').style.display='none';
	}
	var Browser = {
		browser_version : navigator.appVersion, IsIE : function(){
			if (this.browser_version.indexOf("MSIE 6.0") != -1 || this.browser_version.indexOf("MSIE 5.0") != -1)
				return true;
			else
				return false;
		}
	}
	var HeadInnerContainer = document.getElementById('headinnercontainer');
	var MinWidth = 767;
	var MaxWidth = 989;
	var frameWidth = 0;
	setSize = function(){
    if (self.innerWidth)
    {
	    frameWidth = self.innerWidth;
    }
    else if (document.documentElement && document.documentElement.clientWidth)
    {
	    frameWidth = document.documentElement.clientWidth;
    }
    else if (document.body)
    {
	    frameWidth = document.body.clientWidth;
    }
	
		if(frameWidth < MinWidth)
			HeadInnerContainer.style.width=MinWidth+'px';
		else if(frameWidth > MaxWidth)
			HeadInnerContainer.style.width=MaxWidth+'px';
		else {
			HeadInnerContainer.style.width = "auto";
		}
	}
	if(Browser.IsIE()){
		setSize();
		window.onresize = function(){setSize();}
	}
	
}



/**
	Initialize event handlers for input text fields
*/
function initFormFields() {
    var pt = document.getElementById("usercontrolpage");
    var caf = document.getElementById("customer-application-form");
    var pp = document.getElementById("presspage");
    if (pt == null && caf == null && pp == null) {
        var f = document.getElementsByTagName("INPUT");
        for (i = 0; i < f.length; i++) {
            if (f[i].type == "text") {
                f[i].onfocus = function() {
                    if (!this.value) return;
                    DEFAULT_TEXT = this.value;
                    CURR_FIELD = this;
                    this.value = "";
                }
                f[i].onblur = function() {
                    if (this.value == "" && this == CURR_FIELD) {
                        this.value = DEFAULT_TEXT;
                        CURR_FIELD = null;
                    }
                }
            }
        }
    }
} 




/**
	Initialize event handlers for table collapsing
*/
function initCollapseTables(radioBehavior) {
	if(document.getElementsByTagName("BODY")[0].className.indexOf("fullpricelist") == -1) return;
	var c = document.getElementsByTagName("CAPTION");
	for(i=0; i < c.length; i++) {
		c[i].firstChild.onclick = function() { toggleTable(this, radioBehavior); return false;}
	}
}


/**
	Toggle pricelist table visibility
*/
function toggleTable(obj, radioBehavior) {
	obj.blur();
	obj = obj.parentNode.parentNode;
	if(obj.className.indexOf("open") != -1) {
		removeCSSClass(obj, "open");
		//hideTables();
	}
	else {
		if(radioBehavior) hideTables();
		addCSSClass(obj, "open")
	}
}

/**
	Hide all pricelist tables
*/
function hideTables() {
	var t = document.getElementsByTagName("TABLE");
	for(i=0; i < t.length; i++) {
		removeCSSClass(t[i], "open");
	}
}




/**
	CSS class attribute utiliies
*/
function addCSSClass(obj,css) {
	if(obj.className.indexOf(css) != -1) return;
	if(obj.className == null || obj.className == "") obj.className = css;
	else obj.className += (" " + css);
}
function removeCSSClass(obj, css) {
	if(obj.className.indexOf(css) > 0) obj.className = obj.className.replace(" " + css, "");
	else obj.className = obj.className.replace(css, "");
}

/**
Handle backbutton for newspages
*/
function newsBackButton()
{
    var redirectUrl = '';
    if (typeof NewsPageSettings != 'undefined' && NewsPageSettings.length > 0)
    {
        redirectUrl = NewsPageSettings[0];
        if (document.referrer.indexOf(redirectUrl) > -1) history.back(1);
        else location.href = redirectUrl;
    }
    else history.back(1);
}
