// tag name function

jQuery.fn.tagName = function(){
	if(1 === this.length) {
		return this[0].tagName.toLowerCase();
	} else {
		var tagNames = [];
		this.each(function(i, el){
			tagNames[i] = el.tagName.toLowerCase();
		});
		return tagNames;
	}
};

(function($) {

	// return styled message
	$.msg = function(opt) {
		return $('#' + opt.type + 'Tmpl').tmpl(opt);
	}

	// force style widget
	$.widget("ui.force_style", {

		_create: function() {
			this.element.find('p, td').css('color', this.element.css('color'));
		}

	});

})(jQuery);

// document ready
$(document).ready(function() {

	$('body').addClass('claro');

	$('div[id=box]').box();

	$('[forceStyle]').force_style();

	/* ei pane commonisse sellist asja!
	 * 
	$('#poker_odds').dialog({
		autoOpen: false,
		show: 'blind',
		hide: 'explode',
		modal: true,
		width: 830,
		height: 580
	});

	$('#odds_launcher').click(function() {
		$('#poker_odds').dialog('open');
		return false;
	});
	*/

	$("a#single_image").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut':	'elastic',
		'speedIn'		:	500,
		'speedOut'		:	200,
		'overlayShow'	:	false
	});

	$("a.group").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut':	'elastic',
		'speedIn'		:	500,
		'speedOut'		:	200,
		'overlayShow'	:	false
	});
	
});

// colers function
function str2rgb(color) {
	var rgb;
	try {
		eval('rgb = ' + (color.substring(3).replace("(", "[").replace(")", "]")) + ';');
	}
	catch (ex) {
		// ignore
	}
	return rgb;
}
function html2rgb(color) {
	var rgb=[parseInt(color.substring(0,2),16),
		parseInt(color.substring(2,4),16),
		parseInt(color.substring(4,6),16)];
	return rgb;
}
function colorMelt(start, end, amt) {
	try {
		var s,e,r,c,d;
		s=start.length>6?str2rgb(start):html2rgb(start);
		e=start.length>6?str2rgb(end):html2rgb(end);
		d=[e[0]-s[0],e[1]-s[1],e[2]-s[2]];
		if (Math.abs(d[0])>=Math.abs(d[1])&&Math.abs(d[0])>=Math.abs(d[2])) {
			c=Math.abs(d[0])/amt;
		}
		else if (Math.abs(d[1])>=Math.abs(d[0])&&Math.abs(d[1])>=Math.abs(d[2])) {
			c=Math.abs(d[1])/amt;
		}
		else if (Math.abs(d[2])>=Math.abs(d[0])&&Math.abs(d[2])>=Math.abs(d[1])) {
			c=Math.abs(d[2])/amt;
		}
		d[0]=d[0]/c;
		d[1]=d[1]/c;
		d[2]=d[2]/c;
		r=[Math.round(s[0]+d[0]), Math.round(s[1]+d[1]), Math.round(s[2]+d[2])];
		return r[0].toString(16) + r[1].toString(16) + r[2].toString(16);
	}
	catch (ex) {
		// ignore
	}
}

// old school ajax object
function GetXmlHttpObject() {
	var objXMLHttp = null;
	try {
		// Firefox, Opera 8.0+, Safari
		objXMLHttp = new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try {
			objXMLHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return objXMLHttp;
}

// nl2br function
function nl2br (str, is_xhtml) {
	var breakTag = '';
	breakTag = '<br />';
	if (typeof is_xhtml != 'undefined' && !is_xhtml) {
		breakTag = '<br>';
	}
	return (str + '').replace(/([^>]?)\n/g, '$1'+ breakTag +'\n');
}

// openwin function
function openwin(url){
	window.open(url);
}

// popup function
function popup(url, w, h, name, dynamic){
	var attrs = "scrollbars="+(dynamic ? "yes" : "no")+",status=no,resizable="+(dynamic ? "yes" : "no")+", width="+w+", height="+h;
	window.open(url, name, attrs);
}

// image function
function image(url, w, h, dynamic){
	popup(url, w, h, 'image');
}

// aken function
function aken(url, w, h, dynamic){
	popup(url, w, h, 'image');
}

// resize body frame function
function resizeBodyFrame() {
	if(window.console) {	window.console.log("resizeBodyFrame() called!!!"); }
	resizeFrame(getObjectById('bodyFrame'));
}

// preload images function
function preloadImages() {
	var d=document; if(d.images){ if(!d.preload_obj) d.preload_obj=new Array();
		var i,j=d.preload_obj.length,a=preload_images; for(i=0; i<a.length; i++)
		if (a[i].indexOf("#")!=0){ d.preload_obj[j]=new Image; d.preload_obj[j++].src=a[i];}}
}

// findObj function
function findObj(n, d) {
	var p,i,x;	if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); return x;
}

// swap image restore function
function swapImgRestore() {
	var i,x,a=document.sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

// swap image function
function swapImage() {
	var i,j=0,x,a=swapImage.arguments; document.sr=new Array; for(i=0;i<(a.length-2);i+=3);
	 if ((x=findObj(a[i]))!=null){document.sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

// toggleDiv function
function toggleDiv(id) {
  var div = document.getElementById(id);
  if(div.style.display == 'block'){
	 div.style.display = 'none';
  } else {
	 div.style.display = 'block'
  }
}

// highlightit functions
function high(which2) {
	theobject=which2;
	highlighting=setInterval("highlightit(theobject)",50);
}
function low(which2) {
	clearInterval(highlighting);
	which2.filters.alpha.opacity=200;
}
function highlightit(cur2) {
	if (cur2.filters.alpha.opacity>70)
	cur2.filters.alpha.opacity-=7;
	else if (window.highlighting);
	clearInterval(highlighting);
}

// object dump function
var MAX_DUMP_DEPTH = 10;
function dumpObj(obj, name, indent, depth) {
	if (depth > MAX_DUMP_DEPTH) {
		return indent + name + ": <Maximum Depth Reached>\n";
	}
	if (typeof obj == "object") {
		var child = null;
		var output = indent + name + "\n";
		indent += "\t";
		for (var item in obj) {
			try {
				child = obj[item];
			}
			catch (e) {
				child = "<Unable to Evaluate>";
			}
			if (typeof child == "object") {
				output += dumpObj(child, item, indent, depth + 1);
			}
			else {
				output += indent + item + ": " + child + "\n";
			}
		}
		return output;
	}
	else {
		return obj;
	}
}
