/**
 * Opens a video dialog window
 *
 * @param url - the url of the flash video
 * @param title - the tile of the movie
 * @param close - text of the close link
 */
function openVideoDialog(url, title, close) {
	var content = '<br /> \
	<a href="' + url + '" style="display:block; width:484px; height:272px;" id="player"></a> \
	<br /><br /> \
	<div style="width:484px; height:20px; text-align: center;"><a href="#" onclick="Dialog.okCallback();">' + close +'</a></div>';

	var height = 330;
	if (Prototype.Browser.IE) {
		height = 340;
	}
	Dialog.alert(content, { className: "alphacube", width: 500, height: height, bgcolor: "#ffffff", title: title});
	
	flowplayer("player", "/flowplayer/flowplayer-3.0.3.swf");
	return false;
}

/**
 * Opens a contact dialog window
 *
 * @param contentUrl - the url to load inton the dialog
 * @param title - the tile of the dialog window
 * @param recipients - the optional recipients (defaults to info@mbbarter.ch)
 */
function openContactDialog(contentUrl, title, recipients) {
	Dialog.alert({url: contentUrl, options: {method: 'get', evalScripts: true}}, {className: "alphacube", width: '415', title: title, onShow: function() {
		dialogResize();
		if (recipients) {
			$('recipients').value = recipients;
		}
	}});
	return false;
}

/**
 * Toggles the optional information in the contact formular
 *
 */
function toggleOptional() {
	$('optional').toggle(); 
	var link = $('optionalLink');
	if (link.innerHTML.indexOf("+") != -1) {
		link.update(link.innerHTML.replace("+","-"));
	} else {
		link.update(link.innerHTML.replace("-","+"));
	}
	dialogResize();
	return false;
}

/**
 * Resizes the dialog content to a fixed dimension
 */
function dialogResize() {
	var win = Windows.focusedWindow;
	var dimension = win.getSize(); 
	if ($('optional').visible()) {
		if (Prototype.Browser.IE) {
			win.setSize(dimension.width, '640', true);
		} else {
			win.setSize(dimension.width, '610', true);
		}
	} else {
		if (Prototype.Browser.IE) {
			win.setSize(dimension.width, '470', true);
		} else {
			win.setSize(dimension.width, '440', true);
		}
	}; 
	win.computeBounds();	
}

/**
 * Sends the contact form
 */
function sendContactRequest() {
	var valid = true;
	var required = new Array("email", "firstname", "lastname");
	
	required.each(function(field) {
		var labelName = "label".concat(field.slice(0, 1).capitalize() ).concat(field.slice(1, field.length));
		if ($F(field) == "") {
			valid=false;
			$(labelName).addClassName("required");
		} else {
			$(labelName).removeClassName("required");
		}
	});	
	
	if (valid) {
		$("requiresInformation").removeClassName("required");
		$("loader").show();
		$("sendButton").disable();

		var url = $('mailer').action;
		var anchor = url.indexOf('#');
		if (anchor != -1) {
			url = url.substring(0, anchor);
		}
		new Ajax.Request(url , {
			method: 'post',
			parameters: Form.serialize('mailer'),
			onSuccess: function(transport) {
				$('mailForm').hide();
				$('successMessage').show();
				var win = Windows.focusedWindow;
				if (Prototype.Browser.IE) {
					win.setSize('415', '60', true);
				} else {
					win.setSize('415', '40', true);
				}
				win.computeBounds();
				window.setTimeout('Dialog.okCallback()', 1500);
			},
			onFailure: function(transport) {
				$('mailForm').hide();
				$('failureMessage').show();
				var win = Windows.focusedWindow;
				if (Prototype.Browser.IE) {
					win.setSize('415', '60', true);
				} else {
					win.setSize('415', '40', true);
				}
				win.computeBounds();
				window.setTimeout('Dialog.okCallback()', 1500);
			}
		});

	} else {
		$("requiresInformation").addClassName("required");
	}
	
};

/**
 * Loads a new area for the locations
 *
 * @param elem - the area link clicked
 * @param url - the url of the area page
 */
function loadArea(elem, url) {
	
	// load content and show box
	new Ajax.Updater('countryTarget', url, { method: 'get', onSuccess: function(transport) {
		$('country').show();
		new Effect.Highlight($('countryTarget').parentNode, { startcolor: '#ffff99', endcolor: '#ffffff', restorecolor: '#ffffff' });
	 }});

	// select new area link
	$$('.areaLink').each(function(link){
		link.removeClassName("active");
	});
	elem.addClassName("active");
	
	return false;
}

/**
 * Loads a new address for the locations
 *
 * @param elem - the address link clicked
 * @param url - the url of the area page
 */
function loadAddress(elem, url) {
	
	// load content and show box
	new Ajax.Updater('addressTarget', url, { method: 'get', onSuccess: function(transport) {
		$('address').show();
		new Effect.Highlight($('addressTarget').parentNode, { startcolor: '#ffff99', endcolor: '#ffffff', restorecolor: '#ffffff' });
	}});

	// select new area link
	$$('.addressLink').each(function(link){
		link.removeClassName("active");
	});
	elem.addClassName("active");
	
	return false;
}

/**
 * Loads a clicked country on the map
 *
 * @param url - the url of the address
 */ 
function loadCountry(url) {
	new Ajax.Updater('countryTarget', url, { 	
		method: 'get',
		onSuccess: function(transport) {
			new Effect.Highlight($('countryTarget').parentNode, { startcolor: '#ffff99', endcolor: '#ffffff', restorecolor: '#ffffff' });
		}
	});
}

/**
 * Clears the countries
 */
function clearCountry() {
	$('countryTarget').update();
}

/**
 * Appends a clicked country on the map
 *
 * @param url - the url of the address
 */ 
function appendCountry(url) {
	new Ajax.Request(url, { 	
		method: 'get',
		onSuccess: function(transport) {
			$('countryTarget').update($('countryTarget').innerHTML + transport.responseText);
		}
	});
}

/**
 * Loads AJAX pages as search result
 */
function loadAjaxResult(url) {
	if (url.split("/").size() == 7) {
		//subpage of locations, e.g. sellcon page
		location.href=url;
	}
	new Ajax.Updater('ajaxResultTarget', url, { 	
		method: 'get',
		onSuccess: function(transport) {
			$('ajaxResult').show();
			new Effect.Highlight($('ajaxResultTarget').parentNode, { startcolor: '#ffff99', endcolor: '#ffffff', restorecolor: '#ffffff' });
		}
	});	
}