window.onload =	attachEvents;

function $( idSender ){
	return document.getElementById( idSender );
}

function $className( className ){
	return	document.getElementById( className );
}

function $tagName( tagName ){
	return document.getElementsByTagName( tagName );
}

function getAllbyTagAndId( name , id ){
	return $( id ).getElementsByTagName( name ); 
}

function attachEvents(){
  	  setPage();
	  new createClassEvents("a-login-signin-btn");
	  new createClassEvents("a-login-reg-btn");
	  new createClassEvents("a-register");
	  attachVideo();
	  attachChooseVideo();
	  loginEvents();
  	  termsPrivacy();
	  videoClose();
	  doHomeFlashPromos();
	  attachGame();
      downloadPage();
	  magregister();
	  maglogin();
	  chooseCountry();
}

function loginEvents(){
	if( $("a-login") ){
	     $("a-login").onclick = function(){
		doInitLoginForm();
	    }
	}
}

function attachGame(){
	if( $("a-home-promo-secret") ){
		$("a-home-promo-secret").onclick = function() { secretGame(); }
	}	

	if( $("a-fun-game-beauty") ){
		$("a-fun-game-beauty").onclick = function() { secretGame(); }
	}

	if( $("a-fun-game-moxifier") ){
		$("a-fun-game-moxifier").onclick = function() { moxifierGame(); }
	}
	
	if( $("a-fun-game-weathergirl") ){
		$("a-fun-game-weathergirl").onclick = function() { weatherGirlGame(); }
	}

	if( $("a-fun-game-moxiesalon") ){
		$("a-fun-game-moxiesalon").onclick = function() { secretGameMoxieSalon(); }
	}
}


function createClassEvents( className ){
    var a_func;
    a_func = new Array();
    a_func['a-login-signin-btn'] = loginEvent;
    a_func['a-login-reg-btn'] = registerEvent;
    a_func['a-register'] = 	registerEvent;
    if ($className( className )) {
        a_func[className]($className( className ));
    }
}

function loginEvent(obj){
		obj.onclick = function(){ showDialogBox(); doInitLoginForm();  } 
}

function registerEvent(obj){
        obj.onclick = function(){ if(o_acctutil){ showDialogBox(); doInitRegForm(); }  }
}


function magregister(){
	if( s_magregister !== "" ){
		if(o_acctutil){ showDialogBox(); doInitRegForm(); }
	}
}

function maglogin(){
	if( s_maglogin !== "" ){
		showDialogBox(); doInitLoginForm();
	}
}

var attachVideo = function (){
	for (var i=0; i < 6; i++ ){
		if( $("a-video-" + video_list[i].element) ){
			bindVideo( $("a-video-" + video_list[i].element),i );
		}
	}
}

var bindVideo = function(obj,i){
	obj.onclick = function(){
		playVideo(video_list[i].path);
	}
}


function playVideo( uri ){
	var flashvars = { 
		file:uri,
		dock:'false', 
		autostart:'true'
	};
	var params = {
		allowscriptaccess:'always',
		allowfullscreen:'true',
		WMODE:'transparent'
		};
	var attributes = {};
	swfobject.embedSWF("smarty_files/templates/flash/player.swf" , "div-video-content" , "394" , "394" , "9.0.0", "", flashvars, params, attributes);
}

function attachChooseVideo(){
	if( $("select-video") ){
		$("select-video").onchange = function(){
			playVideo( this.value );
		}
	}
}


function videoClose(){ 
	if($("close-video")){
		$("close-video").onclick = function(){ 
			this.style.display = "none";
			$("div-ctrlcover").style.display = "none";
			$("div-ctrldialog").style.display = "none";
			$("div-ctrldialog").innerHTML = "";
		} 
	}
}




function showDialogBox(){
	$("div-ctrldialog").style.display = "block";
	$("div-ctrldialog").innerHTML = "<h3>Loading registration..........</h3>";

}

function chooseCountry(){
	$("a-country").onclick = function(){ o_acctutil.doUserLangInterface(); }
}

function downloadPage(){
   var atags = document.getElementsByTagName("a");
	for( var i = 0; i < atags.length; i++  ){
                var regular = /\d+/;
 		if(atags[i].id.match(regular)){
	   attachDownloadManager( atags[i] ); 
	}
    }	

}

function attachDownloadManager( obj ){
	obj.onclick = function(){
		var i_number = this.id.split('-');
		o_util.doDownloadWindow('?request=download&id='  + i_number[3]  );
	}
}

function secretGame(){
    var codeBool = get_cookie("codeBool");
    var code = "";

 	   if( codeBool == "true" && obj.id  ){
			 popUpWindow( "smarty_files/templates/BeautyBlitz/index.html" , { width : 600 , height : 450  , x : 350 , y : 50 } );
    	   }

   	   else {
		
			if( obj.id ){ 
				
				code = window.prompt("Please enter secret code to play." , code );
                         	if( trim( code.toUpperCase() )  == "F47H31" ){
                             		 assignCookie();
						
					popUpWindow( "smarty_files/templates/BeautyBlitz/index.html" , { width : 600 , height : 450 , x : 350 , y : 50  } ); 
				
                          	}
				else {
				        code = "";
					retry = function(){ 
						code = window.prompt("Please check your secret code and try again." , code );
							if( trim( code.toUpperCase() ) == "X12C09"  ){
							   assignCookie();
							   popUpWindow( "smarty_files/templates/BeautyBlitz/index.html" , { width : 600 , height : 450 ,  x : 350 , y : 50 } ); 
							}
					}	

					retry();

				}

				
			}
			
			else {
				dialogBox();
			}
	
         }

}

function secretGameMoxieSalon(){
    var codeBool = get_cookie("codeBool");
    var code = "";

 	   if( codeBool == "true" && obj.id  ){
			 popUpWindow( "smarty_files/templates/moxiesalon/index.html" , { width : 800 , height : 600  , x : 100 , y : 100 } );
    	   }

   	   else {
		
			if( obj.id ){ 
				
				code = window.prompt("Please enter secret code to play." , code );
                         	if( trim( code.toUpperCase() )  == "KMAF09" ){
                             		 assignCookie();
						
					popUpWindow( "smarty_files/templates/moxiesalon/index.html" , { width : 800 , height : 600 , x : 100 , y : 100  } ); 
				
                          	}
				else {
				        code = "";
					retry = function(){ 
						code = window.prompt("Please check your secret code and try again." , code );
							if( trim( code.toUpperCase() ) == "KMAF09"  ){
							   assignCookie();
							   popUpWindow( "smarty_files/templates/moxiesalon/index.html" , { width : 800 , height : 600 ,  x : 100 , y : 100 } ); 
							}
					}	

					retry();

				}

				
			}
			
			else {
				dialogBox();
			}
	
         }

}


function moxifierGame(){
	if( obj.id ){
		popUpWindow( "smarty_files/templates/moxifier/index.html" , { width : 810 , height : 610 , x : 100 , y : 100 } );
	}
	else {
		dialogBox();
	}
}

function weatherGirlGame(){
	if( obj.id ){
		popUpWindow( "smarty_files/templates/MoxieWeatherGirl/index.php?appName=" + s_appName , { width : 217 , height : 180 , x : 200 , y : 100 } );
	}
	else {
		dialogBox();
	}
}

function assignCookie(){
	document.cookie = "codeBool=true; expires=13/03/2012; path=/;";
}

function get_cookie ( cookie_name ) {
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
  if ( results ){
    return ( unescape ( results[2] ) );
  }
}



 function trim(stringToTrim) {
	if( stringToTrim !== null  ){
		var codeString = stringToTrim.toUpperCase();
		return codeString.replace(/^\s+|\s+$/g,"");
	}
}


function dialogBox(){
 var div = $("div-ctrldialog");
     div.style.display = "block";
     div.innerHTML = "<div id=\"div-closebutton\"><a onclick=\"o_acctutil.doCloseCtrl();\" href=\"#\">[close]</a></div>";	

     div.innerHTML += "<h4>Welcome to moxiegirlz.com! Login or register here.</h4>";
     div.innerHTML += "<button id=\"login-btn\" onClick=\"doInitLoginForm();\">Login</button>";
     div.innerHTML += "<button id=\"register-btn\" onClick=\"if(o_acctutil){doInitRegForm(); showDialogBox(); }\">Register</button>";

}


function popUpWindow( url , objProp ){

	var mywindow = window.open( url , "mywindow" , " location=0, resizable=0 , status=0 ,scrollbars=0 , width=" + objProp.width + " , height=" + objProp.height + " " );
	    mywindow.moveTo(  objProp.x  ,  objProp.y  );
}

function termsPrivacy(){

	if( $("terms") ){
		$("terms").onclick = function(){ popUpWindow( "http://www.mgae.com/terms.asp" , { width : 880 , height : 800 , x : 200 , y : 0  }  );  }
	}

	if( $("privacy") ) {
		$("privacy").onclick = function(){ popUpWindow( "http://www.mgae.com/privacy.asp" , { width : 880 , height : 800 , x : 200 , y : 0  }  ); }
	}

	if( $("rules") ) {
		$("rules").onclick = function(){ popUpWindow( "smarty_files/templates/static/rules.html" , { width : 880 , height : 800 , x : 200 , y : 0  }  ); }
	}

}


var s_bio_girl = "lexa";
var bio_language = "en-us";

function doClickBioGirl(s_new_bio_girl,bio_language){
	$("div-bio-box-" + bio_language).style.background = "url('smarty_files/templates/images/bio_box_"+s_new_bio_girl+"_" + bio_language + ".png') no-repeat top left";
	s_bio_girl = s_new_bio_girl;
	return false;
}

function doHomeFlashPromos() {

var flashvars = {}

var params = {}

var attributes = { 
	wmode : "transparent"
}

    if( $("div-home-promo1") ) {
		swfobject.embedSWF( "smarty_files/templates/flash/home_promo1_en-us.swf" , "div-home-promo1" , "216" , "216" , "9.0.0" , flashvars , params , attributes  );
    }
    if( $("div-home-promo2") ) {
		swfobject.embedSWF( "smarty_files/templates/flash/home_promo2_en-us.swf" , "div-home-promo2" , "216" , "216" , "9.0.0" , flashvars , params , attributes );
    }
    if( $("div-home-promo3") ) {
		swfobject.embedSWF( "smarty_files/templates/flash/home_promo3_en-us.swf" , "div-home-promo3" , "216" , "216" , "9.0.0" , flashvars , params , attributes );
    }
    if( $("div-main-logo") ){
		swfobject.embedSWF( "smarty_files/templates/flash/logo_homepage.swf" , "div-main-logo" , "216" , "216" , "9.0.0" , flashvars , params , attributes );
    }

    if( $("div-logo") ){
		swfobject.embedSWF( "smarty_files/templates/flash/logo.swf" , "div-logo" , "216" , "216" , "9.0.0" , flashvars , params , attributes );

    }

	
}

