$(document).ready(
	function() {

		// Select all
		$("A[href='#select_all']").click( function() {
			$("#" + $(this).attr('rel') + " INPUT[type='checkbox']").attr('checked', true);
			return false;
		});

		// Select none
		$("A[href='#select_none']").click( function() {
			$("#" + $(this).attr('rel') + " INPUT[type='checkbox']").attr('checked', false);
			return false;
    });

    // Invert selection
		$("A[href='#select_toggle']").click( function() {
			$("#" + $(this).attr('rel') + " INPUT[type='checkbox']").each( function() {
				$(this).attr('checked', !$(this).attr('checked'));
			});
			return false;
		});
	}
);

jQuery.fn.log = function (msg) {
	if( typeof(console) != "undefined" ) {
		console.log("%s: %o", msg, this);
		return this;
	}
};

function checkClass(name) {
	$("input[class="+name+"][type='checkbox']").attr('checked', true);
}

function uncheckClass(name) {
	$("input[class="+name+"][type='checkbox']").attr('checked', false);
}

function submitForm(form) {
	$("form:"+form).submit();
}

function forum_emo(obj,emo) {
	obj.focus();
	obj.value += " "+emo;
	obj.focus();
}

function href(url) {
	window.location.href=url;
}

function openHref(url) {
    window.open(url);
}

function getHTTPObject() {
	var A;
	try {
		A=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e) {
		try {
			A = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (oc) {
			A=null;
		}
	}
	if(!A && typeof XMLHttpRequest != "undefined") A = new XMLHttpRequest();
	if (!A) alert("Could not create connection object.");
	return A;
}

function GemsItem(obj,item) {

	var xhr = getHTTPObject();

	if (xhr.readyState != 0) {
		xhr.abort();
	}

	xhr.onreadystatechange = function() {

		if(xhr.readyState == 4) {

			what = document.getElementById(obj)

      if(xhr.status == 200) {
      	what.innerHTML = xhr.responseText;
				//document.ajax.dyn.value = xhr.responseText;
				//alert("Resp.: " + xhr.responseText);
			}
      else {
        what.innerHTML = "Error code: " + xhr.status;
				//alert("Status " + xhr.status);
      }
    }
		else {
			// not ready
		}
	}
	url = "/dialog/itemdb/a/fetch/?dialog=1&item=" + item;
  xhr.open('GET', url,  true);
  xhr.send(null);
}

function GemsDialog(action,id,java,subid) {

	url = '/dialog/?dialog=1&action='+action+'&id='+id+'&java='+java+'&subid='+subid;
	GemsWindow(url);
}

function GemsWindow(url) {
	window.open(url, 'gems_dialog', 'scrollbars=no,menubar=no,height=400,width=600,resizable=yes,toolbar=no,location=no,status=no');
}

function insert_youtube_old(obj) {
	document.write("<object width=\"425\" height=\"355\">\n");
	document.write("	<param name=\"movie\" value=\"http:\/\/www.youtube.com\/v\/"+obj+"&amp;rel=0&amp;color1=0xE46E00&amp;color2=0xFF9900&amp;border=0\"></param>\n");
	document.write("	<param name=\"wmode\" value=\"transparent\"><\/param>\n");
	document.write("	<embed src=\"http:\/\/www.youtube.com\/v\/"+obj+"&amp;color1=0xE46E00&amp;color2=0xFF9900&amp;border=0\" type=\"application\/x-shockwave-flash\" wmode=\"transparent\" width=\"425\" height=\"355\"><\/embed>\n");
	document.write("<\/object>\n");
}

function insert_youtube(obj) {
	document.write("<object width=\"425\" height=\"355\">\n");
	document.write("	<param name=\"movie\" value=\"http:\/\/www.youtube.com\/v\/"+obj+"&amp;rel=0&amp;color1=0xE46E00&amp;color2=0xFF9900&amp;border=0\"></param>\n");
	document.write("	<param name=\"allowFullScreen\" value=\"true\"></param>\n");
	document.write("    <param name=\"allowscriptaccess\" value=\"always\"></param>\n");
	document.write("	<embed src=\"http:\/\/www.youtube.com\/v\/"+obj+"&amp;color1=0xE46E00&amp;color2=0xFF9900&amp;border=0\" type=\"application\/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"425\" height=\"355\"><\/embed>\n");
	document.write("<\/object>\n");
}

function insert_break_video(obj) {
	document.write("<object width=\"464\" height=\"392\">\n");
	document.write("	<param name=\"movie\" value=\"http:\/\/embed.break.com\/"+obj+"\"><\/param>\n");
	document.write("	<embed src=\"http:\/\/embed.break.com\/"+obj+"\" type=\"application\/x-shockwave-flash\" width=\"464\" height=\"392\"><\/embed>\n");
	document.write("<\/object>\n");
}

function insert_google_video(obj) {
	document.write("<embed style=\"width:400px; height:326px;\" id=\"VideoPlayback\" type=\"application\/x-shockwave-flash\" src=\"http:\/\/video.google.com\/googleplayer.swf?docId="+obj+"&amp;hl=en\" flashvars=\"\"><\/embed>\n");
}

function alertSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  window.alert( 'Width = ' + myWidth + '\n' + 'Height = ' + myHeight );
}

function setSize(x,y) {
  var myWidth = x, myHeight = y;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    window.innerWidth = myWidth;
    window.innerHeight = myHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    document.documentElement.clientWidth = myWidth;
    document.documentElement.clientHeight = myHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    document.body.clientWidth = myWidth;
    document.body.clientHeight = myHeight;
  }
}

function set_bg(obj) {
	obj.style.backgroundColor = "525C6E";
	obj.style.Color = "FFFFFF";
}

function reset_bg_color(obj, color) {

  //window.alert("reset_bg_color");

  re_inactive = new RegExp("inactive");
  re_active   = new RegExp("active");
  re_hidden   = new RegExp("hidden");

  new_color = "transparent";

  	if( re_hidden.test(color) ) {
		new_color = "552222";
	}
  	else
	if( re_inactive.test(color) ) {
		new_color = "161a21";
	}
	else {
		new_color = "363a41";
	}
	obj.style.backgroundColor = new_color;
	obj.style.Color = new_color;
}

function reset_bg(obj) {
  new_color = "transparent";
	obj.style.backgroundColor = new_color;
	obj.style.Color = new_color;
}
