// Get out of frames (e.g. from Hotmail)
if (top!=window && top.location.indexOf('facebook')<0)
{
	top.location=window.location;
}

if (typeof(lang_id)=='undefined' || !lang_id) {
	lang_id = 1;
}
if (typeof(site_id)=='undefined' || !site_id) {
	site_id = 1;
}
var item_name = (lang_id!=2?'card':'carta');
if (site_id==2) {
	item_name = (lang_id!=2?'miniature':'miniatura');
}

var prev_row_color = '';
var highlight_row_color = '#ffcccc';

function highlight(row, active) {
	if (row && row.style) {
		if (active) {
			prev_row_color = row.style.backgroundColor;
			row_color = highlight_row_color;
		} else {
			row_color = prev_row_color;
		}
		row.style.backgroundColor=row_color;
	}
}

function openWin(url, win_name){
    if(!win_name){
        win_name = "cardinfo";
    }
    card_window = window.open(url, win_name, "toolbar=0, location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=600,height=700");
    if (card_window) {
    	card_window.focus();
    }
    return false;
}
function openHelpWin(url){
    card_window = window.open(url, "help_page", "width=500, height=600, scrollbars=yes");
    if (card_window) {
    	card_window.focus();
    }
    return false;
}
function openChatWin(url){
	if (url!='#') {
	    window.open(url, "_blank", "toolbar=0, location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=600,height=500");
	}
    return false;
}
function openGameWin(url){
    card_window = window.open(url, 'gameroom', "toolbar=0, location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=1200,height=800");
    if (card_window) {
    	card_window.focus();
    }
    return false;

}

function printLink(text, url, title, lose_focus){
	if (!title) {
		title = text;
	}
	if ((top.name=='cardinfo' || top.name=='help_page') && top.opener) {
		document.write("<a href='javascript:followLink("+'"'+url+'"'+");' onClick='top.opener.focus();");
		if (!lose_focus){
			document.write(" top.focus();");
		}
		document.write(" return true;' title='"+title+"'>"+text+"</a>");
	} else {
		document.write("<a href='"+url+"' title='"+title+"'>"+text+"</a>");
	}
}
function followLink(url){
	top.opener.location = url;
}

var prev_src = "";
function swapImage(name, default_code){
	if (!default_code) {
		default_code = 'a';
	}
	var image = document.getElementById(name);
	if (!image) {
		image = document.getElementsByName(name)[0];
	}
	if (image) {
		prev_src = image.src;
		if(image.src.indexOf("sort_arrow_a.gif")>=0){
			new_src = "sort_arrow_dy.gif";
		} else {
			if(image.src.indexOf("sort_arrow_d.gif")>=0){
				new_src = "sort_arrow_ay.gif";
			} else {
				new_src = "sort_arrow_"+default_code+"y.gif";
			}
		}
		image.src = 'img/'+new_src;
	}
}

function swapBack(name){
	var image = document.getElementById(name);
	if (!image) {
		image = document.getElementsByName(name)[0];
	}
	image.src = prev_src;
}

var select_all_form_name='form';

function setSelectAllFormName(name){
	select_all_form_name = name;
}

function checkAll(item_name, checked){
	var f = document.forms[select_all_form_name];
	if (f && f.elements) {
		for( i=0 ; i<f.elements.length ; i++) {
			if (f.elements[i].name.substring(0, item_name.length)==item_name) {
				f.elements[i].checked=checked;
				if (f.elements[i].onclick) {
					f.elements[i].onclick();
				}
			}
		}
	}
}

function toggle_checkbox(element){
    if (typeof(element)=='string' || element.substring) {
        element = document.getElementById(element);
    }
	if (element) {
		element.checked = (element.checked?false:true);
	}
}

/**
* Centers a given element in the browser window
*/
function centerPane(pane_id, pane_width){
	var pane = document.getElementById(pane_id);
	if (pane && pane.style) {
		if (pane_width<=0) {
			pane_width = pane.clientWidth;
		}
		var pane_left = Math.round(((document.body.clientWidth/2) - (pane_width/2)));
		if (pane_left<0) {
			pane_left = 0;
		}
		pane.style.left = pane_left+'px';
	}
	return false;
}

/**
* Toggles the visibility of a given html element and centers it if a width is provided
*/
function togglePane(pane_id, new_state, pane_width){
	var pane = document.getElementById(pane_id);
	if (pane && pane.style) {
		if (new_state!='block' && new_state!='none') {
			new_state = (pane.style.display == 'block'?'none':'block');
		}
		pane.style.display = new_state;
		if (new_state=='block' && pane_width>0) {
			centerPane(pane_id, pane_width);
		}
	}
	return false;
}

/**
 * Displays or hides elements with a given class
 */
function toggleElementsDisplay(class_name){
	var divs = document.getElementsByTagName('div');
	for (i=0; i<divs.length; ++i) {
		if (divs[i].getAttribute('class')==class_name) {
			divs[i].style.display = (divs[i].style.display=='none'?'block':'none');
		}
	}
	return false;
}

function showUserLevelTip(event){
	var tip_element = document.getElementById('user_level_tip');
	x = Event.pointerX(event) + 15;
	y = Event.pointerY(event);
	Element.setStyle(tip_element, {display: 'block', top: y, left: x});
}
function hideUserLevelTip(event){
	var tip_element = document.getElementById('user_level_tip');
	Element.setStyle(tip_element, {display: 'none'});
}

function increaseValue(id){
	var amount_field = document.getElementById(id);
	if (amount_field && (amount_field.value==0 || amount_field.value==parseInt(amount_field.value))) {
		amount_field.value++;
		if (amount_field.onchange) {
			amount_field.onchange();
		}
	}
}

function decreaseValue(id){
	var amount_field = document.getElementById(id);
	if (amount_field && amount_field.value>0 && amount_field.value==parseInt(amount_field.value)) {
		amount_field.value--;
		if (amount_field.onchange) {
			amount_field.onchange();
		}
	}
}

var add_card_button_enable_timer = null;

/**
 * Disables or re-enables the Quick Add button
 * (helps prevent it being pressed more than once)
 * The button goes automatically back to enabled after a few seconds
 */
function toggle_add_card_button(state){
	$$('.add_card_submit').each(function(element){
		if (state=='disable' && !element.disabled) {
			element.value = (lang_id==2?'Aņadiendo...':'Adding...');
			element.disabled = true;
			add_card_button_enable_timer = setTimeout("toggle_add_card_button('enable');", 6000);
		} else {
			element.value = (lang_id==2?'Aņadir':'Add');
			element.disabled = false;
			clearTimeout(add_card_button_enable_timer);
		}
	});

}

/**
 * Stores the ID for the selected card into the correct form field
 * Called from the Ajax.Autocompleter when an item is selected
 */
function copy_selected_card_id(input_field, selected_item){
	var card_id_element = document.getElementById('selected_card_id');
	if (card_id_element) {
		card_id_element.value = selected_item.id;
	}
}

function showAddForm(collection_type){
	var form = $('add_to_collection_'+collection_type);
	if (form) {
		Element.setStyle(form, {display: 'block'});	
	}
}

