//global 
var blindDownTime=0.3;
var fadeTime=0.5;
var latest='';
var currentPage='';
var xmlHttp="";    

//=========================menu control==================================00

function getContent(str,strFirst){
    currentPage=str;
    //If the link is not klicked dont print anything to txtHint
    if (str.length==0){ 
        document.getElementById("contentdiv").innerHTML="";
        return;
    }

    //send id of menuimage to pulsate only if getContent is not called from body onload()
    if (strFirst!='a'){
        menuFx(str);
        clearTimeout(the_timeout);        
        clearTimeout(the_fade_timer);
        clearTimeout(the_unfade_timer);
    }
    

    //Get a xmlhttpobj by calling the GetXmlHttpObject() in gxho.js
    xmlHttp=GetXmlHttpObject();

    if (xmlHttp==null){
        alert ("Din webläsare saknar stöd för Ajax. Ladda ner Firefox eller uppgradera till Internet Explorer 7");
        return;
    } 

    //Set url 
    var url="content/"+str+".php";    
    xmlHttp.onreadystatechange=stateChanged;
    //xmlHttp.onreadystatechange=function(){stateChanged(str);};
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
} 

var start_rotate_timer;
var hl_timer;
function stateChanged() { 
    if (xmlHttp.readyState==4){ 
        document.getElementById("contentdiv").innerHTML=xmlHttp.responseText;
        xmlHttp="";
        if(currentPage=='start'){
            start_rotate_timer = setTimeout("rotate();", 1000);            
            //rotate();               
        }
        else if (currentPage=='bok'){            
            hl_timer = setTimeout("hl('hlDiv');", 1500);            
        }
        else if (currentPage=='shettis'){
            //if(document.getElementById("what")){
            //    editText('editShettisNews');
            //}
            
        }
        
    }
}

function hl(obj_to_hl){    
    new Effect.Highlight(obj_to_hl, { startcolor: '#FF4500',endcolor: '#FF4500' });    
    clearTimeout(hl_timer);
}

//=========================show/hide details==================================00    
var opened_number_of_times=0;
function disp(openThis){
    if (latest==openThis){
        if(opened_number_of_times > 0){
            new Effect.BlindDown(openThis, { duration: blindDownTime });             
            opened_number_of_times=0;
            return;
        }
        else{
            undisp(openThis);    
            opened_number_of_times++;
            return;
        }               
    }
    
    new Effect.BlindDown(openThis, { duration: blindDownTime }); 
 
    if (latest!=''){
        undisp(latest);        
    }

    latest=openThis; 
    document.getElementById('name').blur();   
}

function undisp(closeThis){
    new Effect.BlindUp(closeThis, { duration: blindDownTime });
}

function menuFx(strItem){    
    new Effect.Pulsate(strItem, { pulses: 1, duration: 2.5 });     
}

//=========================guestbook control==================================00

function dispGbForm(openThis){
    new Effect.BlindDown(openThis, { duration: blindDownTime }); 
    fadeImg('showFormLink');   
    
}

function postIt(poster,note,cap){
    //declare vars
    var strName=poster;
    var strNote=note;
    //var strCap=cap;    
    
    //check so that all fields are filled out
    if ( strName.length==0 ){        
        document.getElementById("poster").value="Du måste fylla i detta fält"; 
        document.getElementById("poster").style.backgroundColor="#FF0000";
        return;
    }
    else if ( strNote.length==0 ){        
        document.getElementById("note").value="Du måste fylla i detta fält"; 
        document.getElementById("note").style.backgroundColor="#FF0000";
        return;
    }
    /*
    else if ( strCap.length==0 ){        
        document.getElementById("cap").value="Du måste fylla i detta fält"; 
        document.getElementById("cap").style.backgroundColor="#FF0000";
        return;
    } 
    */   
    //concatenate string to send to server
    //var strToSend=strName+"-"+strNote+"-"+strCap;
    var strToSend=strName+"-"+strNote;
    //strToSend = replaceSwCh(strToSend);
	//alert(strToSend);
    //create a xmlhttp object
    xmlHttp=GetXmlHttpObject();

    //if the browser do not support ajax requests, let'em know it
    if (xmlHttp==null){
        alert ("Din webläsare saknar stöd för Ajax. Ladda ner Firefox eller uppgradera till Internet Explorer 7");
        return;
    } 
    
    //set path to php file
    var url2="functions/gb.php";

    //add str to send to querystring and get a random number to avoid caching
    url2=url2+"?z="+strToSend;
    url2=url2+"&sid="+Math.random();

    //when something happens i.e the objects state changes call stateChanged with the xmlHttp var and send it all away
    xmlHttp.onreadystatechange=function(){stateChanged2(xmlHttp);};
    xmlHttp.open("GET",url2,true);
    xmlHttp.setRequestHeader("Content-Type", "text/html; charset=utf-8");	
    xmlHttp.send(null);
    undisp('postContainer');
    unfadeImg('showFormLink'); 
    document.getElementById("poster").value="";
    document.getElementById("note").value=""; 
    //document.getElementById("cap").value=""; 
    
}
 
function stateChanged2(xmlHttp) { 
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
        document.getElementById("dispPosts").innerHTML=xmlHttp.responseText;
        xmlHttp="";
    } 
}

//=========================slideshow start page==================================00
var the_images = new Array(1);
the_images[0] = new Image();
the_images[0].src = "img/slideshow/img0.jpg";
the_images[1] = new Image();
the_images[1].src = "img/slideshow/img1.jpg";
the_images[2] = new Image();
the_images[2].src = "img/slideshow/img2.jpg";
the_images[3] = new Image();
the_images[3].src = "img/slideshow/img3.jpg";
the_images[4] = new Image();
the_images[4].src = "img/slideshow/img4.jpg";

the_images[5] = new Image();
the_images[5].src = "img/slideshow/img5.jpg";
the_images[6] = new Image();
the_images[6].src = "img/slideshow/img6.jpg";
the_images[7] = new Image();
the_images[7].src = "img/slideshow/img7.jpg";
the_images[8] = new Image();
the_images[8].src = "img/slideshow/img8.jpg";
the_images[9] = new Image();
the_images[9].src = "img/slideshow/img9.jpg";

var the_timeout;
var the_fade_timer;
var the_unfade_timer;
var index = 0;

function rotate(){
    clearTimeout(start_rotate_timer);
    //new Effect.Pulsate('sliderShow', { pulses: 1, duration: 2.5 });     
    window.document.starterImg.src = the_images[index].src;   
    index++;
    if (index >= the_images.length)    {
        index = 0;
    }
    //the_pulsate_timeout = setTimeout("menuFx('starterImg');", 4000);
    the_fade_timer = setTimeout("fadeImg('starterImg');", 2500);
    the_timeout = setTimeout("rotate();", 5000);
    the_unfade_timer = setTimeout("unfadeImg('starterImg');", 5100);
}

function fadeImg(image){
    new Effect.Opacity(image, { from: 0.8, to: 0, duration: 2.0 });     
}

function unfadeImg(image){
    new Effect.Opacity(image, { from: 0, to: 0.8, duration: 2.0 });     
}

function editText(itemToEdit){
    var dest = "functions/editShettis.php?item="+itemToEdit;
    new Ajax.InPlaceEditor(itemToEdit, dest, {rows:15,cols:40});
    //new Ajax.InPlaceEditor(itemToEdit, 'functions/editShettis.php', {rows:15,cols:40});
}

//==================================add news to frontpage================================================

function addNewArticle(rub,art){
    //declare vars
    var strRubrik=rub;
    var strArticle=art;
    
    
    //check so that all fields are filled out
    if ( strRubrik.length==0 ){        
        document.getElementById("rubrik").value="Du måste fylla i detta fält"; 
        document.getElementById("rubrik").style.backgroundColor="#FF0000";
        return;
    }
    else if ( strArticle.length==0 ){        
        document.getElementById("newsText").value="Du måste fylla i detta fält"; 
        document.getElementById("newsText").style.backgroundColor="#FF0000";
        return;
    }
    
    //concatenate string to send to server
    var strToSend=strRubrik+"-"+strArticle;

    //create a xmlhttp object
    xmlHttp=GetXmlHttpObject();

    //if the browser do not support ajax requests, let'em know it
    if (xmlHttp==null){
        alert ("Din webläsare saknar stöd för Ajax. Ladda ner Firefox eller uppgradera till Internet Explorer 7");
        return;
    } 
    
    //set path to php file
    var url3="functions/add_news.php";

    //add str to send to querystring and get a random number to avoid caching
    url3=url3+"?z="+strToSend;
    url3=url3+"&sid="+Math.random();

    //when something happens i.e the objects state changes call stateChanged with the xmlHttp var and send it all away
    xmlHttp.onreadystatechange=function(){stateChanged3(xmlHttp);};
    xmlHttp.open("GET",url3,true);
    xmlHttp.send(null);
    undisp('newsForm');
    
    document.getElementById("rubrik").value="";
    document.getElementById("newsText").value=""; 
    
    
}
 
function stateChanged3(xmlHttp) { 
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
        document.getElementById("newsDiv").innerHTML=xmlHttp.responseText;
        xmlHttp="";
    } 
}

function replaceSwCh(str){
	var str1=str.replace(/[å]/g, "&aring;");
	var str2=str1.replace(/[ä]/g, "&auml;");
	var str3=str2.replace(/[ö]/g, "&ouml;");

	var str4=str3.replace(/[Å]/g, "&Aring;");
	var str5=str4.replace(/[Ä]/g, "&Auml;");
	var str6=str5.replace(/[Ö]/g, "&Ouml;");

	return str6;
}

