var orig_width = null;
var orig_height = null;

function toggle(id) {
	if (document.getElementById('check-' + id).checked == true) {
		document.getElementById('img-' + id).className = 'thumb';
		document.getElementById('div-' + id).className = 'img_float';
		document.getElementById('check-' + id).checked = false;
	} else {
		document.getElementById('img-' + id).className = 'selected';
		document.getElementById('div-' + id).className = 'img_float_selected';
		document.getElementById('check-' + id).checked = true;
	}
}

function change_all(action) {
	var field = document.body.getElementsByTagName('input');
	for (i = 0; i < field.length; i++) {
		if (field[i].checked != action || action == 'toggle') {
			if (field[i].type == 'checkbox') {
				toggle(field[i].value);
			}
		}
	}
}

function submit_action(action_value, pass_id, submit_bool, loc) {
	document.getElementById('information').name = action_value;
	document.getElementById('information').value = document.getElementById(pass_id).value;
	if (loc != null)
		document.view_form.action = loc;
	if (submit_bool == true)
		document.view_form.submit();
}

function initialize(tmp) {
	orig_width = document.getElementById('img').width;
	orig_height = document.getElementById('img').height;
	resize_img();
	if (tmp == 1)
		cookie_fetch();
}

function cleartext(field) {
	document.getElementById('clear' + field).innerHTML = '';
	document.getElementById('clear' + field).className = '';
}

function filltext(field, text) {
	if (document.getElementById('clear' + field).value == '') {
		document.getElementById('clear' + field).innerHTML = text;
		document.getElementById('clear' + field).className = 'light';
	}
}

function resize_img() {
	var newwidth = clientWidth()-250;
	var ratio = newwidth/document.getElementById('img').width;
	var newheight = document.getElementById('img').height*ratio;
	if (newwidth < orig_width) {
		document.getElementById('img').width = newwidth;
		document.getElementById('img').height = newheight;
	}
}

function star(hilight, id) {
	for (i = 0; i < hilight; i++) {
    document.getElementById('star-' + (eval(i)+1) + '-' + id).src = "http://www.simpload.com/images/icons/star-full.jpg";
	}
	var v = (eval(i));
	for (; v < 6; v++) {
    document.getElementById('star-' + (eval(v)+1) +'-' + id).src = "http://www.simpload.com/images/icons/star-empty.jpg";
	}
}

function star_restore(total, id) {
	var post = 1;
	for (i = total; i > 0; i--) {
		if (Math.round(i) == 1 && i < 1)
	    document.getElementById('star-' + eval(post) + '-' + id).src = "http://www.simpload.com/images/icons/star-half-dim.png";
		else
			if (i >= 1)
				document.getElementById('star-' + eval(post) + '-' + id).src = "http://www.simpload.com/images/icons/star-full-dim.png";
			else
				post--;
			
		post++;
	}

	for (i = post; i < 6; i++) {
		document.getElementById('star-' + eval(i) + '-' + id).src = "http://www.simpload.com/images/icons/star-empty.jpg";
	}
}

function clientWidth()
{
	return f_filterResults
	(
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_filterResults(n_win, n_docel, n_body)
{
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
