var downloadedFlag = 0;
var game_id = window.location.host.split(".")[0];

function log(msg){ // Log a message into the console without throwing an error in IE
	try {
		console.log(msg);
	} catch(err){ }
}

/*	SWFObject v2.2 <http://code.google.com/p/swfobject/> 
	is released under the MIT License <http://www.opensource.org/licenses/mit-license.php> 
*/

function secureLights(){ // Fading the overlay in and out
	if(jQuery("#secure_overlay").is(":visible")){
		jQuery("#secure_overlay").fadeOut(400);
		jQuery("#secure_fade").fadeOut(400);
		jQuery("#secure_fade").fadeTo(0,0);
	} else {
		jQuery("#secure_overlay").fadeIn(400);
		jQuery("#secure_fade").height(jQuery("#wrapper").height());
		jQuery("#secure_fade").show();
		jQuery("#secure_fade").fadeTo(400,0.6);
		jQuery("#secure_overlay_content").fadeIn(400);
	}
}

function getCookie(key){ // Cookie functions
    var cookieValue = null;

    if (key) {
        var cookieSearch = key + "=";
        if (document.cookie){
            var cookieArray = document.cookie.split(";");
            for (var i = 0; i < cookieArray.length; i++) {
                var cookieString = cookieArray[i];
                // skip past leading spaces
                while (cookieString.charAt(0) == ' '){
                    cookieString = cookieString.substr(1);
                }
                // extract the actual value
                if (cookieString.indexOf(cookieSearch) == 0){
                    cookieValue = cookieString.substr(cookieSearch.length);
                }
            }
        }
    }
    return cookieValue;
}

function submitRegisterPage(goPage){			
			jQuery.ajax({
				type : "post",
				url : "/teaser/",
				cache : true,
				data : { pagetype: goPage },
				complete: function(){  },
				success: function(result){
					jQuery("#register_wrapper").html(result);
					jQuery("#loading_animation").fadeOut(200);
					jQuery("#register_wrapper").slideDown(300);
					bindRegisterEvents();
				}
			});
		}


function setCookie(key, val){
    if (key){
        var date = new Date();

        if (val != null){
            // expires in twenty years
            date.setTime(date.getTime() + (20*365*24*60*60*1000));
            document.cookie = key + "=" + val + "; expires=" + date.toGMTString() + "; domain=gpotato.com; path=/";
        } else {
            // expires yesterday
            date.setTime(date.getTime() - (24*60*60*1000));
            document.cookie = key + "=; expires=" + date.toGMTString();
        }
    }
}

jQuery(document).ready(function(){

	document.domain = "gpotato.com";

	/***********
	jQuery('a').each(function(){
		href = jQuery(this).attr('src');
		splitA = href.split('/');
		if (splitA[1]=='node' && splitA[2]=='add') {
		  jQuery(this).attr('href', 'https://'+location.host+href);
		}
	});
	*************/

	// Wrap All Content Insert Overlay HTML
	jQuery("#wrapper").wrap('<div id="secure_overlay_wrapper"></div>');
	jQuery("body").append('<div id="secure_overlay"><div id="secure_fade"></div><div id="secure_overlay_content"><img src="/images/popup_close.gif" id="popup_secure_close" /><iframe id="popupSecureFrame" name="popupSecureFrame" width="585" height="447" scrolling="no" frameborder="0"></iframe></div></div>');	

	jQuery("#secure_fade").fadeTo(0,0);

	jQuery("#popup_secure_close").click(function(){
		secureLights();
	});

	jQuery("a.secure").click(function(){
		jQuery("#popupSecureFrame").attr("src","https://secure.gpotato.com/closedbeta_login.php?cbtlogintype=signin"); 
		if (jQuery.browser.opera == false) {
			var newTop = jQuery("html").scrollTop();
			if(newTop == 0){
				newTop = jQuery("body").scrollTop();
			}
			newTop = newTop + 50;
			
			jQuery("#secure_overlay_content").css("top", newTop + "px");
			secureLights();
			return false;
		}
	});

	jQuery("a.cbtsignup").click(function(){
		jQuery("#popupSecureFrame").attr("src","https://secure.gpotato.com/closedbeta_login.php?cbtlogintype=signupcbt"); 
		if (jQuery.browser.opera == false) {
			var newTop = jQuery("html").scrollTop();
			if(newTop == 0){
				newTop = jQuery("body").scrollTop();
			}
			newTop = newTop + 50;
			
			jQuery("#secure_overlay_content").css("top", newTop + "px");
			secureLights();
			return false;
		}
	});

});
