// Page
var currentStep=0;
var currentBereich="";
function Themenbereich (givenid, giventitle) {
	this.id = givenid
	this.title = giventitle;
	this.csscolorfile = "";
	this.openerpicture = "";
	this.mainbgcolor = "";
	this.maintxtcolor = "";
	
	//this.metatitle = "";
	//this.metakeywords = "";
	//this.metadesc = "";
	
	this.getSubnavi = function () {
		//cleanPageBeforeAnyAction();
		currentBereich = this.title;
		//var url = "php/content/themenbereiche.php?ajaxcall&ajaxcallaction=getSubnavi&bereich=" + this.title;
		var url = "php/content/themenbereiche.php?ajaxcall&ajaxcallaction=getSubnavi&themenbereich=" + this.id;
		loadingBox.show();
		/*new Ajax.Request(url, {
			method: 'get',
			onSuccess: function(transport) {
				var submenuitems = $('submenuitems');
				var submenu = $('submenu');
				if (transport.responseText != "") {
					new Effect.Fade(submenu, {duration: 0.5});
					new Effect.Fade(submenu, {duration: 0.5, afterFinish: function() {
																	submenuitems.innerHTML = transport.responseText;
																	new Effect.Appear(submenu, {duration: 0.5, to: 0.7});
																	hideLoader();
																}
					});
				}
				else
				{
					new Effect.Fade(submenu, {duration: 0.5});
				}
			}
		});*/
		
		//function ajaxHistoryRequest(url, myState) 
		//{

			$('bgholder').setStyle("background-image: url(" + this.openerpicture + ")");
			//new Effect.Morph("topleft", { style: 'background:' + this.mainbgcolor + '; color: ' + this.maintxtcolor + ';', // CSS Properties
			//								  duration: 0.3 // Core Effect properties
			//								  });
			
			$$('.chgbgcolor').each( function(myelement) {
											 		var myBereich = Bereiche.getBereichById(currentBereichId);
													myelement.morph( 'background:' + myBereich.mainbgcolor + '; color: ' + myBereich.maintxtcolor + ';', // CSS Properties
											  		{
														duration: 0.3 // Core Effect properties
											  		})
											}
									);
			
			
			/*currentStep = currentStep+1;
			new Ajax.History.Request(url, {
				history : {
					id    : 'Counter',
					//state : this.title,
					cache : false,
					onStateChange: function(state) {
						// change title
						//History.setTitle(History.getTitle() + ' - Page Ajax #' + state);
					}
				},
				//parameters : { bereich: this.title},
				parameters : { bereich: this.id},
				onSuccess: function(transport, json) {
					//Page.getIntro(transport.request.parameters.bereich);
					//console.log(transport.request.parameters.bereich);
					var jsonobject = transport.responseText.evalJSON();
					
					var submenuitems = $('submenuitems');
					var submenu = $('submenu');
					if (transport.responseText != "") {
						//new Effect.Fade(submenu, {duration: 0.5});
						new Effect.Fade(submenu, {duration: 0.3, afterFinish: function() {
																		//submenuitems.innerHTML = transport.responseText;
																		submenuitems.innerHTML = jsonobject.txt;
																		new Effect.Appear(submenu, {duration: 0.3, to: 0.85});
																		//new Effect.Morph('topleft', { style: 'background:#a8101b; color: #fff;', // CSS Properties
																		//								  duration: 0.3 // Core Effect properties
																		//								});
																		hideLoader();
																		
																		//getthe first Navigationpoint and load the Text
																		Page.getText("dynamic", jsonobject.firstcontentid);
																	}
						});
					}
					else
					{
						new Effect.Fade(submenu, {duration: 0.3});
					}
				}
			});*/
			
			new Ajax.Request(url, {
				//parameters : { bereich: this.title},
				parameters : { bereich: this.id},
				onSuccess: function(transport, json) {
					//Page.getIntro(transport.request.parameters.bereich);
					//console.log(transport.request.parameters.bereich);
					var jsonobject = transport.responseText.evalJSON();
					
					var submenuitems = $('submenuitems');
					var submenu = $('submenu');
					if (transport.responseText != "") {
						//new Effect.Fade(submenu, {duration: 0.5});
						new Effect.Fade(submenu, {duration: 0.3, afterFinish: function() {
																		//submenuitems.innerHTML = transport.responseText;
																		submenuitems.innerHTML = jsonobject.txt;
																		new Effect.Appear(submenu, {duration: 0.3, to: 0.85});
																		//new Effect.Morph('topleft', { style: 'background:#a8101b; color: #fff;', // CSS Properties
																		//								  duration: 0.3 // Core Effect properties
																		//								});
																		hideLoader();
																		
																		//getthe first Navigationpoint and load the Text
																		anyAnimationShowing = false;
																		Page.getText("dynamic", jsonobject.firstcontentid);
																		
																	}
						});
					}
					else
					{
						new Effect.Fade(submenu, {duration: 0.3, afterFinish: function() {anyAnimationShowing = false;} });
					}
				}
			});
		//}

		
		//location.href += "#dumdidum";
		//this.loadcssfile();
	
	};
	this.getIntro = function () {
		var url = "php/content/themenbereiche.php?ajaxcall&ajaxcallaction=getIntro&bereich=" + this.title;
		loadingBox.show();
		new Ajax.Request(url, {
			method: 'get',
			onSuccess: function(transport) {
				var contenttext = $('contenttext');
				if (transport.responseText != "") {
					new Effect.Fade(contenttext, {duration: 0.5, afterFinish: function() {
																	contenttext.innerHTML = transport.responseText;
																	new Effect.Appear(contenttext, {duration: 0.5});
																	hideLoader();
																}
												});
				}
			}
		});
	};
	
	this.loadcssfile = function() {
		Page.UnloadPrevouisCssElem();
		var linkElem = document.createElement("link");
		linkElem.rel="stylesheet";
		linkElem.type = "text/css";
		linkElem.href = this.csscolorfile;
		

		document.getElementsByTagName("head")[0].appendChild(linkElem);
		Page.loadedColorCss = linkElem;
	}

}

var Bereiche = {
	bereiche : new Array( new Array(0, "Default", "http://internet:8080/Resortneu/site/css/colors_default.css", "images/opener/resort.jpg", "#83b5da", "#FFF"),
							new Array(2, "Resort", "http://internet:8080/Resortneu/site/css/colors_blue.css", "images/opener/resort.jpg", "#83b5da", "#FFF"),
							new Array(8, "Restaurants", "http://internet:8080/Resortneu/site/css/colors_blue.css", "images/opener/restaurants.jpg", "#83b5da", "#FFF"),
							new Array(14, "Meeting", "http://internet:8080/Resortneu/site/css/colors_blue.css", "images/opener/meeting.jpg", "#83b5da", "#FFF"), 
							new Array(20, "Specials", "http://internet:8080/Resortneu/site/css/colors_blue.css", "images/opener/specials.jpg", "#83b5da", "#FFF"),
							new Array(26, "Wellness", "http://internet:8080/Resortneu/site/css/colors_red.css", "images/opener/wellness.jpg", "#a8101b", "#FFF"),
							new Array(32, "Freizeit", "http://internet:8080/Resortneu/site/css/colors_blue.css", "images/opener/freizeit.jpg", "#83b5da", "#FFF"),
							new Array(38, "Service", "http://internet:8080/Resortneu/site/css/colors_blue.css", "images/opener/service.jpg", "#83b5da", "#FFF")
							),
	getBereichById : function(_id) {
		for(var bereich in Page.themenbereiche) {
			//if( Page.themenbereiche[bereich].title == bereichtoload) {
			if( Page.themenbereiche[bereich].id == _id) {
				return Page.themenbereiche[bereich];
			}
		}
	},
	getBereichByName : function(_name) {
		for(var bereich in Page.themenbereiche) {
			//if( Page.themenbereiche[bereich].title == bereichtoload) {
			if( Page.themenbereiche[bereich].title == _name) {
				return Page.themenbereiche[bereich];
			}
		}		
	}
}

var currentBereichId = 0;
var Page = {
	newText : "",
	metatitle : "",
	metakeywords : "",
	metadesc : "",
	themenbereiche : new Array(),
	init : function() {
		var bereiche = Bereiche.bereiche;
		for(var bereich in bereiche) {
			var temp = new Themenbereich(bereiche[bereich][0], bereiche[bereich][1]);
			temp.csscolorfile = bereiche[bereich][2];
			temp.openerpicture = bereiche[bereich][3];
			temp.mainbgcolor = bereiche[bereich][4];
			temp.maintxtcolor = bereiche[bereich][5];
			Page.themenbereiche.push(temp);
		}
	},
	
	changeMetaTitle  :function(newTitle) {
		Page.metatitle = newTitle;
		if(newTitle != "") {
			document.title = newTitle;
		}
	},

	changeMetaKeywords  :function(newKeywords) {
		Page.metakeywords = newTitle;	
	},

	changeMetaDesc  :function(newDesc) {
		Page.metadesc = newDesc;	
	},
	
	getSubnavi : function(bereichtoload) {
		if(anyAnimationShowing == false) {
			anyAnimationShowing = true;
			for(var bereich in Page.themenbereiche) {
				//if( Page.themenbereiche[bereich].title == bereichtoload) {
				if( Page.themenbereiche[bereich].id == bereichtoload) {
					currentBereichId = Page.themenbereiche[bereich].id;
					Page.themenbereiche[bereich].getSubnavi();
				}
			}
		
		}
	},
	
	getIntro : function(bereichtoload) {
		for(var bereich in Page.themenbereiche) {
			if( Page.themenbereiche[bereich].title == bereichtoload) {
				Page.themenbereiche[bereich].getIntro();
			}
		}		
	},
	
	getText : function(type, id) {
		if(anyAnimationShowing == false) {
			anyAnimationShowing = true;
		
		if(type == "static") {
			var url = "php/content/texts.php?ajaxcall&ajaxcallaction=getSText&id=" + id;		
		}
		else if(type == "dynamic") {
			var url = "php/content/texts.php?ajaxcall&ajaxcallaction=getDText&id=" + id;	
		}

		loadingBox.show();
		/*new Ajax.Request(url, {
			method: 'get',
			onSuccess: function(transport) {
				var contenttext = $('contenttext');
				if (transport.responseText != "") {
					new Effect.Fade(contenttext, {duration: 0.5, afterFinish: function() {
																	contenttext.innerHTML = transport.responseText;
																	new Effect.Appear(contenttext, {duration: 0.5});
																	hideLoader();
																}
												});
				}
			}
		});*/
		currentStep = currentStep+1;
		new Ajax.History.Request(url, {
			history : {
				id    : 'Counter',
				//state : id,
				cache : false,
				onStateChange: function(state) {
					// change title
					//History.setTitle(History.getTitle() + ' - Page Ajax #' + state);
				}
			},
			parameters: {id: id},
			onSuccess: function(transport, json) {
				deleteHighSlideContainer();
				removeResizeFixForGallery();
				var jsonobject = transport.responseText.evalJSON();
				
				var contenttext = $('contenttext');
				if (transport.responseText != "") {
					var myiframe = $$('iframe')[0];
					if(myiframe) {
						//myiframe.scrolling="no";
						myiframe.style.visibility = "hidden";
					}
					Effect.SlideUp(contenttext, {duration: 0.5, afterFinish: function() {
																			//$('contenttext').innerHTML = "<div style=\"padding: 20px;\">" + transport.responseText + "</div>";
																			
																			Page.changeMetaTitle(jsonobject.windowtitle);
																			
																			var htmltext = "";
																			$('contenttext').innerHTML = "";
																			htmltext += "<table cellpadding=\"0\" cellspacing=\"0\" style=\"width: 100%;\">";
																			htmltext += "<tr>";
																			htmltext += "<td style=\"width: 460px; padding: 30px;\" valign=\"top\" id=\"td1\">" + jsonobject.txt + "</td>"; //transport.responseText + "</td>";
																			var myBereich = Bereiche.getBereichById(currentBereichId);
																			htmltext+= "<td style=\"background-color: " + myBereich.mainbgcolor +  "\" valign=\"top\" class=\"chgbgcolor\" id=\"td2\"><div id=\"galleryholder\">&nbsp;</div></td>";
																			htmltext += "</tr>";
																			htmltext += "</table>";
																			$('contenttext').innerHTML = htmltext;
																			anyAnimationShowing = false;
																			Page.getImagesForText("dynamic", transport.request.parameters.id);
																				var myiframe = $$('iframe')[0];
																				if(myiframe) {
																					//myiframe.scrolling="no";
																					myiframe.style.visibility = "hidden";
																				}
																			Effect.SlideDown('contenttext', {duration: 0.5, afterFinish: function() {
																																	hideLoader();
																																	//$('contenttext').scrollTo();
																																	var myiframe = $$('iframe')[0];																				var myiframe = $$('iframe')[0];
																																	if(myiframe) {
																																		//myiframe.scrolling="no";
																																		myiframe.style.visibility = "";
																																	}

																																},
																																queue: 'end'
																															});
																		}
																	});
				}
			}
		});
		
		
		}
	
	},
	
	
	getImagesForText : function(type, id) {
		if(anyAnimationShowing == false) {
			anyAnimationShowing = true;
		
		if(type == "static") {
			var url = "php/content/images.php?ajaxcall&ajaxcallaction=getSText&id=" + id;		
		}
		else if(type == "dynamic") {
			var url = "php/content/images.php?ajaxcall&ajaxcallaction=getDText&id=" + id;	
		}

		loadingBox.show();
		//Stop(); //Stop Animation
		new Ajax.Request(url, {
			method: 'get',
			onSuccess: function(transport) {
				var contentbody = $('galleryholder');
				if (transport.responseText != "") {
					var checker = transport.responseText.split("|");
					if(checker[0] == "deletepic") {
						contentbody.hide();
						//new Effect.Fade(contentbody, {duration: 0.5, afterFinish: function() {
						//												contentbody.innerHTML = transport.responseText;
						//												new Effect.Appear(contentbody, {duration: 0.5});
						//												hideLoader();
						//											}
						//							});
						$("td1").setStyle("width: 100%;");
						$("td2").up().removeChild($("td2"));
						
						anyAnimationShowing = false;
					}
					else if(checker[0] == "nopic") {
						//new Effect.Fade(contentbody, {duration: 0.5, afterFinish: function() {
						//												contentbody.innerHTML = transport.responseText;
						//												new Effect.Appear(contentbody, {duration: 0.5});
						//												hideLoader();
						//											}
						//							});
						anyAnimationShowing = false;
					}
					else if(checker[0] == "firstpic") {
						new Effect.Fade(contentbody, {duration: 0.5, afterFinish: function() {
																		contentbody.innerHTML = checker[1];
																		new Effect.Appear(contentbody, {duration: 0.5});
																		loadingBox.hide();
																		anyAnimationShowing = false;
																	}
													});
					}
					else if(checker[0] == "inrow") {
						new Effect.Fade(contentbody, {duration: 0.5, afterFinish: function() {
																		contentbody.innerHTML = checker[1];
																		new Effect.Appear(contentbody, {duration: 0.5});
																		loadingBox.hide();
																		anyAnimationShowing = false;
																	}
													});
					}
					else if(checker[0] == "animation") {
						new Effect.Fade(contentbody, {duration: 0.5, afterFinish: function() {
																		contentbody.innerHTML = checker[1];
																		new Effect.Appear(contentbody, {duration: 0.5});
																		loadingBox.hide();
																		anyAnimationShowing = false;
																	}
													});
					}
					else if(checker[0] == "hiddenanimation") {
						new Effect.Fade(contentbody, {duration: 0.5, afterFinish: function() {
																		contentbody.innerHTML = checker[1];
																		new Effect.Appear(contentbody, {duration: 0.5});
																		loadingBox.hide();
																		anyAnimationShowing = false;
																	}
													});
					}
					else if(checker[0] == "inpageanimation") {
						new Effect.Fade(contentbody, {duration: 0.5, afterFinish: function() {
																		contentbody.innerHTML = checker[1];
																		new Effect.Appear(contentbody, {duration: 0.5});
																		loadingBox.hide();
																		window.setTimeout("applyResizeFixForGallery()", 100);
																		window.setTimeout("document.getElementById('hiddenthumb0').onclick()", 200);
																		anyAnimationShowing = false;
																	}
													});
					}
					else {
						//do nothing
						loadingBox.hide();
						anyAnimationShowing = false;
					}
				}
			}

		});
		/*currentStep = currentStep+1;
		new Ajax.History.Request(url, {
			history : {
				id    : 'Counter',
				//state : id,
				cache : false,
				onStateChange: function(state) {
					// change title
					//History.setTitle(History.getTitle() + ' - Page Ajax #' + state);
				}
			},
			onSuccess: function(transport) {
				var contenttext = $('contenttext');
				if (transport.responseText != "") {
					new Effect.Fade(contenttext, {duration: 0.5, afterFinish: function() {
																	contenttext.innerHTML = transport.responseText;
																	new Effect.Appear(contenttext, {duration: 0.5});
																	hideLoader();
																}
												});
				}
			}
		});*/
		
		}
	
	},

	
	loadedColorCss : null,
	UnloadPrevouisCssElem : function () {
		if(Page.loadedColorCss != null) {
			document.getElementsByTagName("head")[0].removeChild(Page.loadedColorCss);
		}
	}
};

Page.init();

