/* _                          _      _   
  (_)__ ___ ____ _ ___ __ _ _(_)_ __| |_ 
  | / _` \ V / _` (_-</ _| '_| | '_ \  _|  http://www.ubora.nl
 _/ \__,_|\_/\__,_/__/\__|_| |_| .__/\__|  contact@ubora.nl
|__/ Developed by Ubora © 2011 |_|                          */

$(document).ready(function() {
	CheckBrowserVersion(BrowserErrorText);

	// ERROR HANDLING
	// ==============

	// Show error dialog if a wrong confirmationcode was entered on a form
	if (ConfirmationErrorText) {
		$('#AlertDialog').remove();
		$(document.body).append('<div id="AlertDialog" title="&nbsp;"><p>' + ConfirmationErrorText + '</p></div>');
		$('#AlertDialog').dialog( {
			modal: true, width: 350, resizable: false,
			close: function(event, ui) {
				$("html:not(:animated),body:not(:animated)").animate({scrollTop: $('#form').offset().top-10}, 1000 );
			},
			buttons: {
				Ok: function() {
					$(this).dialog("close");
				}
			}
		});
	}

	// Show error dialog if a submitted form contained a too large file
	if (FilesizeErrorText) {
		$('#AlertDialog').remove();
		$(document.body).append('<div id="AlertDialog" title="&nbsp;"><p>' + FilesizeErrorText + '</p></div>');
		$('#AlertDialog').dialog( {
			modal: true, width: 350, resizable: false,
			close: function(event, ui) {
				$("html:not(:animated),body:not(:animated)").animate({scrollTop: $('#form').offset().top-10}, 1000 );
			},
			buttons: {
				Ok: function() {
					$(this).dialog("close");
				}
			}
		});
	}

	// Show error dialog if sending mail form caused an error
	if (SendingErrorText) {
		$('#AlertDialog').remove();
		$(document.body).append('<div id="AlertDialog" title="&nbsp;"><p>' + SendingErrorText + '</p></div>');
		$("#AlertDialog").dialog( {
			modal: true, width: 350, resizable: false,
			close: function(event, ui) {
				$("html:not(:animated),body:not(:animated)").animate({scrollTop: $('#form').offset().top-10}, 1000 );
			},
			buttons: {
				Ok: function() {
					$(this).dialog("close");
				}
			}
		});
	}

	// Show error dialog if saving information caused an error
	if (SavingErrorText) {
		$('#AlertDialog').remove();
		$(document.body).append('<div id="AlertDialog" title="&nbsp;"><p>' + SavingErrorText + '</p></div>');
		$("#AlertDialog").dialog( {
			modal: true, width: 350, resizable: false,
			close: function(event, ui) {
				$("html:not(:animated),body:not(:animated)").animate({scrollTop: $('#form').offset().top-10}, 1000 );
			},
			buttons: {
				Ok: function() {
					$(this).dialog("close");
				}
			}
		});
	}

	// FANCYBOX LINKS
	// ==============

	// Images
	$("a[href$='.jpg'],a[href$='.png'],a[href$='.gif']").fancybox({ 
		'titlePosition' : photo_title_location, 'transitionIn': photo_zoom_effect, 'transitionOut': photo_zoom_effect, 'speedIn': 500, 'speedOut': 500
	});

	// Iframe
	$("a.iframe").fancybox({ 'width': 997, 'height': window.innerHeight });

	// Youtube
	$("a[href^='http://www.youtube.com/watch']").live('click', function() {
		$.fancybox({
			'padding': 0, 'autoScale': false, 'transitionIn': photo_zoom_effect, 'transitionOut': photo_zoom_effect, 'width': 640, 'height': 385, 'href': this.href.replace(new RegExp("watch\\?v=", "i"), 'v/')+'?autoplay=1', 'type': 'swf'
		});
		return false;
	});

	$("a[href^='http://youtu.be/']").live('click', function() {
		$.fancybox( {
			'padding': 0, 'autoScale': false, 'transitionIn': photo_zoom_effect, 'transitionOut': photo_zoom_effect, 'width': 640, 'height': 385, 'href': this.href.replace(new RegExp("youtu.be/", "i"), 'www.youtube.com/v/')+'?autoplay=1', 'type': 'swf'
		});
		return false;
	});

	// Vimeo
	$("a[href^='http://vimeo.com/']").live('click', function() {
		$.fancybox( {
			'padding': 0, 'autoScale': false, 'transitionIn': photo_zoom_effect, 'transitionOut': photo_zoom_effect, 'width': 640, 'height': 360, 'href': this.href.replace(new RegExp("([0-9])","i"),'moogaloop.swf?autoplay=1&clip_id=$1'), 'type': 'swf'
		});
		return false;
	});

	// Uitzending Gemist
	$("a[href^='http://gemi.st/']").live('click', function() {
		$.fancybox( {
			'padding': 0, 'autoScale': false, 'transitionIn': photo_zoom_effect, 'transitionOut': photo_zoom_effect, 'width': 640, 'height': 360, 'href': this.href.replace('http://gemi.st/', 'http://embed.player.omroep.nl/fle/ugfl.swf?episodeID=')+'&volume=100', 'type': 'swf'
		});
		return false;
	});

	// CHANGE CONTENT OF LINKS
	// =======================

	$("a").each(function() {
		var text = $(this).text();

		// Youtube
		if (text.substring(0,23) == "http://www.youtube.com/") {
			var results = text.match("[\\?&]v=([^&#]*)");
			var vid = (results === null) ? text:results[1];
			$(this).html("<div style='text-align:center;'><img src=\"http://img.youtube.com/vi/"+vid+"/0.jpg\" width=\"480\" border=\"0\"></div>");
			$(this).append("<div style='position:relative; margin:0px; text-align:center; height:223px; margin-top:-225px;'><img src='/sitemanager/images/play.png' border='0' /></div>");
		}
		// Youtube
		else if (text.substring(0,16) == "http://youtu.be/") {
			var results = text.match("[\\?be]/([^&#]*)");
			var vid = (results === null) ? text:results[1];
			$(this).html("<div style='text-align:center;'><img src=\"http://img.youtube.com/vi/"+vid+"/0.jpg\" width=\"480\" border=\"0\"></div>");
			$(this).append("<div style='position:relative; margin:0px; text-align:center; height:223px; margin-top:-225px;'><img src='/sitemanager/images/play.png' border='0' /></div>");
		}
		// Vimeo
		else if (text.substring(0,17) == "http://vimeo.com/") {
			var current_link = $(this);
			var video_id = text.substring(17,80);

			$.getJSON('http://www.vimeo.com/api/v2/video/' + video_id + '.json?callback=?', {format: "json"}, function(data) {
				current_link.html("<div style='text-align:center;'><img src=\"" + data[0].thumbnail_large + "\" width=\"480\" border=\"0\"></div>");
				current_link.append("<div style='position:relative; margin:0px; text-align:center; height:170px; margin-top:-175px;'><img src='/sitemanager/images/play.png' border='0' /></div>");
			});
		}
	});

	// Make all date fields in folders show the datepicker
	$('.datefield').datepicker( {
		dateFormat: DateFormat,
		showWeek: true,
		changeMonth: true,
		changeYear: true,
		yearRange: '1900:2100',
		firstDay: 1,
		monthNamesShort: DateMonthsShort,
		monthNames: DateMonthsLong,
		dayNamesMin: DateDaysShort,
		dayNames: DateDaysLong
	});
});

// Check on browser version. If browser version is old, dialog message is shown.
function CheckBrowserVersion(message) {
	if (message) {
		if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)) { var ffversion=new Number(RegExp.$1); if (ffversion<6) { var BrowserVersionError = "Firefox";	} }
		else if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { var ieversion=new Number(RegExp.$1); if (ieversion<8) { var BrowserVersionError = "Internet Explorer"; } }
		else if (/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent)) { var oprversion=new Number(RegExp.$1); if (oprversion<9) {	var BrowserVersionError = "Opera"; } }
		else if (/Chrome[\/\s](\d+\.\d+)/.test(navigator.userAgent)) { var chrversion=new Number(RegExp.$1); if (chrversion<1) { var BrowserVersionError = "Chrome"; } }
		else if (/Safari[\/\s](\d+\.\d+)/.test(navigator.userAgent)) { var safversion=new Number(RegExp.$1); if (safversion<3) { var BrowserVersionError = "Safari"; } }

		if (BrowserVersionError && StatusErrorCookie() == null) {
			$('#AlertDialog').remove();
			$(document.body).append('<div id="AlertDialog" title="Error"><p>' + message + '</p><table width="100%" border="0"><tr><td align="center"><img src="/sitemanager/images/logos/icon-firefox.png"> <a href="http://www.mozilla.com/firefox" target="_blank" style="color: #000000;">Mozilla Firefox</a></td><td align="center"><img src="/sitemanager/images/logos/icon-chrome.png"> <a href="http://www.google.com/chrome" target="_blank" style="color: #000000;">Google Chrome</a></td><td align="center"><img src="/sitemanager/images/logos/icon-ie.png"> <a href="http://www.microsoft.com/ie" target="_blank" style="color: #000000;">MS Internet Explorer</a></td><td align="center"><img src="/sitemanager/images/logos/icon-safari.png"> <a href="http://www.apple.com/safari" target="_blank" style="color: #000000;">Apple Safari</a></td></tr></table></div>');
			$("#AlertDialog").dialog( {
				modal: true, width: 640, resizable: false,
				buttons: {
					Ok: function() {
						$(this).dialog("close");
					}
				}
			});

			var date = new Date();
			date.setTime(date.getTime()+(365*24*60*60*1000));
			var expires = "expires="+date.toGMTString();
			document.cookie = "HideBrowserError=hide;"+expires+"; path=/";
		}
	}
}

// Function to check if error message of old browser was already shown.
function StatusErrorCookie() {
	var nameEQ = "HideBrowserError=";
	var ca = document.cookie.split(';');

	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

// Default popup window function.
function Open(page) {
	if (popup_horizontal == 'left') { var x = 5; }
	else if (popup_horizontal == 'center') { var x = (screen.width - popup_w) / 2; }
	else if (popup_horizontal == 'right') {	var x = (screen.width - popup_w) + 5; }

	if (popup_vertical == 'top') { var y = 5; }
	else if (popup_vertical == 'middle') { var y = (screen.height - popup_h - 30) / 2; }
	else if (popup_vertical == 'bottom') { var y = (screen.height - popup_h - 30); }

	winprops = 'height='+popup_h+',width='+popup_w+',top='+y+',left='+x+',scrollbars=yes',
	detailwindow = window.open(page,'PopupWindow',winprops);
	if (parseInt(navigator.appVersion) >= 4) { detailwindow.window.focus(); }
}
