var Site;

(function($) {
	Site = {

		// This vars should be set in <head> server-side
		config: {
			base_url: '',
			site_url: ''
		},
	
		// This method is called on every page
		init: function() {
			
			// On Dom Ready
			$(function() {
				$("a.group").fancybox({
					'hideOnContentClick': false,
					'autoScale': false,
					'centerOnScroll': false,
					'titlePosition': 'over'
				});
				$("a.tour_modal").fancybox({
					'hideOnContentClick': false,
					'autoScale': false,
					'centerOnScroll': false,
					'width': '320',
					'height': '256',
					'titlePosition': 'outside'
				});
				$("a.video_link").fancybox({
					'hideOnContentClick': false,
					'width'				: '75%',
					'height'			: '75%',
			        'autoScale'     	: false,
			        'transitionIn'		: 'none',
					'transitionOut'		: 'none',
					'type'				: 'iframe'
				});
			});
		},
		
		frontend: {
			init: function() {
				$(function() {
					$('#nav li').bind('mouseover', function() {
						$(this).addClass('active_nav_link');
						$('ul', $(this)).show();
					}).bind('mouseout', function() {
						$(this).removeClass('active_nav_link');
						$('ul', $(this)).hide();
					});
					$('#events_right').cycle({ 
					    fx:    'scrollHorz', 
					    speed: 400,
					    cleartypeNoBg: true,
						next: '#next_events_col_link',
						prev: '#prev_events_col_link',
						after: Site.frontend.right_slider
					}).cycle('pause');	
					Site.frontend.eventsglance();
				});
			},
			eventsglance: function() {
				$(function() {
					$('#events_at_a_glance ul li').bind('mouseover', function() {
						$this = $(this);
						var pos = $this.offset();  
						var width = $this.width();
						var top = pos.top + 25;
						var left = pos.left - 62;
						$('.event_tooltip', $this).css( { "left": (left + width) + "px", "top":top + "px" } );
						$('.event_tooltip', $this).show();
					}).bind('mouseout', function() {
						$this = $(this);
						$('.event_tooltip', $this).hide();
					});
					$('#events_at_a_glance_ajax .next a').bind('click', function(e) {
						e.preventDefault();
						var ajax_month = $('#events_at_a_glance_ajax .next a').attr('rel');
						$('#events_at_a_glance_ajax .next a').attr('rel', parseInt(ajax_month) + 1);
						$('#events_at_a_glance_ajax').load('/frontend/eventsglance/' + ajax_month);
					});
					$('#events_at_a_glance_ajax .previous a').bind('click', function(e) {
						e.preventDefault();
						var ajax_month = $('#events_at_a_glance_ajax .previous a').attr('rel');
						$('#events_at_a_glance_ajax .previous a').attr('rel', parseInt(ajax_month) - 1);
						$('#events_at_a_glance_ajax').load('/frontend/eventsglance/' + ajax_month);
					});
				});
			},
			right_slider: function() {
				$(function() {
					if ($('#events_right #first_event_col').css('display') == 'none')
					{
						$('#prev_events_col_link').show();
					}
					else
					{
						$('#prev_events_col_link').hide();
					}
					if ($('#events_right #last_event_col').css('display') == 'none')
					{
						$('#next_events_col_link').show();
					}
					else
					{
						$('#next_events_col_link').hide();
					}
				});
			}
		},
		
		tour: {
			wmode: function() {
				$(function() {
					$('#nav li').bind('mouseover', function() {
						$('embed').attr('wmode', 'transparent');
						$('object').html('<param name="wmode" value="transparent" />' + $('object').html());
					}).bind('mouseout', function() {
						$(this).removeClass('active_nav_link');
						$('embed').removeAttr('wmode');
						$('param[name=wmode]').replaceWith('');
					});
				});
			},
			accordion: function() {
				$(function() {
					$('a[rel*=accordion]').simple_accordion({active:'less_info'});
				});
			}
		},
		
		admin: {
			listing: function() {
				$(function() {
					$('.confirm').bind('click', function(e) {
						e.preventDefault();
						var where_to = confirm('Do you really want to delete this event?');
						 if (where_to == true)
						 {
						   window.location = $(this).attr('href');
						 }
					});
				});
			},
			filter: function(current_url) {
				$(function() {
					$('#status_select, #date_select').bind('change', function() {
  						$('#filter_form').submit();
					});
				});
			},
			add_edit: function() {
				$(function() {
					
					$('textarea.tinymce').tinymce({
						// Location of TinyMCE script
						script_url : '/js/tiny_mce/tiny_mce.js',

						// General options
						content_css : "css/frontend.css",
						theme : "advanced",
						theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,undo,redo,|,cleanup,|,bullist,numlist,|,link,unlink",
						theme_advanced_buttons2 : "",
						theme_advanced_buttons3 : "",
						theme_advanced_toolbar_location: "bottom"

					});
					
					$('#event_form').ketchup();
					
					$('#posted_at').DatePicker({
						format:'m/d/Y',
						date: $('#posted_at').val(),
						current: $('#posted_at').val(),
						starts: 1,
						position: 'right',
						onBeforeShow: function(){
							$('#posted_at').DatePickerSetDate($('#posted_at').val(), true);
						},
						onChange: function(formated, dates){
							$('#posted_at').val(formated);
							$('#posted_at').DatePickerHide();
						}
					});
					
					$('#start_date').DatePicker({
						format:'m/d/Y',
						date: $('#start_date').val(),
						current: $('#start_date').val(),
						starts: 1,
						position: 'right',
						onBeforeShow: function(){
							$('#start_date').DatePickerSetDate($('#start_date').val(), true);
						},
						onChange: function(formated, dates){
							$('#start_date').val(formated);
							$('#start_date').DatePickerHide();
							Site.admin.showtimes();
						}
					});
					$('#end_date').DatePicker({
						format:'m/d/Y',
						date: $('#end_date').val(),
						current: $('#end_date').val(),
						starts: 1,
						position: 'right',
						onBeforeShow: function(){
							$('#end_date').DatePickerSetDate($('#end_date').val(), true);
						},
						onChange: function(formated, dates){
							$('#end_date').val(formated);
							$('#end_date').DatePickerHide();
							Site.admin.showtimes();
						}
					});
					$('#on_sale').DatePicker({
						format:'m/d/Y',
						date: $('#on_sale').val(),
						current: $('#on_sale').val(),
						starts: 1,
						position: 'right',
						onBeforeShow: function(){
							$('#on_sale').DatePickerSetDate($('#on_sale').val(), true);
						},
						onChange: function(formated, dates){
							$('#on_sale').val(formated);
							$('#on_sale').DatePickerHide();
						}
					});
					Site.admin.showtimes();
				});
			},
			showtimes: function(current_url) {
				$(function() {
					var options = { 
					        target: '#showtimes_container', 
							url: '/admin/showtimes/'
					    }; 
						
					$('#event_form').ajaxSubmit(options); 
				});
			}
		}
	};
})(jQuery);

Site.init();
