/*extern
Ajax,
alert,
document,
panel,
selectedMenu,
window
*/

var Site = {
	
	msgBox:null,
						  
	start: function(){
		selectedMenu="menu_start";
		document.getElementById(selectedMenu).style.cssText="background-position: bottom left;";
		Site.msgBox = new Site.MessageBox();
		Site.msgBox.setText("testing");
	},
	
	selectMenu: function(menuID, siteID){
		if(!selectedMenu) 
		{
			selectedMenu="menu_start";
		}
		document.getElementById(selectedMenu).style.cssText="";
		
		selectedMenu=menuID;
		document.getElementById(selectedMenu).style.cssText="background-position: bottom left;";

		panel.clearTimer().toElement($(siteID)); return false;
		
	}, 
	
	sendMail: function()
	{
		var mailBody ="";
		var errorText="";
		var mName =  document.getElementById('mailName').value;
		var mTelefon = document.getElementById('mailTelefon').value;
		var mEmail = document.getElementById('mailEmail').value;
		var mBody = document.getElementById('mailBody').value;
		
		if(mName!== "" && mName!== "Namn:")
		{
			mailBody += "Namn: "+ mName + "\n";
		}
		else
		{
			errorText += "- Namn <br>";
		}
		
		if(mTelefon!== "" && mTelefon!== "Telefon:")
		{
			mailBody += "Telefon: "+ mTelefon + "\n";
		}
		else
		{
			errorText += "- Telefon <br>";
		}
		
		if(mEmail!== "" && mEmail!== "Email:")
		{
			mailBody += "Email: "+ mEmail + "\n";
		}
		else
		{
			errorText += "- Email <br>";
		}		
		
		if(mBody!== "" && mBody!== "Meddelande:")
		{
			mailBody += "Meddelande: "+ mBody + "\n";
		}
		else
		{
			errorText += "- Meddelande <br>";
		}		
		   
		
		if(errorText==="") 
		{
			var mailClient = new Ajax('mail.php',{postBody:{"mailBody":mailBody}, onComplete: Site.mailFeedback}).request();
		}
		else
		{
			Site.msgBox.setVisible(true); 
			Site.msgBox.setText("<h3>F&ouml;ljande f&auml;lt &auml;r ej korrekt ifyllda</h3><br><p>"+errorText+"</p>");
		}
	},
	
	mailFeedback: function(e)
	{
			Site.msgBox.setVisible(true); 
			Site.msgBox.setText(e);

	},
	
	clearDefault: function(obj, defaultValue)
	{
		if(obj.value === defaultValue)
		{
			obj.value ="";
		}
	},
	
	setDefault: function(obj, defaultValue)
	{
		if(obj.value === "")
		{
			obj.value =defaultValue;
		}
	},

	MessageBox : function()
	{
		var that = this;
		this.html= document.createElement("div");
		this.html.style.visibility = "hidden";
		this.html.style.zIndex = "9000";
		this.html.className = "messageBox";
	
		this.topBar = document.createElement("div");
		this.topBar.className = "topBar";	
		this.topBar.appendChild(document.createTextNode("St\u00e4ng"));

		this.message = document.createElement("div");
		this.message.className = "messageContent";

		this.topBar.onclick = function(){ that.setVisible(false);};
		document.body.appendChild(this.html);
		this.html.appendChild(this.topBar);
		this.html.appendChild(this.message);
	
		this.setText = function(text)
		{
			this.message.innerHTML = text;	
		};
	
		this.setVisible=function(visible)
		{
			if(visible)
			{ 
				that.html.style.visibility = "visible";

			}
			else
			{
				that.html.style.visibility = "hidden";
				that.setText("");
	
			}
		};
	}

};


	var offices = [
	{id:0,street:"Myntgatan 1",city:"591 61 Motala", telefon:"0141- 21 27 72", email:"office@mattjanst.se",pic:""},
	{id:1,street:"Fiskhallsv\u00e4gen 16",city:"120 44 \u00c5rsta", telefon:"08-722 4600", email:"office@mattjanst.se",pic:"",postadress:"Myntgatan 1, 591 61 Motala"},
	{id:2,street:"Upplagsv\u00e4gen 5B",city:"972 54 Lule\u00e5", telefon:"0141- 21 27 72", email:"office@mattjanst.se",pic:""},
	{id:3,street:"Krokstorpsv\u00e4gen 7",city:"261 62 Glumsl\u00f6v",telefon:"0418 - 731 72",email:"office@mattjanst.se",pic:""}
	];
	
	var employees = [];
	var news = [];
	var projects=[];
	var projectStartsida =[],projectStart=0;
	
	var tjanster = [
					{id:0, name:'', info:''},
					]
	
	var selectedOffice = -1, selectedEmployees=-1;

	 function selectEmployees(id){
	 	if(id!=selectedEmployees){
			if(selectedEmployees!=-1){
			document.getElementById("empMenu").childNodes[selectedEmployees].firstChild.setAttribute("class","");
			document.getElementById("empMenu").childNodes[selectedEmployees].firstChild.setAttribute("className","");
			}
			selectedEmployees = id;
			document.getElementById("empMenu").childNodes[selectedEmployees].firstChild.setAttribute("class","active");
			document.getElementById("empMenu").childNodes[selectedEmployees].firstChild.setAttribute("className","active");
			
			var tmp= new Json.Remote("engine.php",{onComplete: function(obj){renderEmployees(obj);}}).send("ACTION=selectEmployeesFromOffice&OFFICEID="+selectedEmployees);
			//renderEmployees(employees[selectedEmployees]);
			
			
	 }
	 
	 function renderEmployees(obj)
	 {
		 	employees = obj.result;
			var count=0;
			var farg = false;
			var htmlCode = "<br/>"

			while(count < employees.length){
			if (farg) htmlCode+="<a class='alist' href='#' onclick='showEmployeeDetails("+selectedEmployees+","+count+");' >&raquo; "+employees[count][1]+"</a>";
			else htmlCode+="<a class='blist' href='#' onclick='showEmployeeDetails("+selectedEmployees+","+count+");'>&raquo; "+ employees[count][1]+"</a>";
			//<a href="#TB_inline?height=300&width=300&inlineId=myOnPageContent" title="" class="smoothbox">Show</a>

			farg=!farg;
			count++;
			}
			//document.getElementById("kontor_pic").setAttribute("src",offices[selectedOffice].pic);
			document.getElementById("employees").innerHTML = htmlCode;
			//showAddress(offices[selectedOffice].street +", "+offices[selectedOffice].city);
		}
		 
		 
	 }
	 
	 function selectOffice(id){
		if(id!=selectedOffice && id < offices.length){
			if(selectedOffice!=-1){
			document.getElementById("officeMenu").childNodes[selectedOffice].firstChild.setAttribute("class","");
			document.getElementById("officeMenu").childNodes[selectedOffice].firstChild.setAttribute("className","");
			}
			selectedOffice = id;
			document.getElementById("officeMenu").childNodes[selectedOffice].firstChild.setAttribute("class","active");
			document.getElementById("officeMenu").childNodes[selectedOffice].firstChild.setAttribute("className","active");
			document.getElementById("kontor_pic").setAttribute("src",offices[selectedOffice].pic);
			
			if (offices[selectedOffice].pic=="") document.getElementById("kontor_pic").setAttribute("style","display:none;");
			else document.getElementById("kontor_pic").setAttribute("style","display:inline;");
			document.getElementById("address").innerHTML = "M&auml;ttj&auml;nst AB<br/>"+offices[selectedOffice].street+"<br/>"+offices[selectedOffice].city+"<br/><br/>Telefon: "+offices[selectedOffice].telefon+"<br/>Email: "+offices[selectedOffice].email;
			
			if(offices[selectedOffice].postadress)
			{
				document.getElementById("address").innerHTML = document.getElementById("address").innerHTML + "<br/><br/>Postadress: " + offices[selectedOffice].postadress;
			}
			showAddress(offices[selectedOffice].street +", "+offices[selectedOffice].city);
		}

	}
	
	function showEmployeeDetails(officeId,employeeId)
	{
		var img ="";
		if(employees[employeeId][6]!=="")
		{
			img = "<img src='"+employees[employeeId][6]+"' align='right' hspace=10 vspace=10 class='employeepic'/>";
		}
		Site.msgBox.setText(img+"<h2>"+employees[employeeId][1]+"</h2><p>"+employees[employeeId][2]+"<br><br>Telefon:"+employees[employeeId][3]+"<br>Email: <a href='mailto:"+employees[employeeId][4]+"'>"+employees[employeeId][4]+"</a></p>");
		Site.msgBox.setVisible(true);
	}
	
	function showTjanster(id)
	{
		Site.msgBox.setText("<h2>"+tjanster[id].name+"</h2><p>"+tjanster[id].info+"</p>");
	}
	
	function selectJobs()
	{
		var tmp= new Json.Remote("engine.php",{onComplete: function(obj){renderJobs(obj);}}).send("ACTION=selectJobs");	
	}
	
	function renderJobs(obj)
	{
		
		var tmp = document.getElementById('jobList');
		
		if(obj.result && obj.result.length > 0)
		{
			var htmlCode = "<ul class='jobList'>";
			var count=0;

			while(count < obj.result.length){
			htmlCode+="<li><a class='alist' href='"+obj.result[count][3]+"' target='_blank'>&raquo; "+ obj.result[count][1]+"</a></li>";
			count++;
			}
			
			htmlCode += "</ul>"
			tmp.innerHTML = htmlCode;
		}
	}
	
	function selectProjects()
	{
		var tmp1= new Json.Remote("engine.php",{onComplete: function(obj){renderProjects("0",obj);}}).send("ACTION=selectOpenProjects");	
		var tmp2= new Json.Remote("engine.php",{onComplete: function(obj){renderProjects("1",obj);}}).send("ACTION=selectClosedProjects");	
		
	}
	
	function renderProjects(id,obj)
	{
		projects[id] = [];
		projects[id] = obj.result;
		
		var count=0;
		
		
		while(count < projects[id].length)
		{			
			if(projects[id][count].startpage==1)
			{
				projectStartsida[projectStartsida.length]={"type":id,"id":count};
			}
			
			if(projects[id][count].xPos!==""&&projects[id][count].yPos!=="")
			{
				var cardPOINT = new GLatLng(projects[id][count].xPos,projects[id][count].yPos);
				
				projektKarta.addOverlay(createMarker(cardPOINT,id,count));
			}

	
			//htmlCode+="<a class='blist' href='#' onclick='showProjectDetails(\""+count+"\",\""+id+"\");'>&raquo; "+ projects[id][count][1]+"</a>";
			count++;
		}
		
		projectStart++;
		if(projectStart===2)
		{
			showProjectsOnStartPage();	
		}
		
		if(id==="0")
		{
			showProjects(id);
		}
	}
	
	function createMarker(point, id,count) {
		
  		// Create a lettered icon for this point using our icon class
  		var letter = projects[id][count].name;
		var marker = new GMarker(point);
		
		
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml("<span style='color:black'><b>" + letter + "</b><br><a href='#'  style='text-decoration:underline;' onclick='showProjectDetails(\""+count+"\",\""+id+"\");'>&raquo; mer information</a></span>");
		  });
		  return marker;
		}


	function showProjectsOnStartPage()
	{
		if(projectStartsida[0])
		{
			document.getElementById("projekt1").innerHTML="<div style='width: 250px; height:135px; overflow:hidden;' onclick='showProjectDetails(\""+projectStartsida[0].id+"\",\""+projectStartsida[0].type+"\");'><div style='width:240px;background-color:#666; padding:5px;color:white;'>"+projects[projectStartsida[0].type][projectStartsida[0].id].name+"</div><img style='width:250px;' src='"+projects[projectStartsida[0].type][projectStartsida[0].id]["bild1"]+"'></div>";
		}
		
		if(projectStartsida[1])
		{
			document.getElementById("projekt2").innerHTML="<div style='width: 250px; height:135px; overflow:hidden;' onclick='showProjectDetails(\""+projectStartsida[1].id+"\",\""+projectStartsida[1].type+"\");'><div style='width:240px;background-color:#666; padding:5px;color:white;'>"+projects[projectStartsida[1].type][projectStartsida[1].id].name+"</div><img style='width:250px;' src='"+projects[projectStartsida[1].type][projectStartsida[1].id]["bild1"]+"'></div>";
		}

	}
	
	function showProjects(id)
	{
		// open projects
	 	if(id==="0")
		{
			document.getElementById("proMenu").childNodes[1].firstChild.setAttribute("class","");
			document.getElementById("proMenu").childNodes[1].firstChild.setAttribute("className","");
			document.getElementById("proMenu").childNodes[0].firstChild.setAttribute("class","active");
			document.getElementById("proMenu").childNodes[0].firstChild.setAttribute("className","active");
		}
		else
		{
			document.getElementById("proMenu").childNodes[0].firstChild.setAttribute("class","");
			document.getElementById("proMenu").childNodes[0].firstChild.setAttribute("className","");
			document.getElementById("proMenu").childNodes[1].firstChild.setAttribute("class","active");
			document.getElementById("proMenu").childNodes[1].firstChild.setAttribute("className","active");

		}
			
	
			var count=0;
			var farg = false;
			var htmlCode = "<br/>"

			while(count < projects[id].length){
			if (farg) htmlCode+="<a class='alist' href='#' onclick='showProjectDetails(\""+count+"\",\""+id+"\");' >&raquo; "+projects[id][count].name+"</a>";
			else htmlCode+="<a class='blist' href='#' onclick='showProjectDetails(\""+count+"\",\""+id+"\");'>&raquo; "+ projects[id][count].name+"</a>";
			//<a href="#TB_inline?height=300&width=300&inlineId=myOnPageContent" title="" class="smoothbox">Show</a>

			farg=!farg;
			count++;
			}
			
			//document.getElementById("kontor_pic").setAttribute("src",offices[selectedOffice].pic);
			document.getElementById("projects").innerHTML = htmlCode;
			//showAddress(offices[selectedOffice].street +", "+offices[selectedOffice].city);
	}


	function showProjectDetails(id,type)
	{
		Site.selectMenu('menu_projekt','projektdetail');
		var html = document.getElementById("pDetailContent");
		var text="";
		
		if(html)
		{
			text = "<h2>"+projects[type][id].name+"</h2><div style='float:left; width:280px;'><p>("+projects[type][id].startDay+" - "+projects[type][id].endDay+")<br><br>Beskrivning:<br>"+nl2br_js(projects[type][id].description)+"</p>";
			
				if(projects[type][id]["youtube"]){
				text+="<img src='img/movie.jpg' style='padding-right:5px;'><a href='#' onclick='loadPlayer(\""+projects[type][id]["youtube"]+"\")'>Se projektfilm!</a>";
				}
				
				text+="<br><br><a href='#' onclick='Site.selectMenu(\"menu_projekt\",\"projekt\");return false;'>&raquo; Tillbaka till projekt&ouml;versikt</a></div><div style='float:right;'>";
			if(projects[type][id]["bild1"].trim()!=="")
			{
				text+= "<img src='"+projects[type][id]["bild1"]+"' style='display:inline; width:170px; padding:5px; margin:5px; border:solid 1px white;' onclick='showImagePopUp(\""+projects[type][id]["bild1"]+"\")'>";
			}
			if(projects[type][id]["bild2"].trim()!=="")
			{
				text+= "<img src='"+projects[type][id]["bild2"]+"' style='display:inline; width:170px;  padding:5px; margin:5px; border:solid 1px white;' onclick='showImagePopUp(\""+projects[type][id]["bild2"]+"\");'><br style='clear:both;'>";
			}
			if(projects[type][id]["bild3"].trim()!=="")
			{
				text+= "<img src='"+projects[type][id]["bild3"]+"' style='display:inline; width:170px;  padding:5px; margin:5px; border:solid 1px white;' onclick='showImagePopUp(\""+projects[type][id]["bild3"]+"\");'>";
			}
			if(projects[type][id]["bild4"].trim()!=="")
			{
				text+= "<img src='"+projects[type][id]["bild4"]+"' style='display:inline; width:170px; padding:5px; margin:5px; border:solid 1px white;' onclick='showImagePopUp(\""+projects[type][id]["bild4"]+"\");'><br style='clear:both;'>";
			}
			
			text+="</div><br style='clear:both;'>";
			
			html.innerHTML = text;
		}
	}
	
	// The "main method" of this sample. Called when someone clicks "Run".
	function loadPlayer(videoID) {
		var htmlCode="<div id='ytPlayer'></div>";
		Site.msgBox.setText(htmlCode);

	// Lets Flash from another domain call JavaScript
	  var params = { allowScriptAccess: "always" };
	 	 // All of the magic handled by SWFObject (http://code.google.com/p/swfobject/)
	swfobject.embedSWF("http://www.youtube.com/v/" + videoID +
                     "&enablejsapi=1&playerapiid=player1", "ytPlayer", "380", "228", "9.0.0", "expressInstall.swf");	

	  
	  Site.msgBox.setVisible(true);

	}

	function showImagePopUp(uri)
	{
			var htmlCode="<img src='"+uri+"' style='width:380px;'>";
				Site.msgBox.setText(htmlCode);
				Site.msgBox.setVisible(true);		
	}
	
	function selectNews()
	{
		var tmp= new Json.Remote("engine.php",{onComplete: function(obj){renderNews(obj);}}).send("ACTION=selectNews");	
	}
	
	function renderNews(obj)
	{
		var tmp = document.getElementById('newsContainer');
		
		news=obj.result;
		
		if(obj.result && obj.result.length > 0)
		{
			
			var htmlCode = "";
			var count=0;

			while(count < obj.result.length && count< 2){
			htmlCode+="<div class='news'><div class='date'>"+obj.result[count][0]+"</div>";
			htmlCode+="<p>"+obj.result[count][2]+"</p>"
			
			// open extra window for news
			if(obj.result[count][3]!=="")
			{
				htmlCode+="<a href='#' onclick='showDetailsNews("+count+")'>&raquo; L&auml;s mer</a>"
			}
			else 
			{
				//intern link		
				if(obj.result[count][6]!=="")
				{
					htmlCode+="<a href='#' onclick='Site.selectMenu(\"menu_"+obj.result[count][6]+"\",\""+obj.result[count][6]+"\")'>&raquo; L&auml;s mer</a><br>"				
				}
				//extern link
				else if(obj.result[count][7]!=="")
				{
					htmlCode+="<a href='"+obj.result[count][7]+"' target='_blank'>&raquo; L&auml;s mer</a><br>"								
				}
				//doc
				if(obj.result[count][8]!=="")
				{
					htmlCode+="<a href='"+obj.result[count][8]+"' target='_blank'>&raquo; Ladda ner</a>"								
				}
				
			}
			
			htmlCode+="</div>";
			count++;
			}
			
			tmp.innerHTML = htmlCode;
		}
		
		
		 /*<div class="news">
										<div class="date">2007-10-04</div>
										<p>Mättjänst expanderar.<br />Vi söker personal till Stockholm.</p>
										<a href="#" onclick="Site.selectMenu('menu_jobb','jobb'); return false;">» Läs mer</a>						
									</div>	*/
	}
	
	function showNewsArchive()
	{
			var count=0;
			var farg = false;
			var htmlCode = "<br/>"

			while(count < news.length){
			if (farg) htmlCode+="<a class='alist' href='#' onclick='showDetailsNews("+count+");' >&raquo; "+news[count][0]+" "+news[count][2]+"</a>";
			else htmlCode+="<a class='blist' href='#' onclick='showDetailsNews("+count+");;'>&raquo; "+ news[count][0]+" "+news[count][2]+"</a>";
			//<a href="#TB_inline?height=300&width=300&inlineId=myOnPageContent" title="" class="smoothbox">Show</a>

			farg=!farg;
			count++;
			}
			
			//document.getElementById("kontor_pic").setAttribute("src",offices[selectedOffice].pic);
			document.getElementById("newsArchiveContent").innerHTML = htmlCode;
			//showAddress(offices[selectedOffice].street +", "+offices[selectedOffice].city);
	}
	
	function showDetailsNews(id)
	{
	
		if(news[id])
		{
				
				var htmlCode="<h2>"+news[id][2]+"</h2><p>"+nl2br_js(news[id][3])+"</p>";
				//intern link		
				if(news[id][6]!=="")
				{
					htmlCode+="<a href='#' onclick='Site.selectMenu(\"menu_"+news[id][6]+"\",\""+news[id][6]+"\"); Site.msgBox.setVisible(false);'>&raquo; L&auml;s mer</a><br>"				
				}
				//extern link
				else if(news[id][7]!=="")
				{
					htmlCode+="<a href='"+news[id][7]+"' target='_blank'>&raquo; L&auml;s mer</a><br>"								
				}
				//doc
				if(news[id][8]!=="")
				{
					htmlCode+="<a href='"+news[id][8]+"' target='_blank'>&raquo; Ladda ner</a>"								
				}
			
			
			Site.msgBox.setText(htmlCode);
			Site.msgBox.setVisible(true);
		}
	}
	
	function nl2br_js(myString){return myString.replace( /\n/g, '<br />\n' );}

