$(function(){
	timeIt('6:00',loggedIn);
	$('#header_tab_nav li').not('#current').hoverIntent({
		over:function(){
				$(this).addClass('hoverTab');
			},
		out:function(){
				$(this).removeClass('hoverTab');
			},
		interval:20
	});
	$('#banner-cycle').cycle({ 
		fx:     'fade',
		speed:  1000,
		timeout: 10000
	});
    // Prevent Google Toolbar from styling our form inputs
    // $('input,select,textarea').each(function(){
        // $(this).bind('propertychange',function(e){
            // $(this).css({"background-color":"#fff"});
        // });
    // });
    // If this page is loading in an iframe
	if(window.top==window.self){
		$('#home_link').click(function(){
			window.location="/index.php";
		});
	}
	
	$('.current_account').click(function() { 
		var rel = $(this).attr("rel");
		var vals = rel.split("|",3);
		var gameID = vals[0];
		var username = vals[1];
		var whereto = vals[2];
		
		$.post("../session-o-matic.php", {
			strPost: gameID + '|' + username
		}, function(){
			var go_to_url = '';
			switch(whereto) {
				case "profile": go_to_url = "../myprofile.php"; break;
				case "redeemcode": go_to_url = "../redeemcode/"; break;
				case "marketplace": go_to_url = "../marketplace/"; break;
				case "knightcash": go_to_url = "../distribute_knight_cash.php"; break;
			}
			window.location = go_to_url;
		});
	});
	
	$('.gf-logo').click(function() {
		var rel = $(this).attr("rel");				 
		$.post("/session-o-matic.php", {
			resetCodeType: 'resetme'
		}, function(response){
			window.location = '/index.php' + rel;
		});
	});
});

function checkIt(field,type){
	fieldArr = field.split("");
	fieldCt = fieldArr.length;
	var array;
	var username = new Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
	var email = new Array(".","_","-","+","@","a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
	var password = new Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
	var password2 = new Array("!","@","#","$","%","^","&","*","?","_","~","-","(",")","a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
	var answers = new Array(" ",".","_","-","a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
    var re = /^(\?[A-Za-z0-9])?(((?!.*[*|]|delete|select|update|insert|--).)*)$/
	switch(type){
        case 1:
            array = username;
            break;
        case 2:
            array = password;
            break;
        case 3:
            array = email;
            break;
        case 4:
            array = password2;
            break;
        case 5:
            array = answers;
            break;
        case 6:
            array = false;
    }
    if(type != 6){
    	var i,f,c;
    	c = 0;
    	for(i=0;i<fieldArr.length;i++){
    		for(f=0;f<array.length;f++){
    			if(fieldArr[i]==array[f]){
    				field.replace(fieldArr[i],"*");
    				c++;
    			}
    		}
    	}
    	if(c<field.length){
    		return false;
    	}
    	return true;
    }else if(type == 6) {
        if(re.test(field))
            return true;
        else
            return false;
    }
}

jQuery.preloadImages = function()
{
	for(var i = 0; i<arguments.length; i++)
	{
		jQuery("<img>").attr("src", arguments[i]);
	}
}

jQuery.fn.slideFadeToggle = function(speed, easing, callback) {
	return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback); 
};

function capsLock(e){
	kc = e.keyCode?e.keyCode:e.which;
	sk = e.shiftKey?e.shiftKey:((kc == 16)?true:false);
	if(((kc >= 65 && kc <= 90)) && sk)
		return 'SHIFT';
	if(((kc >= 65 && kc <= 90))||((kc >= 97 && kc <= 122) && sk))
		return 'CAPS';
	return false;
}

function toggleLabel(element){
    if(element.val() == "" && element.prev().is(":visible"))
        element.prev().hide();
    else if(element.val() == "" && element.prev().is(":hidden"))
        element.prev().show();
}

function updateMarketplaceAccountDetailsSidePanel() {
	var domain = window.location.protocol+"//"+document.domain+"/";
	$.post(domain + "marketplace/account_details.php", {
		postVal: "update"
	}, function(response) {
		var returnVal = unescape(response);
		$('#sidePanel_accountDetails').fadeOut("fast", function() {
			$(this).html(returnVal).fadeIn();	
			
			$("#wrShowAllInventory").click(function () {
		    	$("#hiddenInventory").slideToggle("medium");
		    });
			
			var host_name = document.location.hostname;
			var current_url = document.location.href;
			if (current_url == 'http://' + host_name + '/index.php') {
				$('.inventory').hide();
			}
            
            if(current_url == 'http://' + host_name + '/backpack.php') {
                $('#mkt_backpack').remove();
            }
			
			// Side panel Account Details
			// Portal Nickname and Game Nickname are clickable and once clicked will take you to the Gamer Profile for that account
			$('.game_nickname').click(function() {
				var rel = $(this).attr("rel");
				var vals = rel.split("|",2);
				var gameID = vals[0];
				var username = vals[1];
				
				$.post(domain + "session-o-matic.php", {
					strPost: gameID + '|' + username
				}, function() {
					window.location = domain + "myprofile.php";
				});
			});
			
		});
	});
}

// Credit Counter
function CreditCounterPopup(value,updated_counter_value,gameID) {
	if (value <= 0) {
		// User has spent over the credit counter threshold
		// Popup 'you got a free promo code' message
		$('.promo_counter'+gameID).html(updated_counter_value);
		
		$('#credit_counter_popup').jqm({
	        ajax:'/marketplace/promo_code_reward.php?gID='+gameID,
	        onLoad: giftCodePopupScripts,
	        overlay: 50,
	        toTop: true,
	        modal: true
	    }).jqmShow();
	}
	else
		$('.promo_counter'+gameID).html(updated_counter_value);
}
// this function is needed for the credit counter when esn codes are redeemed.
// the actual function is in redeem_scripts.js
var giftCodePopupScripts = function(hash){

}

var response_timer;
function float_response_timer(seconds){
    clearTimeout(response_timer);
    if(!seconds) { seconds = 5;}
    seconds = seconds * 1000;
    $('#floatResponse:visible').click(function(){
        $(this).fadeOut();
    });
    if($('#floatResponse').is(":visible"))
        response_timer = setTimeout("$('#floatResponse').fadeOut()",5000);
}
