/*
* Func: postJSON
* Desc: Use JSON to import scripts/data
*/
$.postJSON = function(url, data, callback) {
	$.post(url, data, callback, "json");
};

/*
* Function: popWindow
*/
function popWindow(id,url,x,y) {
	day = new Date();
    eval(id+"=window.open(url,'"+id+"','toolbar=0,scrollbars=0,location=0,statusbar=1,menubar=0,resizable=0,width="+x+",height="+y+",left=400,top=300');");
}

/*
* Function: in_array(arry,toFind)
* Ret true if toFind is in array. Emulates PHP's function
*/
function in_array(arry,toFind) {
	return ('||'+arry.join('||')+'||').indexOf('||'+toFind+'||') > -1;
}

/*
* Redirect
*/
function redirect(mylocation,mytime) {
	setTimeout( function(){
		location.href = mylocation.replace('#','');
	},mytime);
}

/*
* Test if value is integer 
*/
function isInt(s){
	return parseInt(s,10)===s;
} 

function isNumber(n) {
	return !isNaN(parseFloat(n)) && isFinite(n);
}
function isObj(o){ 
	return (typeof(o)=="object")
}

/*
* Pad zeroes to front of number; 
*/
function padZeroes(number, length) {  
    var str = '' + number;
    while (str.length < length) {
        str = '0' + str;
    } 
    return str;
}

/*
* From resig
*/
Array.max = function(array){
    return Math.max.apply( Math, array );
};

/*
* Some color manipulating functions 
*/
function getHex(r,g,b) {
	if(r<0)r=0;if(r>255)r=255;
	if(g<0)r=0;if(g>255)g=255;
	if(b<0)r=0;if(b>255)b=255;
    //alert("#"+decColor.toString(16));
    return '#' + ( r << 16 | g << 8 | b ).toString(16);
}

/*
* Get r,g,b array from hex color  
*/
function getRGB(hex) {
	//if(hex.length<6)
	//	for(i=0;i<=(6-hex.length);i++)
	//		hex += hex[hex.length-1];
	return([
	    parseInt((cutHex(hex)).substring(0,2),16),
	    parseInt((cutHex(hex)).substring(2,4),16),
	    parseInt((cutHex(hex)).substring(4,6),16)
	]);
}

/*
* Do some magic on returned RGB 
*/
function hex(c) {
	var m = /rgba?\((\d+), (\d+), (\d+)/.exec(c);
	return m ? '#' + ( m[1] << 16 | m[2] << 8 | m[3] ).toString(16): c;
}
function rgb(c) {
	var m = /rgba?\((\d+), (\d+), (\d+)/.exec(c);
	return [m[1],m[2],m[3]];
}

/*
* Show and hide dark page overlay
*/
function showDialog() {
    $('#darkbg').height($(document).height());
    $('#darkbg').css({"visibility":"visible"});
    $('#darkdiv').css({"visibility":"visible"});
}

function hideDialog() {
    $('#darkbg').css({"visibility":"hidden"});
    $('#darkdiv').css({"visibility":"hidden"});
}

/*
* Show a help page
*/
function dialog(page) {
    $('#darkContent').html("Loading Interface...");
    showDialog();
    $('#darkContent').load('pages/dialogs/'+page);
}

/*
* Get credit card type from number 
* Returns: "Mastercard","AMEX","Discover","VISA","Error"
*/
function getCCardType(value) {
	var first  = value.charAt(0);
	var second = value.charAt(1);
	var third  = value.charAt(2);
	var fourth = value.charAt(3);
	if(first=="4") return "VISA";
	else if((first == "3")&&((second == "4")||(second == "7"))) return "AMEX";
	else if(first=="5") return "Mastercard";
	else if((first == "6")&&(second == "0")&&(third == "1")&&(fourth == "1")) return "Discover";
	else return "Error";
	// Easy nuff.
}

// Thanks internet
$.fn.clearForm = function() {
    return this.each(function() {
      var type = this.type, tag = this.tagName.toLowerCase();
      if (tag == 'form')
        return $(':input',this).clearForm();
      if (type == 'text' || type == 'password' || tag == 'textarea')
        this.value = '';
      else if (type == 'checkbox' || type == 'radio')
        this.checked = false;
      else if (tag == 'select')
        this.selectedIndex = -1;
    });
  };

// Get lets add some interactivity for our new buttons!
$(document).ready(function(){
	/*
	* Buttons... this is all for you
	*/
	// For buttons... transfer click events to parent div... maybe?	
	$('[class^="btn_"] .txt').children("a").click(function(e){
		e.stopPropagation();
	});
	$('[class^="btn_"] .txt').click(function(){
		$(this).children("a:first").trigger("click");
		// this next part sucks... stupid javascript
		if(typeof($(this).children("a:first").attr("href"))!="undefined")
			window.location=$(this).children("a:first").attr("href");
	});

	$('[class^="btn_"]').mouseover(function(){ 
		$(this).children(".txt,.l,.r").css({"background-color":"#fff"});
	}).mouseout(function(){
		$(this).children(".txt,.l,.r").css({"background-color":"transparent"});
	}).mousedown(function(){
		var offl=0; var offr=0;
		var col=$(this).attr("class").split("_").pop();
		// damn. hardcode. oh well
		switch(col) {
			case "blu":offl=7;offr=3;break;
			case "wht":offl=21;offr=17;break;
			case "red":offl=15;offr=10;break;
		}

		$(this).children(".txt").css({"background":"url(graphx/images/n_btnbg"+col+"hov.png)"});
		$(this).children(".l").css({"background":"url(graphx/images/n_btnsprite.png) "+offl+"px 0"});
		$(this).children(".r").css({"background":"url(graphx/images/n_btnsprite.png) "+offr+"px 0"});
		$(this).children(".txt").children("a").css({"text-shadow":"none"});
	
		
	}).mouseup(function(){
		var offl=0; var offr=0;
		var col=$(this).attr("class").split("_").pop();
		// Button pops back up
		switch(col) {
			case "blu":offl=-14;offr=-18;break;
			case "wht":offl=0;offr=-4;break;
		}
		
		$(this).children(".txt").css({"background":"url(graphx/images/n_btnbg"+col+".png)"});
		$(this).children(".l").css({"background":"url(graphx/images/n_btnsprite.png) "+offl+"px 0"});
		$(this).children(".r").css({"background":"url(graphx/images/n_btnsprite.png) "+offr+"px 0"});
	});
	
	/*
	* 
	*/
});
