var debug;

function init ()
{
	externalLinks();
	controlTextArea();
}
window.onload = init;

function changeClass(obj, new_class)
{
    obj.className = new_class;
}

function changeBgColor(obj, new_color)
{
	obj.style.backgroundColor=new_color;
}

function changePic(name, pic)
{
	name.src = pic;
}

function popup (url, name, width, height)
{
	left_pos = (screen.width / 2) - (width / 2);
	top_pos = (screen.height / 2) - (height / 2);

	window.open(url, name, 'width=' + width + ',height=' + height + ',left=' + left_pos + ',top=' + top_pos);	
}

function externalLinks()
{
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++)
	{
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
			anchor.target = "_blank";
	}
}

function emptyfield (field, defaultval, blur)
{
	
	if ( blur == true ) {
	
		if (field.value == '') {
			field.value = defaultval;
		}
	
	} else {
	
		if (field.value == defaultval) {
			field.value = '';
		}
	
	}
		
	return true;
}


function saveCursor (formElement)
{
    if (document.getElementById(formElement).createTextRange) {
        document.getElementById(formElement).cursorPosition = document.selection.createRange().duplicate();
	}  
}
function addTag (tag, formElement)
{
	if (document.getElementById(formElement).createTextRange && document.getElementById(formElement).cursorPosition) {
        document.getElementById(formElement).cursorPosition.text = document.getElementById(formElement).cursorPosition.text.charAt(document.getElementById(formElement).cursorPosition.text.length - 1) == ' ' ? tag + ' ' : tag;
    } else {
        document.getElementById(formElement).value += tag;
    }
    document.getElementById(formElement).focus();
}


function checkFieldValue (id_name, dictionary_value)
{
	//alert(id_name+' : '+dictionary_value);
	if (dictionary_value == document.getElementById(id_name).value)
		document.getElementById(id_name).value = '';
}


/* textarea rows control */
function controlTextAreaCountLines (text, cols)
{
	var hard_lines = 1;
	var last = 0;

	while (true)
	{
		last = text.indexOf("\n", last+1);
		hard_lines ++;
		if ( last == -1 ) break;
	}

	var soft_lines = Math.round(text.length / (cols-1));
	var hard = eval("hard_lines  " + unescape("%3e") + "soft_lines;");
	if ( hard ) soft_lines = hard_lines;
	return soft_lines;
}

function controlTextAreaSub ()
{
	var text = document.getElementById('form_1_content');
	text.rows = controlTextAreaCountLines(text.value, text.cols) + 1;
}

function controlTextArea ()
{
	if (document.getElementById('form_1_content'))
	{
		controlTextAreaSub();
		setTimeout("controlTextAreaSub();", 100);
	}
}



function contentVideo()
{
	$$('.content .media').each(function(media_box) {
		var image = media_box.down('img');
		var video = media_box.down('a');

		if (image && video) {
			var width = 320;
			var height = 240;
			media_box.id = 'content_media_'+ Math.floor(Math.random() * 1000 + 1);
			var so = new SWFObject('js/mediaplayer.swf', media_box.id, width, height, 9);
			so.addParam('allowfullscreen', true);
			//so.addParam('wmode', 'transparent');
			so.addVariable('width', width);
			so.addVariable('height', height);
			so.addVariable('file', video.href);
			if (image) {
				so.addVariable('image', image.src);
			}
			so.addVariable('searchbar', 'false');
			so.addVariable('volume ', '100');
			so.write(media_box.id);
		}
	});
}
Event.observe(document, 'dom:loaded', contentVideo);