if(!jsFrontend) { var jsFrontend = new Object(); }

jsFrontend = {
	// datamembers
	debug: false,
	// init, something like a constructor
	init: function() {
		jsFrontend.general.init();
		jsFrontend.favicon.init();
		jsFrontend.forms.init();
		jsFrontend.layout.init();
		jsFrontend.lightbox.init();
		jsFrontend.shoutbox.init();
		jsFrontend.search.init();
		jsFrontend.selectorSupport.init();
		jsFrontend.video.init();
		jsFrontend.dialog.init();
	},
	// end
	_eoo: true
}

jsFrontend.dialog = {
	init: function() {
		if($('.dialog').length > 0) jsFrontend.dialog.dialogButton();
		if($('.dialogStretch').length > 0) jsFrontend.dialog.stretchDialogButton();
	},
	dialogButton: function() {
		$('.dialog').each(function() {
			$($(this).attr('href')).dialog({ autoOpen: false, modal: true, resizable: false, draggable: false, width: 768, dialogClass: 'videoModal' });
		});
		$('.dialog').bind('click', function(evt) {
			evt.preventDefault();
			$($(this).attr('href')).dialog('open');
		});
	},
	stretchDialogButton: function() {
		$('.dialogStretch').each(function() {
			// get data
			var viewportHeight = $(window).height();
			var viewportWidth = $(window).width();
			
			// calculate the height
			var height = parseInt(viewportHeight - 100); 
			var width = parseInt(height * (16/9));

			// fit in window?
			if(width > viewportWidth) {
				// recalculate
				width = parseInt(viewportWidth - 50);
				height = parseInt(width * (9/16));
			}
			// calculate offset
			$($(this).attr('href')).dialog({ autoOpen: false, modal: true, resizable: false, draggable: false, width: width, height: height, dialogClass: 'videoModal' });
		});
		$('.dialogStretch').bind('click', function(evt) {
			evt.preventDefault();
			$($(this).attr('href')).dialog('open');
		});
	},
	// end
	_eoo: true				
}

jsFrontend.favicon = {
	// init, something like a constructor
	init: function() {
		if($('.loadFavicons').length > 0) { jsFrontend.favicon.load(); }
	},
	// load favicons
	load: function() {
		$('.loadFavicons a[href^="http://"]').each(function() {
			var link = $(this);
			var favicon = new Image();
			favicon.src = link.attr('href').replace(/^(http:\/\/[^\/]+).*$/, '$1') +'/favicon.ico';
			favicon.onload = function() { 
				link.attr('style', 'background-image: url('+ favicon.src + ');');
				link.addClass('loaded');
			}
		});
	},
	// end
	_eoo: true		
}

jsFrontend.forms = {
	// datamembers
	buttonHTML: '<a href="#" class="button buttonSubmit"><b>&nbsp;</b><span>{value}</span><i>&nbsp;</i></a>',
	// init, something like a constructor
	init: function() {
		jsFrontend.forms.submitWithLink();
	},
	// load favicons
	submitWithLink: function() {
		if($('.submitWithLink').length > 0) {
			$('form.submitWithLink').each(function() {
				// get id
				var formId = $(this).attr('id');
				
				// validate id
				if(formId != '') {
					// loop every button to be replaced
					$('form#'+ formId + '.submitWithLink input:submit').each(function() { 
						$(this).after(jsFrontend.forms.buttonHTML.replace('{value}', $(this).val()))
								.css({position:'absolute', top:'-2000px'})
								.attr('tabindex', -1); 
					});

					// add onclick event for button (button can't have the name submit)
					$('form#'+ formId + ' a.buttonSubmit').bind('click', function(evt) {
						evt.preventDefault();
						$('form#'+ formId).submit();
					});
				}
			});
		}
	},
	// end
	_eoo: true		
}

jsFrontend.general = {
	// init, something like a constructor
	init: function() {
		$('#searchterm, #newsletterSignupEmail, #registerHomeText').click(function (evt) { $(this).select(); });
	},
	// end
	_eoo: true		
}

jsFrontend.layout = {
	// init, something like a constructor
	init: function() {
		jsFrontend.layout.fixFocus();
		jsFrontend.layout.fancyPriceFormating();
	},
	// someone wanted fancy-price-formating.. It looks nice but it will be slow..
	fancyPriceFormating: function() {
		if($('.fixPriceFormating').length > 0) {
			$('.fixPriceFormating').each(function() {
				var chunks = $(this).html().split(',');
				if(chunks[1] == undefined) chunks[1] = '00';
				var current = chunks[0] + '<span>,'+ chunks[1] + '</span>';
				$(this).html(current);
			});
		}
	},
	// fix focus for some older browsers
	fixFocus: function() {
		$('input, textarea').bind('focus', function(e) { $(this).addClass('focus'); })
							.bind('blur', function(e) { $(this).removeClass('focus'); });
	},
	// end
	_eoo: true
}

jsFrontend.lightbox = {
	// init, something like a constructor
	init: function() {
		if($('a.lightBox').length > 0) {
			$('a.lightBox').lightBox({ txtImage: 'foto', 
										txtOf: 'van', 
										imageLoading: 'http://api.musichall.be/static/jquery_lightbox/images/loading.gif',
										imageBtnClose: 'http://api.musichall.be/static/jquery_lightbox/images/close.gif',
										imageBtnPrev: 'http://api.musichall.be/static/jquery_lightbox/images/prev.gif',
										imageBtnNext: 'http://api.musichall.be/static/jquery_lightbox/images/next.gif',
										imageBlank: 'http://api.musichall.be/static/jquery_lightbox/images/blank.gif'});
		}
	},
	// end
	_eoo: true
}

jsFrontend.shoutbox = {
	interval: null,
	intervalInSeconds: 30,
	// datamembers
	shoutHTML: 	'<div id="comment-{comment_id}" class="shoutboxComment">' +
				'	<div class="avatar av48">' +
				'		<a href="{url}" title="{nick}">' +
				'			<img src="http://api.musichall.be/modulefiles/profiles/avatars/48x48/{avatar}" width="48" height="48" alt="{nick}" />' +
				'		</a>' +
				'	</div>' +
				'	<div class="message">' +
				'		<p class="author secondaryContent"><a href="{url}" title="{nick}">{nick}</a> schreef:</p>' +
				'		{text}' +
				'	</div>' +
				'</div>',
	// init, something like a constructor
	init: function() {
		if($('#shoutbox').length > 0) {
			jsFrontend.shoutbox.counter();
			$('#add-comment-text').bind('keyup', jsFrontend.shoutbox.counter);
			$('#add-comment-button').bind('click', function(evt) {
				evt.preventDefault();
				if($('#add-comment-text').val().length > 500) { $('#modalCommentTooLong').dialog('open'); }
				else jsFrontend.shoutbox.add(evt);
			});
			$('.flag-comment').bind('click', jsFrontend.shoutbox.flag);
			
			if($('#shoutbox form').length > 0) jsFrontend.shoutbox.interval = setInterval(jsFrontend.shoutbox.get, parseInt(jsFrontend.shoutbox.intervalInSeconds * 1000));
			
			$('#modalCommentTooLong').dialog({ autoOpen: false, modal: true, resizable: false, draggable: false, 
				buttons: { 
							'Ja': function() { 
								$(this).dialog('close'); 
								$('#add-comment-text').val($('#add-comment-text').val().substring(0, 500)); 
								jsFrontend.shoutbox.counter(), 
								jsFrontend.shoutbox.add(); 
							},
							'Annuleer': function() { $(this).dialog('close'); return false; }
						 }});
		}
	},
	// count the used characters
	counter: function() {
		if($('#add-comment-text').length > 0) {
			var length = parseInt($('#add-comment-text').val().length);
			
			if(length > 495) { $('#usedCharactersWrapper').addClass('overflow'); } 
			else { $('#usedCharactersWrapper').removeClass('overflow'); }
			
			if(length >= 1 && length <= 500) { $('#add-comment-button').removeClass('disabled'); } 
			else { $('#add-comment-button').addClass('disabled'); }
			
			$('#usedCharacters').html(length.toString());
		}
	},
	// add the comment
	add: function(evt) {
		if(evt != undefined) evt.preventDefault();
		
		$('.textareaHolder').addClass('isSubmitting');
		$('#shoutboxSpinner').show();
		
		$.ajax({ type: 'post', dataType: 'json', cache: false,
			url: $('#add-comment-form').attr('action'), 
			data: $('#add-comment-form').serialize(),
			success: function(json) {
				switch (parseInt(json.status.code)) {
					case 200:
					case 400:
						var toInsert = JS_NETLASH.utils.string.assignFromObject(JS_NETLASH.utils.string.assignFromObject(jsFrontend.shoutbox.shoutHTML, json.content.user), json.content.comment);
						$('#comments').prepend(toInsert);
						$('#comment-'+ json.content.comment.comment_id +' .message').effect('highlight', null, 2500);
						$('#add-comment-form').get(0).reset();
						$('#no-comments').remove();
					break;

					case 500:
					default:
						if(jsFrontend.debug) { alert(json.status.text); }
					break;
				}

				$('.textareaHolder').removeClass('isSubmitting');
				$('#shoutboxSpinner').hide();
				
				jsFrontend.shoutbox.counter();
			},
			error: function(xhr,err,e) { if(jsFrontend.debug) { alert(err + ' ' + e, 'Critical Error'); } }
		});
	},
	// flag a comment
	flag: function(evt) {
		evt.preventDefault();
		
		var id = $(this).attr('rel');
		var url = $(this).attr('href');

		$('#modalCommentFlag').dialog('open'); 
		$('#modalCommentFlag').dialog({ modal: true, resizable: false, draggable: false, 
										buttons: { 
													'Ja': function() { 
																						$.ajax({ type: 'post', dataType: 'json', cache: false,
																							url: url, 
																							data: 'id='+ id,
																							success: function(json) {
																								switch (parseInt(json.status.code)) {
																									case 200:
																									case 400:
																										$($('#flag-'+ id).parent()[0]).html('Gerapporteerd').effect('highlight', null, 2500);
																										if(json.content[0] == 'flagged') $('#comment-'+ id).addClass('reported');
																										if(json.content[0] == 'deleted') $('#comment-'+ id).slideUp('slow', function() {
																											if($('#comments div').length == 0) $('#no-comments').show();
																										});
																									break;
																			
																									case 500:
																									default:
																										if(jsFrontend.debug) { alert(json.status.text); }
																									break;
																								}
																			
																								$('.textareaHolder').removeClass('isSubmitting');
																								$('#shoutboxSpinner').hide();
																								
																								jsFrontend.shoutbox.counter();
																							},
																							error: function(xhr,err,e) { if(jsFrontend.debug) { alert(err + ' ' + e, 'Critical Error'); } }
																						});
																						
																						// close
																						$(this).dialog('close');
																					},
													'Annuleer': function() { $(this).dialog('close'); }
												 } 
									  });
	},
	get: function() {
		var url = $('#shoutbox form').attr('action').replace('add_comment', 'get_comments');
		var receiverId = $('#receiver_id').val();
		var largestId = 0;

		// get largestId
		$('#comments .shoutboxComment').each(function() { 
			var id = parseInt($(this).attr('id').replace('comment-', ''));
			if(id > largestId) largestId = id;
		});
		
		$('#shoutboxLoading').show();

		$.ajax({ type: 'post', dataType: 'json', cache: false,
			url: url,
			data: 'receiver_id='+ receiverId +'&largest_id='+ largestId,
			success: function(json) {
				switch (parseInt(json.status.code)) {
					case 200:
					case 400:
						for(var i in json.content.comments) {
							var toInsert = JS_NETLASH.utils.string.assignFromObject(jsFrontend.shoutbox.shoutHTML, json.content.comments[i]);
							$('#comments').prepend(toInsert);
							$('#comment-'+ json.content.comments[i].comment_id +' .message').effect('highlight', null, 2500);
							$('#no-comments').remove();
						}
					break;

					case 500:
					default:
						clearInterval(jsFrontend.shoutbox.interval);
					break;
				}

				$('#shoutboxLoading').hide();
			},
			error: function(xhr,err,e) { 
				clearInterval(jsFrontend.shoutbox.interval);
				$('#shoutboxLoading').hide();
			}
		});
	},
	 // end of object
	_eoo: true
}

jsFrontend.search = {
	// init, something like a constructor
	init: function() {
		$("#searchterm, #q").autocomplete('/ajax.php?module=search&action=autocomplete', { width: 185, max: 5, selectFirst: false });
		$("#searchterm, #q").bind('blur', function(evt) { if($("#searchterm, #q").val() == '') { $("#searchterm, #q").val('Vul uw zoekterm in'); }	});
	},
	// end
	_eoo: true
}

jsFrontend.selectorSupport = {
	// init, something like a constructor
	init: function() {
		// only do for IE
		if(jQuery.browser.msie) {
			// Rewrite to PHP if possible
			$('.shows tr:last-child td').addClass('lastChild');	// can't be done don't know the differene between dates in future or past
			$('#subnavigation li:last-child a').addClass('lastChild'); // can't be done, don't know how many items there will be.

			// First child
			$('#eventsBrowse h3:first-child').addClass('firstChild');
			$('.sidebarList .sidebarItem:first-child').addClass('firstChild');
			$('#innerSidebar .infoWidget:first-child h3').addClass('firstChild');
			
			// Last child
			$('.sidebarList .sidebarItem:last-child').addClass('lastChild');
			$('.generalMessage ul li:last-child').addClass('lastChild');
			$('#innerSidebar .infoWidget:last-child').addClass('lastChild');

			// If we ever implement #commentReminder
			//$('#commentReminder .item:last-child').addClass('lastChild');
	
			// Fix :focus pseudoselector support for IE6
			$('.inputText, textarea').focus(function() { $(this).addClass('focus'); });
			$('.inputText, textarea').blur(function() { $(this).removeClass('focus'); });
		}
	},
	// end
	_eoo: true
}

jsFrontend.video = {
	// init, something like a constructor
	init: function() {
		if($('#movieModal').length > 0) {
			$('#movieModal').dialog({ autoOpen: true, modal: true, resizable: false, draggable: false, width: 768, dialogClass: 'videoModal',
												close: function(event, ui) { $('#slideshow .playHolder').show(); } 
			});
		}
	},
	// end
	_eoo: true
}

$(document).ready(function() { jsFrontend.init(); });