/**
 * @author Miquel Subirats
 */

var lang;
var picture = 2;
var goingUp = 1;
var t = setTimeout("changeScreenPic("+picture+")",3000);

function checkExplorer(){	
	if(navigator.appName != 'Microsoft Internet Explorer'){
	    window.location = './index.php' + document.location.hash;
	} 
}

function checkBrowser(){	
	if(navigator.appName == 'Microsoft Internet Explorer'){
	    window.location = './index_ie.php' + document.location.hash;
	} 
}

function loadPage(initLang){
    
	$("#various1").fancybox({
		'titlePosition'		: 'inside',
		'transitionIn'		: 'none',
		'transitionOut'		: 'none'
	});
	
	$("a[rel=example_group]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});
	
	lang = initLang;
    document.getElementById("BT"+lang).className = "headerLanguagesButtonHover";                
    if(document.location.hash != ''){
    	var hash = document.location.hash.split('#!')[1];
    	var page = hash.split('&')[0];
    	if(page == 'news'){
    		var newId = hash.split('&')[1];
        	var newLang = hash.split('&')[2];
        	if(newId && lang){
        		if(lang != newLang){
        			window.location = './?lang='+newLang + document.location.hash;
        		}else{
        			gotoPage('news', newId);
        		}                			                		
        	}else{
        		gotoPage('news', '');
        	}
    	}else{
    		gotoPage(page, '');
    	}
    }else{
    	gotoPage('start', '');
    }          
}

function getPagePosition(element){
	var curleft = 0;
	var curtop = 0;
    if (element.offsetParent) {
         do {
           if(element.style && element.style.position && element.style.position=='absolute')break;
               curleft += element.offsetLeft;
               curtop += element.offsetTop;
         } while (element = element.offsetParent);
    }
    return [curleft,curtop];
}

function changeScreenPic(numPic){
   
    if(numPic == 0){
        numPic = 2;
        goingUp = 1;
    }else if(numPic == 7){
        numPic = 5;
        goingUp = 0;
    }

    if(goingUp == 1){
        picture = numPic + 1;
    }else if(goingUp == 0){
        picture = numPic - 1;
    }
    
    clearTimeout(t);
    t = setTimeout("changeScreenPic("+picture+")",3000);
    document.getElementById("screenPic1").className = "screenHidden";
    document.getElementById("screenPic2").className = "screenHidden";
    document.getElementById("screenPic3").className = "screenHidden";
    document.getElementById("screenPic4").className = "screenHidden";
    document.getElementById("screenPic5").className = "screenHidden";
    document.getElementById("screenPic6").className = "screenHidden";
    document.getElementById("screenPic"+numPic).className = "screen";
    document.getElementById("screenBt1").className = "screenBt";
    document.getElementById("screenBt2").className = "screenBt";
    document.getElementById("screenBt3").className = "screenBt";
    document.getElementById("screenBt4").className = "screenBt";
    document.getElementById("screenBt5").className = "screenBt";
    document.getElementById("screenBt6").className = "screenBt";
    document.getElementById("screenBt"+numPic).className = "screenBtHover";
}

function gotoPage(page, params){             
    if(page == 'news' && params != ''){                	
		document.location.hash = '#!' + page + '&' + params + '&' + lang;	
		params = '?nid=' + params + '&lang=' + lang;                	             	
    }else{
    	document.location.hash = '#!' + page;
    }
    document.getElementById("startButton").className = "menuBt";
    document.getElementById("companyButton").className = "menuBt";
    document.getElementById("improvementButton").className = "menuBt";
    document.getElementById("productsButton").className = "menuBt";
    document.getElementById("imagesButton").className = "menuBt";
    document.getElementById("faqButton").className = "menuBt";
    document.getElementById("newsButton").className = "menuBt";
    document.getElementById("ratingsButton").className = "menuBt";
    document.getElementById("contactButton").className = "menuBt";
    document.getElementById(page+"Button").className = "menuBtHover";

    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("content").innerHTML = xmlhttp.responseText;
            setTimeout(function(){ layoutRefeshSize(); },0);
            setTimeout(function(){ layoutRefeshSize(); },100);
            setTimeout(function(){ layoutRefeshSize(); },500);
            setTimeout(function(){ layoutRefeshSize(); },1000);
            setTimeout(function(){ layoutRefeshSize();/*fancyBoxSetting();*/ },1500);
            
            paseAjaxResponse(document.getElementById("content").innerHTML);
            //document.getElementById("content").style.height = document.getElementById("contentInserted").style.height;
        }
    }
    xmlhttp.open("GET", page+".php"+params, true);
    xmlhttp.send();
}

function paseAjaxResponse(somemixedcode){
    var source = somemixedcode;
    var scripts = new Array();
    while(source.indexOf("<script") > -1 || source.indexOf("</script") > -1) {
        var s = source.indexOf("<script");
        var s_e = source.indexOf(">", s);
        var e = source.indexOf("</script", s);
        var e_e = source.indexOf(">", e);
        scripts.push(source.substring(s_e+1, e));
        source = source.substring(0, s) + source.substring(e_e+1);

    }
    
    for(var x=0; x<scripts.length; x++) {
        try {
            eval(scripts[x]);
        }
        catch(ex) {
        }
    }
    return source;
}

function layoutRefeshSize(){
    var footer = document.getElementById('footer');
    if(footer){
    	var pos = getPagePosition(footer);
        var heightPos = pos[1];
        heightPos += footer.offsetHeight - 40;
        document.getElementById("layout").style.height = heightPos+'px';
    }    
}


function changeLang(lang){
	var page = document.location.hash.split('#!')[1];
	page = page.split('&')[0];
	if(page == 'news'){
		document.location.hash = '#!' + page;
	}
    window.location = './?lang='+lang + document.location.hash;
}
