$(document).ready(function() {

	$("a[href^='#']").each(
		function( i ) {
			$(this).attr("href", document.URL + $(this).attr("href") );
		}
	);

	// filter limit
	$('#limit').change(function() {
		$('#paginationForm_limit').submit();	
	});
	
	var myForm = eval( 'document.' + currentForm );

	// Select all links with lightbox class
	if (typeof use_lightbox != "undefined") {
		if(use_lightbox) {
			//$('a.lightbox').lightBox();
			$(function() {
				$('a.lightbox').lightBox();
			});
		}
	}
	
	// search.php 'search-agent'
	// register_interview.php STEP3
	// annonce.php / user_annonce.php
	
	// define vars
	var regionbox =  $('#regiondiv');
	var countryselect =  $('#country');
	var regionsselect =  $('#regions');
        
	// hide regionbox
	if(currentForm=='searchForm' || currentForm=='user_searchForm')
		regionbox.hide();

	// 'toggle' regionbox
	if (typeof use_jQuery != "undefined") {
		if((currentForm=='user_annonceForm' || currentForm=='annonceForm') && use_jQuery) {
			// option to select region(s)
			var regionscheckitem = myForm.regions[2].selected;

			if(regionscheckitem)
				regionbox.show();
			else
				regionbox.hide();
		}
	}

	regionsselect.change(function(e) {
		e.preventDefault();
		// no country selected
		var regionscheckitem = myForm.regions[2].selected;

		if(regionscheckitem)
			regionbox.show();
		else
			regionbox.hide();
	});

	if(currentForm=='user_profileForm') {
		// var myForm = eval( 'document.' + currentForm );
		if(myForm.country.value == '')
			regionbox.hide();
	}

	countryselect.change(function(e) {
		e.preventDefault();
		// no country selected
		var countrycheckitem = myForm.country[0].selected;

		if(countrycheckitem)
			regionbox.hide();
		else
			regionbox.show();
	});

	// register_interview.php STEP4
	if(currentForm=='register_interviewForm3' || currentForm=='register_interviewForm4' || currentForm=='user_matchForm') {
		// define vars
		var regionsbox_ch = $('#ch_regions');
		var regionsbox_de = $('#de_regions');
		var regionsbox_at = $('#at_regions');
		var regions_ch_selected = $('#regions_ch_selected');
		var regions_de_selected = $('#regions_de_selected');
		var regions_at_selected = $('#regions_at_selected');
		var regions_ch_all = $('#regions_ch_all');
		var regions_de_all = $('#regions_de_all');
		var regions_at_all = $('#regions_at_all');
		var country_ch = $('#country_ch');
		var country_de = $('#country_de');
		var country_at = $('#country_at');

		// init
		if(myForm.country_ch.checked == false) {
			myForm.regions_ch[0].checked = false;
			myForm.regions_ch[1].checked = false;
			// hide regionsbox
			regionsbox_ch.hide();
		} else if (myForm.country_ch.checked == true && myForm.regions_ch[0].checked == true) {
			regionsbox_ch.hide();
		}

		if(myForm.country_de.checked == false) {
			myForm.regions_de[0].checked = false;
			myForm.regions_de[1].checked = false;
			// hide regionsbox
			regionsbox_de.hide();
		} else if (myForm.country_de.checked == true && myForm.regions_de[0].checked == true) {
			regionsbox_de.hide();
		}

		if(myForm.country_at.checked == false) {
			myForm.regions_at[0].checked = false;
			myForm.regions_at[1].checked = false;
			// hide regionsbox
			regionsbox_at.hide();
		} else if (myForm.country_at.checked == true && myForm.regions_at[0].checked == true) {
			regionsbox_at.hide();
		}

		// show / hide regionsbox CH
		regions_ch_selected.click(function(e) {

			myForm.country_ch.checked = true;
			regionsbox_ch.show();
		});

		regions_ch_all.click(function(e) {
			myForm.country_ch.checked = true;
			regionsbox_ch.hide();
		});

		// show / hide regionsbox DE
		regions_de_selected.click(function(e) {
			myForm.country_de.checked = true;
			regionsbox_de.show();
		});

		regions_de_all.click(function(e) {
			myForm.country_de.checked = true;
			regionsbox_de.hide();
		});

		// show / hide regionsbox AT
		regions_at_selected.click(function(e) {
			myForm.country_at.checked = true;
			regionsbox_at.show();
		});

		regions_at_all.click(function(e) {
			myForm.country_at.checked = true;
			regionsbox_at.hide();
		});

		country_ch.click(function(e) {
			if(myForm.country_ch.checked == true) {
				if(myForm.regions_ch[0].checked == false && myForm.regions_ch[1].checked == false)
					myForm.regions_ch[0].checked = true;
			} else {
				myForm.regions_ch[0].checked = false;
				myForm.regions_ch[1].checked = false;
				regionsbox_ch.hide();
			}
		});

		country_de.click(function(e) {
			if(myForm.country_de.checked == true) {
				if(myForm.regions_de[0].checked == false && myForm.regions_de[1].checked == false)
					myForm.regions_de[0].checked = true;
			} else {
				myForm.regions_de[0].checked = false;
				myForm.regions_de[1].checked = false;
				regionsbox_de.hide();
			}
		});

		country_at.click(function(e) {
			if(myForm.country_at.checked == true) {
				if(myForm.regions_at[0].checked == false && myForm.regions_at[1].checked == false)
					myForm.regions_at[0].checked = true;
			} else {
				myForm.regions_at[0].checked = false;
				myForm.regions_at[1].checked = false;
				regionsbox_at.hide();
			}
		});

	}

	// keyword-search
	$('#keys').mousedown(function() {
		$('#keys').focus();
		$('#keys').val('');
	});
    
});

/**
 * borrowed from Joomla! CMS
 *
 * Writes a dynamically generated list
 * @param string The parameters to insert into the <select> tag
 * @param array A javascript array of list options in the form [key,value,text]
 * @param string The key to display for the initial state of the list
 * @param string The original key that was selected
 * @param string The original item value that was selected
 */
function writeDynaList( selectParams, source, key, orig_key, orig_val ) {
	
	var html = '\n<select ' + selectParams + '>';
	var i = 0;
	for (x in source) {
		if (source[x][0] == key) {
			var selected = '';
			if ((orig_key == key && orig_val == source[x][1]) || (i == 0 && orig_key != key)) {
				selected = '<option value="'+source[x][1]+'" selected="selected">'+source[x][2]+'</option>';
				html += '\n'+selected;
			} else {
				html += '\n<option value="'+source[x][1]+'">'+source[x][2]+'</option>';
			}
		}
		i++;
	}
	html += '\n</select>';
	
	document.writeln( html );
}

/**
 * borrowed from Joomla! CMS
 *
 * Changes a dynamically generated list
 * @param string The name of the list to change
 * @param array A javascript array of list options in the form [key,value,text]
 * @param string The key to display
 * @param string The original key that was selected
 * @param string The original item value that was selected
 */
function changeDynaList( formname, listname, source, key, orig_key, orig_val ) {
	
	var list = eval( 'document.' + formname + '.' + listname );
	
	// empty the list
	for (i in list.options.length) {
		list.options[i] = null;
	}
	i = 0;
	for (x in source) {
		if (source[x][0] == key) {
			opt = new Option();
			opt.value = source[x][1];
			opt.text = source[x][2];
			
			if ((orig_key == key && orig_val == opt.value) || i == 0) {
				opt.selected = true;
			}
			list.options[i++] = opt;
		}
	}
	list.length = i;
}


// common functions
function openWindow (url) {
	
	var wWidth = 550;
	var wHeight = 460;
		
	var l = (screen.availWidth - wWidth) / 2;
	var t = (screen.availHeight - wHeight) / 2;
	
	myWindow = window.open(url, "extraWindow", "width=" + wWidth + ",height=" + wHeight + ",left=" + l + ",top=" + t + ",status=no,scrollbars=no,resizable=no,location=no");
	myWindow.focus();
	
	return false;
}

function openInfoWindow (url, wWidth, wHeight) {
	
	var l = (screen.availWidth - wWidth) / 2;
	var t = (screen.availHeight - wHeight) / 2;
	
	myWindow = window.open(url, "extraWindow", "width=" + wWidth + ",height=" + wHeight + ",left=" + l + ",top=" + t + ",status=no,scrollbars=yes,resizable=no,location=no");
	myWindow.focus();
	
	return false;
}

function askUserPopup (user) {
	
	var wWidth = 800;
	var wHeight = 600;
	var url = "user_questions.php?questioner="+user;
		
	var l = (screen.availWidth - wWidth) / 2;
	var t = (screen.availHeight - wHeight) / 2;
	
	myWindow = window.open(url, "extraWindow", "width=" + wWidth + ",height=" + wHeight + ",left=" + l + ",top=" + t + ",status=no,scrollbars=yes,resizable=no,location=no");
	myWindow.focus();
	
	return false;
}
