var isIE = ( navigator.userAgent.indexOf("MSIE") != -1 );
var isIE = /MSIE ((5\.5)|[6789])/.test(navigator.userAgent) &&
				navigator.platform == "Win32";

var selfClosingTags = /br|img|hr|div/;
var condenseWhite   = /[\s]+/g;
var trimWhite       = /(^\s|\s$)+/g;
var CSSobj = [];

getCSS();

var flashEnabled = true;
//var flashEnabled = false;

function initScroll(){
	
	
}


function loadShadowBox(){
	
	Shadowbox.loadSkin('classic', 'js/shadowbox-2.0/src/skin'); // use the "classic" skin
	Shadowbox.loadLanguage('en', 'js/shadowbox-2.0/src/lang'); // use the English language
	Shadowbox.loadPlayer(['img', 'qt', 'iframe'], 'js/shadowbox-2.0/src/player'); // use 
}

function initStart(){
	
	
	//PNGFix();
	
	Shadowbox.init();
	
	var title_FO = xGetElementById("title_FO");
	var top_menu_FO = xGetElementById("top_menu");
	
	if (flashEnabled == true){
		
		if(top_menu_FO != null){
			initFlashTM();
		}
		initFlashLogo();
		initFlashHero();
		
		if(title_FO != null){
			normalizeTitle();
			initFlashTitle();
		}
		
	}else{
		appearState('top_menu');
		
		if(title_FO != null){
			appearState(title_FO);
			normalizeTitle();
		}
		
	}
	
	appearState("hero_FPO");
	
	updateCopy();
}


function initStartSub(fpo){
	
	if(fpo){
	
		if (flashEnabled == false){
			appearState('fpoContainer');
			initScroll();
		}else{
			preloadingState('fpoContainer');
			initFlashArea();
		}
		
	}else{
		appearState('fpoContainer');
		initScroll();
	}
	
}


function getSource( elem ) {

	if( elem.nodeType == 3 ) {
		return elem.nodeValue.replace(condenseWhite,' ').replace(trimWhite,'');
	} else if ( elem.nodeType != 1 ) {
		return "";
	}

	var name = elem.nodeName.toLowerCase();
	var str = "<" + name;

	var a = elem.attributes;
	var i = 0;
	var attr, attrn, attrv;
	while( attr = a[i++] ) {
		attrn = attr.nodeName;
		attrv = attr.nodeValue;
		if( !attr.specified || attrv == '' ) continue;
		if( isIE && attrn == 'style' ) {
			str += ' style="' + elem.style.cssText.toLowerCase() + '"';
		} else {
			str += ' ' + attrn + '="' + attrv + '"';
		}
	} 
	
	if( !elem.hasChildNodes() ) {
	if ( selfClosingTags.test(name) ) {
			str += ' />';
		} else {
			str += '>';
		}
	} else {
		str += '>'
		var child = elem.firstChild;
		for( child; child != null; child = child.nextSibling ) {
			str += getSource( child );
			getSource( child );
		}
		str += '</' + name + '>';
	}
	
	return str;

}

function getSourceById( id ) {
	return getSource( document.getElementById( id ) );
}

function addFlash( elementID, swfSrc, swfID, swfWidth, swfHeight, swfVersion, swfColor ) {
	var fo = new FlashObject( swfSrc, swfID, swfWidth, swfHeight, swfVersion, swfColor );
	fo.addVariable( "source", encodeURIComponent( getSourceById( elementID ) ) );
	fo.write( elementID );
}




function PNGFix() {
	if( !isIE ) return;
	var l = document.images.length;
	for( var i = 0; i<l; i++ ) {
		fixImage( document.images[i] );
	}
}

function fixImage( element ) {
	var realSrc;
	var blankSrc = "images/blank.gif";
	
	// get src
	var src = element.src;

	// check for real change
	if (src == realSrc && /\.png$/i.test(src)) {
		element.src = blankSrc;
		return;
	}

	if ( ! new RegExp(blankSrc).test(src)) {
		// backup old src
		realSrc = src;
	}

	// test for png
	if (/\.png$/i.test(realSrc)) {
		// set blank image
		element.src = blankSrc;
		// set filter
		element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft." +
					"AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
	}
	else {
		// remove filter
		element.runtimeStyle.filter = "";
	}
}
function MM_openBrWindow(theURL,winName,features) { 
  window.open(theURL,winName,features);
}

function xGetElementById(e){
	if(typeof(e)!='string') return e;
	if(document.getElementById) e=document.getElementById(e);
	else if(document.all) e=document.all[e];
	else e=null;
	return e;
}




function trimCondence(stringValue){
	stringValue.replace(condenseWhite,' ').replace(trimWhite,'');
	
	return stringValue;
}


function normalizeAttributes(source){
	
	source = source.split('<SCRIPT type=text/javascript>').join('<SCRIPT type="text/javascript">');
	
		
	///Corect one!!!!
	//var regExp = /(\w+)=([a-zA-Z0-9_\[\]]+)/g;
	//source = source.replace(regExp, "$1=\"$2\"");
	
	var regExp = /<([^>]+)/g;
	
	source = source.replace(regExp,
		function (p1){
			//var regExp2 = /(\s\w+)=([a-zA-Z0-9_\[\]]+)/g;
			//var regExp2 = /(\s\w+)=([a-zA-Z0-9_\.\?\&\[\]]+)/g;
			//var regExp2 = /(\s\w+)=([a-zA-Z0-9_\.\?\&\=\%\[\]]+)/g;
			//var regExp2 = /(\s\w+)=([a-zA-Z0-9_\.\?\&\=\%\!\[\]]+)/g;
			var regExp2 = /(\s\w+)=([^ \f\n\r\t\v"]+)/g;
			return p1.replace(regExp2, "$1=\"$2\"");
		});
	
	
	
	
	//var regExp = /(\w+)=(\w+)/g;
	
	//var re = /(<[^>]*)(\w+)=([^\s">]+)([^<]*>)/g;
	//source = source.replace(re, "$1$2=\"$3\"$4"); 

	
	source = source.replace(/<DIV/g,'<div')
	source = source.replace(/<\/DIV/g,'</div')
	source = source.replace(/<ACTION/g,'<action')
	source = source.replace(/<\/ACTION/g,'</action')
	source = source.replace(/<A/g,'<a')
	source = source.replace(/<\/A/g,'</a')
	source = source.replace(/<SCRIPT/g,'<script')
	source = source.replace(/<\/SCRIPT/g,'</script')
	source = source.replace(/<LI/g,'<li')
	source = source.replace(/<\/LI/g,'</li')
	source = source.replace(/<UL/g,'<ul')
	source = source.replace(/<\/UL/g,'</ul')
	source = source.replace(/<P/g,'<p')
	source = source.replace(/<\/P/g,'</p')
	source = source.replace(/<H/g,'<h')
	source = source.replace(/<\/H/g,'</h')
	source = source.replace(/<SPAN/g,'<span')
	source = source.replace(/<\/SPAN/g,'</span')
	source = source.replace(/<IMG/g,'<img')
	source = source.replace(/<BR> /g,'<br>')
	source = source.replace(/<BR>/g,'<br>')
	source = source.replace(/<br> /g,'<br>')
	source = source.replace(/<br>/g,'<br />')
	source = source.replace(/<INPUT /g,'<input')
	source = source.replace(/<BOX>/g,'<box>')
	source = source.replace(/<\/BOX>/g,'</box>')
	source = source.replace(/<CUS>/g,'<cus>')
	source = source.replace(/<\/CUS>/g,'</cus>')
	
	var regExp2 = /style="([^">]+)/g;
	source = source.replace(regExp2,function (p1,p2){return p1.toLowerCase()});
	
	if(is_ie5up){
		var regExp2b = /style="([^"]+)/g;
		source = source.replace(regExp2b, 'style="$1;');
	}
	
	
	var regExp3 = /<img([^>]+)/g;
	source = source.replace(regExp3, '<img$1 /');
	
	var regExp4 = /<input([^>]+)/g;
	source = source.replace(regExp4, '<input $1 /');
	
	if(is_ie){
	// IE only
		var reg = /\<li\>(.+)[^(\<\/li\>)]\<li\>/gim;   
		while(source.search(reg)!=(-1)){  
			source = source.replace(reg,'<li>$1<\/li><li>');  
		}   
	}

	
	return source;
}


function preloadingState(obj){
	fpo = xGetElementById(obj);
	fpo.style['visibility'] = 'hidden';
	fpo.style['display'] = 'none';
}

function appearState(obj){
	fpo = xGetElementById(obj);
	fpo.style['visibility'] = 'visible';
	fpo.style['display'] = 'block';
}


function initFlashTM(){
	
	var elementID = "top_menu";
	var fo = new FlashObject( "flash/top_menu.swf", "top_menu", "725", "45", "8");

	var top_menu_data = xGetElementById("top_menu");
	
	var source = top_menu_data.innerHTML.split("+").join("&#43;");
	source = removeBreaks(source);
	source = trimCondence(source);
	
	source = normalizeAttributes(source);
	
	//source = '<div id="top_menu">'+ source +'</div>';
	source = encodeURIComponent(source);
	
	
	
	fo.addVariable("htmlSource", source );
	fo.addVariable("local", "false" );
	fo.addParam("wmode", "transparent");

	
	var hasFlash = fo.write( elementID );
	
	

	if (hasFlash == false){
		appearState(elementID);
	}
	
	appearState(elementID);
	
	
	
}


function initFlashLogo(){

		var elementID = "logo_FO";
		var fo = new FlashObject( "flash/logo.swf", "logo_FO", "160", "122", "8");
	
		var logo_data = xGetElementById("logo_FO");
		
		var source = logo_data.innerHTML.split("+").join("&#43;");
		source = removeBreaks(source);
		source = trimCondence(source);
		
		source = normalizeAttributes(source);
		
		//source = '<div id="top_menu">'+ source +'</div>';
		source = encodeURIComponent(source);
		
		fo.addVariable("htmlSource", source );
		fo.addVariable("local", "false" );
		fo.addParam("wmode", "transparent");
	
		
		var hasFlash = fo.write( elementID );

		if (hasFlash == false){
			appearState(elementID);
		}
}

function initFlashTitle(){

		var elementID = "title_FO";
		var fo = new FlashObject( "flash/title.swf", "title_FO", "630", "67", "8");
	
		var title_data = xGetElementById("title_FO");
		
		var source = title_data.innerHTML.split("+").join("&#43;");
		source = removeBreaks(source);
		source = trimCondence(source);
		
		source = normalizeAttributes(source);
		
		//alert(source);
		
		//source = '<div id="top_menu">'+ source +'</div>';
		source = encodeURIComponent(source);
		
		
		fo.addVariable("htmlSource", source );
		fo.addVariable("local", "false" );
		fo.addParam("wmode", "transparent");
	
		
		var hasFlash = fo.write( elementID );

		appearState(elementID);
		
		
}


function initFlashHero(){
	
	
	var elementID = "hero_FPO";
	var hero_area = xGetElementById(elementID);
	
	var type = hero_area.className;
	
	var sizes = new Array();
	
	sizes['home'] = new Array();
	sizes['home']['width'] = 725;
	sizes['home']['height'] = 362;
	
	sizes['sub'] = new Array();
	sizes['sub']['width'] = 688;
	sizes['sub']['height'] = 277;
	
	var fo = new FlashObject( "flash/hero.swf", "hero_FPO", sizes[type]['width'], sizes[type]['height'], "8");

	
	var source = hero_area.innerHTML.split("+").join("&#43;");
	source = removeBreaks(source);
	source = trimCondence(source);
	
	source = normalizeAttributes(source);
	
	//alert(type);
	
	source = '<div id="' + type + '">'+ source +'</div>';
	source = encodeURIComponent(source);
	
	fo.addVariable("htmlSource", source );
	fo.addVariable("local", "false" );
	fo.addParam("wmode", "transparent");

	
	var hasFlash = fo.write( elementID );

	if (hasFlash == false){
		appearState(elementID)
	}
	
}

function initFlashArea(){
	
	var cnt = xGetElementById("content");
	var cntName = cnt.className;
	
	//alert(content.style['width']);
	//#siteFrame #container #fpoContainer
	var w = CSSobj['#siteFrame #container #fpoContainer .' + cntName].width;
	w = w.replace(/px/g,'');
	
	var h = CSSobj['#siteFrame #container #fpoContainer .' + cntName].height;
	h = h.replace(/px/g,'');
	
	if(is_safari){
		h = h-4;
	}
	
	if(is_ff){
		h = h-18;
	}
	
		
	var elementID = "fpoContainer";
	var fo = new FlashObject( "flash/flash_area.swf", "flash_area", w, h, "8");

	var fpoContainer = xGetElementById(elementID);
	
	var source = fpoContainer.innerHTML.split("+").join("&#43;");
	source = removeBreaks(source);
	source = trimCondence(source);
	
	source = normalizeAttributes(source);
	

	//source = '<div id="flash_area">'+ source +'</div>';
	source = encodeURIComponent(source);
	
	fo.addVariable("htmlSource", source );
	fo.addVariable("local", "false" );
	fo.addParam("wmode", "transparent");

	
	var hasFlash = fo.write( elementID );

	if (hasFlash == false){
		initScroll();
		//preloadedFPO();
	}
	
	appearState('fpoContainer');
	
}





function removeBreaks(noBreaksText){

	noBreaksText = noBreaksText.replace(/(\n\r|\n|\r)/gm,"<1br />");
	
	re1 = /<1br \/><1br \/>/gi;
	re1a = /<1br \/><1br \/><1br \/>/gi;
	
	noBreaksText = noBreaksText.replace(re1," ");
	
	//noBreaksText = noBreaksText.replace(re1a,"<1br /><2br />");
	//noBreaksText = noBreaksText.replace(re1,"<2br />");
	
	re2 = /\<1br \/>/gi;
	noBreaksText = noBreaksText.replace(re2, " ");
	
	re3 = /\s+/g;
	noBreaksText = noBreaksText.replace(re3," ");
	
	re4 = /<2br \/>/gi;
	noBreaksText = noBreaksText.replace(re4,"\n\n");
	
	return noBreaksText;
}





function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_showHideLayers() { //v3.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v; }
}

function updateCopy(){
	
	var today=new Date();
	var todayy=today.getFullYear();
	
	var copyElement = xGetElementById("copy");
	
	copyElement.innerHTML = '&copy; ' + todayy + ' M Resort, LLC. ALL RIGHTS RESERVED';
	
}


function normalizeTitle(){
	
	var title_FO = xGetElementById("title_FO");
	
	var source = title_FO.innerHTML.split("+").join("&#43;");
	source = removeBreaks(source);
	source = trimCondence(source);
	
	source = normalizeAttributes(source);
	
	var outSource = source;
	
	var has_iMagine = (outSource.indexOf('iMagine') != -1);
	var has_AboutM = (outSource.indexOf('About M') != -1);
	
	if(has_iMagine){
		outSource = outSource.replace(/iMagine/g,'i</box><box><img src="images/logo_title.png" width="41" height="44" hspace="0" vspace="0" border="0" style="margin-left:-14px; margin-right:0px; margin-top:-12px; margin-bottom:-6px;"></box><box>agine');	
	}
	
	if(has_AboutM){
		outSource = outSource.replace(/About M/g,'About </box><box><img src="images/logo_title.png" width="41" height="44" hspace="0" vspace="0" border="0" style="margin-left:-14px; margin-right:0px; margin-top:-12px; margin-bottom:-6px;"></box><box>');	
	}
	
	outSource = outSource.replace(/<br \/>/g,'</box><box><br /></box><box>');
	outSource = '<box>' + outSource + '</box>';
	
	//alert(outSource);
	//<box>i</box><box><img src="images/logo_title.png" width="41" height="44" hspace="0" vspace="0" border="0" style="margin-left:-14px; margin-right:0px; margin-top:-12px; margin-bottom:-6px;" /></box><box>agine the Most Innovative New Club in</box><box><br /></box><box>Las Vegas.</box>
	
	//<box>Re-i</box><box><img src="images/logo_title.png" width="41" height="44" hspace="0" vspace="0" border="0" style="margin-left:-14px; margin-right:0px; margin-top:-12px; margin-bottom:-6px;" /></box><box>agine Gaming Excitement.</box>
	
	title_FO.innerHTML = '&nbsp;' + outSource;
	
	//alert(title_FO.innerHTML);
}



function submitContactUsForm(){
	var oForm = document.getElementById('send');

	if(oForm.name.value == ''){
		alert("Please enter your name");
		oForm.name.focus();
		return;
	}
	if(oForm.address.value == ''){
		alert("Please enter your address");
		oForm.address.focus();
		return;
	}
	/*
	if(oForm.address2.value == ''){
		alert("Please enter your address 2");
		oForm.address2.focus();
		return;
	}
	*/
	if(oForm.city.value == ''){
		alert("Please enter your city");
		oForm.city.focus();
		return;
	}
	if(oForm.state.value == ''){
		alert("Please enter your state");
		oForm.state.focus();
		return;
	}
	/*if(oForm.zip.value == ''){
		alert("Please enter your zip. e.g. 08400");
		oForm.zip.focus();
		return;
	}*/
	if(!oForm.email.value.match(/^.+@[^\.].*\.[a-z]{2,}$/)){
		alert("Please enter your email");
		oForm.email.focus();
		return;
	}
	if(oForm.phone.value.match(/[a-z]/)){
		alert("Please enter your phone. e.g. 123-123-1234");
		oForm.phone.focus();
		return;
	}

	/*
	if(oForm.question.value == ''){
		alert("Please enter your question");
		oForm.question.focus();
		return;
	}
	*/
	
	oForm.action = 'sendContactUs.php';
	oForm.submit();
}



function submitReservationsForm(){
	var oForm = document.getElementById('reservationsForm');

	if(oForm.arrive.value == '' || oForm.arrive.value == 'GROUP CODE'){
		alert("Please enter the date");
		oForm.arrive.focus();
		return;
	}
	
	
	
	if(Date.isValid(oForm.arrive.value,'') == false){
		alert("Please enter the correct date");
		oForm.arrive.focus();
		return;
	}
	
	if(oForm.Group.value == 'GROUP CODE'){
		oForm.Group.value = '';
	}
	
	
	
	oForm.action = 'https://reservations.synxis.com/gc/rez.aspx';
	oForm.submit();
}


function redirecting(url){
	//alert(url);
	location.href = url;
	
}

function getCSS(){
	
	var ruleList;

	for(var i=0; i< document.styleSheets.length; i++){
		
		if(is_ie){
			ruleList = document.styleSheets[i].rules;
		}else{
			ruleList = document.styleSheets[i].cssRules;
		}
		
		for(var j=0; j< ruleList.length; j++){
			
			if(ruleList[j].style != undefined){
				CSSobj[ruleList[j].selectorText] = ruleList[j].style;
			}
			
		}
		
	}

	
	//alert(CSSobj['body, html'].margin);
	
}


function reservationGroupFocus(){
	var Group = xGetElementById("Group");
	if(Group.value == 'GROUP CODE'){
		Group.value = '';
	}
}

function reservationGroupBlur(){
	var Group = xGetElementById("Group");
	if(Group.value == ''){
		Group.value = 'GROUP CODE';
	}
}

function reservationDateFocus(){
	var arrive = xGetElementById("arrive");
	if(arrive.value == 'MM/DD/YYYY'){
		arrive.value = '';
	}
}

function reservationDateBlur(){
	var arrive = xGetElementById("arrive");
	if(arrive.value == ''){
		arrive.value = 'MM/DD/YYYY';
	}
}

