/**
* @author Martin Anderle
*/

function xmlIgnoreWhiteSpace(xml)
{
	var ret = new Array();
	var i=0;
	var l=xml.length;
			
	for(i=0;i<l;i++)
	{
		if(xml[i].textContent.length>3)
			ret.push(xml[i]);
	}
			
	return ret;
};

function xmlValue(entry,type)
{
	if(jQuery.browser.msie)
		return entry.getElementsByTagName(type)[0].text;
	else
		return entry.getElementsByTagName(type)[0].firstChild.wholeText;
};

function trace(s)
{
	var i=0;
	var l=arguments.length;
	
	var str="";
	for(i=0;i<l;i++)
	{
		str+=arguments[i];
		
		if(i<l-1)
			str+=", ";
	}
	
	if(window.console)
		window.console.log(str);
}

function debug(s)
{
	if(window.console)
		window.console.log(arguments);
}


function loadData(url,dataType,callback,param)
{
	var xmlHttp = null;
	
	try
	{
		xmlHttp = new XMLHttpRequest();
	}
	catch(e)
	{
		try
		{
			xmlHttp  = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				xmlHttp  = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e)
			{
				xmlHttp  = null;
			}
		}
	}
			
	if (xmlHttp)
	{
		xmlHttp.open('GET', url, true);
			
		xmlHttp.onreadystatechange = function ()
		{
			if (xmlHttp.readyState == 4)
			{
				if(dataType=="xml")
				{
					if(param!=null)
					{
						if(String(param).length>0)
						{
							callback(xmlHttp.responseXML,param);
						}
						else
							callback(xmlHttp.responseXML);
					}
					else
						callback(xmlHttp.responseXML);
					
				}
				else
				{
					if(param!=null)
					{
						if(String(param).length>0)
						{
							callback(xmlHttp.responseText,param);
						}
						else
							callback(xmlHttp.responseText);
					}
					else
						callback(xmlHttp.responseText);
				}
			}
		};
		xmlHttp.send(null);
	}
}

// Custom utility functions

function toTitleCase(str) {
    return str.substr(0,1).toUpperCase() + str.substr(1).toLowerCase();
}      

function formatTitle(title) {
    return 'Nordisch' + (title != '/' ? ' / ' + toTitleCase(title.substr(1, title.length - 2).replace(/\//g, ' / ')) : '');
}

function getTransport() {
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try {
            return new ActiveXObject('Msxml2.XMLHTTP');
        } catch(e) {
            return new ActiveXObject('Microsoft.XMLHTTP');
        }
    }
}  

// Custom SWFAddress and Ajax handling
function updateChange(xhr)
{
	if (xhr.readyState == 4)
	{
        if (xhr.status == 200)
		{
			contentData = xhr.responseText;
			
			if(window.siteInit)
			{
				if(window.isMobile)
				{
					contentSetData();
					return;
				}
				
				if(window.site==null)
				{
					contentSetData();
					return;
				}
				
				if(window.site.pos!=window.next.pos)
				{
					view.motionOut();
				}
				else
				{
					contentChange();
				}
			}
			else
				contentSetData();
        }
		else
		{
            alert('Error: ' + xhr.status + '!');
        }
    }
}


function handleChange(event)
{
	var index, rel, links = document.getElementsByTagName('a'), path = event.path;
	var loadId;
	
	if (path.substr(path.length - 1) != '/')
        path += '/';
	
	
	
	//NAVI TOP
	var pathArray = path.split("/");
	
	if(pathArray.length>2)
	{
		for(var s=0;s<sites.items.length;s++)
		{
			if(sites.items[s].id==pathArray[1])
			{
				for (var i = 0, l, link; link = links[i]; i++) 
				{
					rel = link.rel.split("/")[1];
					
					if(rel!=null)
					{
						if(sites.items[s].navi == rel)
						{
							link.className = "selected";
							loadId = sites.items[s].id;
						}
						else
						{
							link.className = "";
						}
					}
				}
				break;
			}
		}
	}
	else
	{
    	for (var i = 0, l, link; link = links[i]; i++) 
		{
			index = link.rel.indexOf('?');
        	rel = (index > -1) ? link.rel.substr(0, index) : link.rel;
		
			link.className = (rel == path) ? 'selected' : '';
		
			if((rel=="/" || rel=="home" || rel=="/home/"+window.lang+"/" || rel=="/home/"+window.lang+"/") && (path=="/" || path=="/home/"))
			{
				link.className = 'selected';
				loadId = "home";
			}
		}
	}
	
	//CONTENT
    var parameters = '';
    for (var p in event.parameters)
	{
        parameters += '&' + p + '=' + event.parameters[p];
    }
	
	
	var siteUpdate = true;
	//SITE UPDATE
	
	if(event.path==window.loadSite)
	{
		if(!window.site.php)
			siteUpdate=false;
	}
	
	
	
	if(siteUpdate)
	{
		if(window.site!=null) //SITE CHANGE EXCEPTION FOR DIFFERENT HOME SITES
		{
			if(loadId == "home" && (loadId==window.site.id))
				return;
		}
		
		var xhr = getTransport();
    	xhr.onreadystatechange = function()
		{
    	    updateChange(xhr);
    	};
	
    	xhr.open('get', 'datasource.php?swfaddress=' + event.path + parameters, true);
    	xhr.send('');
		contentSetNext();
		window.loadSite=event.path;
	}
	else
		siteDataUpdate();
}

function fadeOut(id,func)
{
	$(id).stop({clearQueue:true});
	
	if(func!=null)
		$(id).fadeOut(250,func);
	else
		$(id).fadeOut(250);
};

function fadeIn(id)
{
	$(id).stop({clearQueue:true});
	$(id).fadeIn(400);
};


function handleBgResize()
{
	$("#backgroundImage").css("left","0");
    var doc_width = $(window).width();
    var doc_height = $(window).height();
    var image_width = 1920;//1280;//$("#triquibackimg").width();
    var image_height = 1080;//720;//$("#triquibackimg").height();
    var image_ratio = image_width/image_height;      
    var new_width = doc_width;
    var new_height = Math.round(new_width/image_ratio);
	 
    if(new_height<doc_height)
	{
		new_height = doc_height;
		new_width = Math.round(new_height*image_ratio);
		
		var width_offset = Math.round((new_width-doc_width)/2);
		$("#backgroundImage").css("left","-"+width_offset+"px");
	}
	
	$("#backgroundImage").width(new_width);
	$("#backgroundImage").height(new_height);
}

window.loadSite = "";
window.site = null;
var contentData;
var namespace=function(c,f,b){var e=c.split(f||"."),g=b||window,d,a;for(d=0,a=e.length;d<a;d++){g=g[e[d]]=g[e[d]]||{};} return g;};
