
//document.getElementsByTagName("body").item(0).style.cursor = "wait";

var req;
var js_dynamic = -1;
var str_js_object_list = "";
var obj_flash_content = new Object();
var str_flash_content = "";
var bln_is_flash = true;
var body_alpha = 0;
  
/* Page loading
===========================================================*/

function setAlpha(obj_ref, percentage) {
		if (obj_ref.filters && obj_ref.filters[0]) {
			if (typeof obj_ref.filters[0].opacity == "number") { //if IE6+
				obj_ref.filters[0].opacity = percentage;
			} else { //else if IE5.5-
				obj_ref.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+percentage+")";
			}
		} else if (obj_ref.style.MozOpacity) {
			obj_ref.style.MozOpacity = percentage / 101;
		} else if (obj_ref.style.KhtmlOpacity) {
			obj_ref.style.KhtmlOpacity = percentage / 100;
		}
}
	
// <div style="zoom: 1; margin-top: 0px; filter:progid:DXImageTransform.Microsoft.Alpha(opacity=100);-moz-opacity:100;-khtml-opacity:100;" 

function loadXMLDoc(url) 
{        

    // Kill cache
    var ck = Math.round(Math.random() * 8999)+1000;
    if (url.split("?").length < 2){
        url+="?ck=" + ck;
    } else {
        url+="&ck=" + ck;
    }   
	
	//prompt("?" , url);   
	
    if (window.XMLHttpRequest) {
        // branch for native XMLHttpRequest object
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);        
    } else if (window.ActiveXObject) {
        // branch for IE/Windows ActiveX version
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    } else {
        // Error
        redirectToStaticSite();    
    }
}
    
	
function processReqChange() 
{
    // only if req shows "complete"
    if (req.readyState == 4) {
    
        // only if "OK"
        if (req.status == 200) {
        
            //var xml_response = req.responseXML.documentElement;            
            // convert the string to an XML object
            var str_response_text = req.responseText;        
            var xml_response;
            
            if (window.ActiveXObject)
            {
                
				// code for IE
                xml_response= new ActiveXObject("Microsoft.XMLDOM");
                xml_response.async="false";
                xml_response.loadXML(str_response_text);
				
            } else {
			    
				// code for Mozilla, Firefox, Opera, etc.   
                var parser=new DOMParser();
                xml_response=parser.parseFromString(str_response_text,"text/xml");
				
            }            
			// Reset the pagination current page
			int_currently_visible_pagination_div = 1; 
			            
            //var str_success = String(xml_response.getElementsByTagName('response')[0].attributes["success"].nodeValue);
			
			var node_response = xml_response.getElementsByTagName('response')[0];
			
			var str_success = getAttributeVal(node_response, "success");
			
			if (str_success != "1" ){
				 redirectToStaticSite();    
				 return;
			}
			
			//="content/background-script.js" ="test12323423423423,temp_function,populateSideImageWithRandomPic"
            // Set body text -  
            //if (e("body-content-div")){       

				// Unload of javscript objects if any
				if (js_dynamic != -1){
					document.getElementsByTagName("head")[0].removeChild(js_dynamic);
					js_dynamic = -1;
					if (str_js_object_list.length > 1){
						var arr_del_js_objects = str_js_object_list.split(",");
						for( var i = 0; i < arr_del_js_objects.length ; i++){
							if (arr_del_js_objects[i] != "e"){   
								var tmp_js_obj = eval("window." + arr_del_js_objects[i] );
								if (tmp_js_obj){ 
									if (typeof(tmp_js_obj) == "function"){			
										eval("window." + arr_del_js_objects[i] + "=null" );
										//tmp_js_obj = null;					
									}
								}
							
							}
						}
					}
					str_js_object_list = "";
				}

				var bln_any_bodycontent = false;
				
				if (getAttributeVal(node_response, "any_bodycontent") == "1"){
					bln_any_bodycontent = true;
				}
				
				obj_flash_content = new Object();
				//flash_swf_path="image-gallery.swf" flash_xml_path="image_gallery-COMP071155AMPbTqfA.xml" flash_x="0" flash_y="0" flash_embed="1"
				if (getAttributeVal(node_response, "flash_embed") == "1"){
					obj_flash_content["flash_embed"]=true;
					obj_flash_content["flash_swf_path"]=getAttributeVal(node_response, "flash_swf_path");
					obj_flash_content["flash_xml_path"]=getAttributeVal(node_response, "flash_xml_path");
					obj_flash_content["flash_x"]=getAttributeVal(node_response, "flash_x");
					obj_flash_content["flash_y"]=getAttributeVal(node_response, "flash_y");
				} else {
					obj_flash_content["flash_embed"]=false;	
				}
				
				if (!bln_any_bodycontent){
					// Tell flash content is revealed.
					// alert("no html content");
					//onPageContentRevealed();	
					onPageContentLoaded(false);	
					return;
				}

				// Get body content
				var str_bodytext = xml_response.getElementsByTagName('bodytext')[0].firstChild.data;
												
				// Load content
                e("body-content-div").innerHTML = str_bodytext;  		
				
				var str_bln_jsfile = getAttributeVal(node_response, "bln_jsfile"); 
				
				if (str_bln_jsfile == "1" ){
				
					var str_jsfile_path = getAttributeVal(node_response, "jsfile_path");  //String(xml_response.getElementsByTagName('response')[0].attributes["jsfile_path"].nodeValue);
					str_js_object_list = "";
					str_js_object_list = getAttributeVal(node_response, "js_objects"); //String(xml_response.getElementsByTagName('response')[0].attributes["js_objects"].nodeValue);
					
					js_dynamic = document.createElement("script");
					js_dynamic.exists = true;
					js_dynamic.setAttribute("type", "text/javascript");
					js_dynamic.setAttribute("src", str_jsfile_path);
					document.getElementsByTagName("head")[0].appendChild(js_dynamic);
									
				}
					
			// Make callback			
			onPageContentLoaded(true);
						            
        } else {
            // Error
            redirectToStaticSite();    
        }
    } 
}    

//function fixSafari(){
	//alert("Safari");	
//}


function getAttributeVal(node, str_attribute_name){				
		var str_attribute_val = "";
		for( var x = 0; x < node.attributes.length; x++ ) {
			  if( node.attributes[x].nodeName.toLowerCase() == str_attribute_name ) {
				str_attribute_val = node.attributes[x].nodeValue;
				break;
			  }
		}
		return str_attribute_val;
}

// ----------------------------------------------------------------------------------

var bln_isthere_html_content = false;
function onPageContentLoaded(bln_html_content){
	bln_isthere_html_content = bln_html_content;
	// Check if flash content is required.
	//var bln_flash_content = false;
	if (bln_html_content){
		body_alpha = 0;
		if (bln_is_IE6){			  
			e("body-content-container-div").className="hide";
		} else {
			e("body-content-container-div").className="show";
			if (e("body-content-div")){
				setAlpha(e("body-content-div") , body_alpha );
			}
		}
		//fadeInContent();
		onPageContentLoaded__pre2();
	} else {
		onPageContentLoaded__pre2();
	}
}

function onPageContentLoaded__pre2(){
	// prop_one_~_prop_val___prop_two_~_val_two
	var $str_flash_content = "";
	for (var p in obj_flash_content){
			if ($str_flash_content.length > 0){
				$str_flash_content+="___";
			} 
			$str_flash_content+=p+"_~_"+obj_flash_content[p];
	}
	str_flash_content = "";
	str_flash_content = $str_flash_content ;
	setTimeout("onPageContentLoaded__2()",5);	
} 

function onPageContentLoaded__2(){
	if (getFlashObjectById('main_flash')){
		getFlashObjectById("main_flash").onPageContentLoaded__js_to_flash(str_flash_content,bln_isthere_html_content);
	}
}

function loadPageByUid(str_page_uid){
	var obj_page_request = new Object();
	obj_page_request["page_uid"] = str_page_uid;	
	getFlashObjectById("main_flash").loadPage__js_to_flash(obj_page_request);	
}

function revealContent__flash_to_js(){
	if (bln_is_IE6){		
		e("body-content-container-div").className="show";
		onPageContentRevealed();
		if (bln_is_IE6){
			 setTimeout("processAlphaPNGSforIE6()",3);
		}
	} else {
		fadeInContent();   
	}
}

function fadeInContent(){
	body_alpha+=3;
	if (body_alpha > 100){
		body_alpha = 100;
	}
	setAlpha(e("body-content-div") , body_alpha );
	if (body_alpha < 100){
		setTimeout("fadeInContent()",3);
	} else {
		onPageContentRevealed();
	}
}



function onPageContentRevealed(){
	if (getFlashObjectById('main_flash')){
		getFlashObjectById("main_flash").onPageContentRevealed__js_to_flash();
	}
}

function redirectToStaticSite(){
	//alert("e r r o r");
    window.location = "static/";
}

function getFlashObjectById(str_id){
	if(navigator.appName.indexOf("Microsoft") != -1) {
		obj_flash = window[str_id];
	}else {
		obj_flash = window.document[str_id];
	}
	return obj_flash;
}

// FUNCTION CALLS FROM FLASH :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

function loadPageContent__flash_to_js(obj_page_request){
	loadXMLDoc("GetPageContent.aspx?page="+obj_page_request["page_uid"]+"&force_update=0" );
}

function hidePageContent__flash_to_js(){ 	
	fadeOutContent(["onPageContentHidden"]);
}

function onPageContentHidden(){
	e("body-content-container-div").className="hide";
	setTimeout("getFlashObjectById('main_flash').onPageContentHidden__js_to_flash()" , 10); 
}

cb_arr__fadeOutContent = new Array();
function fadeOutContent(cb_arr){
	// Save callback
	for (var i = 0 ; i < cb_arr.length ; i++){
		cb_arr__fadeOutContent[i] = cb_arr[i];
	}
	// If already hidden return;	
	if (!bln_isthere_html_content){
		makeCallBack(cb_arr__fadeOutContent);
		return;
	}
	
	if (bln_is_IE6){	
		// Simple flick
		e("body-content-container-div").className="hide";
		makeCallBack(cb_arr__fadeOutContent);
	} else {
		body_alpha = 100;
		fadeOutContentLoop();
	}
	
}


function onFadeOutDone(){
	e("body-content-container-div").className="hide";
	makeCallBack(cb_arr__fadeOutContent);
}

function fadeOutContentLoop(){
	body_alpha-=10;	
	if (body_alpha < 0){
		body_alpha = 0;
	}
	setAlpha(e("body-content-div") , body_alpha );
	if (body_alpha > 0){
		setTimeout("fadeOutContentLoop()",3);
	} else {
		onFadeOutDone()
		//getFlashObjectById("main_flash").onPageContentRevealed(true);
	}
}


cb_arr__fadeInContent = new Array();

function makeCallBack(cb_arr){
	if (cb_arr.length < 1 ){
		return;
	} else if (cb_arr.length == 1) {
		cb_arr[1] = "";
	}
	eval(cb_arr[0] + "(" + cb_arr[1]+ ")");
}


function setPageTitleAndHashAddress__flash_to_js(str_page_title, str_page_uid){
	
	
	
	if (bln_hash_history_not_iframes){
		var str_hash = String(window.location.hash);
		if (str_hash.split("#").join("") == str_page_uid){
			//alert("HASH LEFT");
		} else {	
			bln_ignore_next_hash_change = true;
			window.location.hash = str_page_uid;
		}
	} else {
		window.location.hash = str_page_uid;
	}
	setTimeout("setPageTitle(\"" +str_page_title+"\")" , 100);
}
 
function setPageTitle(str_title){
	document.title="Meridien - " + str_title;
}


function savePageRequestStringToHistory__flash_to_js(str_page_request_string, str_connection_id, str_page_title){
	if (e("history_frame")){
		e("history_frame").src = "History.aspx?remote_connection_id=" + str_connection_id + "&page_request_string=" + str_page_request_string + "&str_title=" + str_page_title;
	}
}


if (bln_hash_history_not_iframes){

	var str_last_hash = "";
	var bln_ignore_next_hash_change = true;
	var int_timeout_id = -1;
	function checkForHistoryHashChange(){
		var str_current_hash = window.location.hash
		if (str_current_hash != str_last_hash){
			if (bln_ignore_next_hash_change){
				bln_ignore_next_hash_change = false;								
			} else {
				//clearInterval(int_timeout_id);
				// Format : "prop_one_~_prop_val___prop_two_~_val_two"	
				getFlashObjectById("main_flash").loadPageFromHistory__js_to_flash("page_uid_~_" + str_current_hash.split("#").join("") );					
			}
			str_last_hash = str_current_hash;
		}		
	}
	
	int_timeout_id = setInterval("checkForHistoryHashChange()" , 500);

}

// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

 /**
 * Sets a Cookie with the given name and value.
 *
 * name Name of the cookie
 * value Value of the cookie
 * [expires] Expiration date of the cookie (default: end of current session)
 * [path] Path where the cookie is valid (default: path of calling document)
 * [domain] Domain where the cookie is valid
 * (default: domain of calling document)
 * [secure] Boolean value indicating if the cookie transmission requires a
 * secure transmission
 */

 function SetCookie(name, value, expires, path, domain, secure)
 {

 var expires = new Date();
 expires.setYear(3000);

 document.cookie= name + "=" + escape(value) +
 ((true) ? "; expires=" + expires : "") +
 ((path) ? "; path=" + path : "") +
 ((domain) ? "; domain=" + domain : "") +
 ((secure) ? "; secure" : "");
 }
 //alert(String("25/12/3000").toGMTString()) ;
 /**
 * Gets the value of the specified cookie.
 *
 * name Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 * or null if cookie does not exist.
 */
 function GetCookie(name)
{
 var dc = document.cookie;
 var prefix = name + "=";
 var begin = dc.indexOf("; " + prefix);
if (begin == -1)
 {
 begin = dc.indexOf(prefix);
 if (begin != 0) return null;
 }
 else
 {
 begin += 2;
 }
 var end = document.cookie.indexOf(";", begin);
 if (end == -1)
 {
 end = dc.length;
 }
 return unescape(dc.substring(begin + prefix.length, end));
 }

 /**
 * Deletes the specified cookie.
 *
 * name name of the cookie
 * [path] path of the cookie (must be same as path used to create cookie)
 * [domain] domain of the cookie (must be same as domain used to create cookie)
 */
 function deleteCookie(name, path, domain)
 {
 if (GetCookie(name))
 {
 document.cookie = name + "=" +
 ((path) ? "; path=" + path : "") +
 ((domain) ? "; domain=" + domain : "") +
 "; expires=Thu, 01-Jan-70 00:00:01 GMT";
 }
 }

 // ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


