/*************************************************/
/* @file browser.js								 */
/* @description King's Church browser JavaScript */
/* @modified 01/14/12							 */
/* @author John Goldsmith						 */
/* @email john@reflexionstudios.com				 */
/*************************************************/

/*************/
/* Variables */
/*************/

var banner_init				= 0;
var install_path			= ''; // no trailing slash
var path_to_banner_images	= install_path + '/public/images/browser/banner/'; // with trailing slash
var path_to_ajax 			= install_path + '/ajax/'; // with trailing slash
var cookie_expire			= 7; // days

/************/
/* DOM Init */
/************/

$(document).ready(function(){
	
	init = function()
	{		
		//$('.nav-dropdown').hide();
		$('#overlay').hide();
		hide_modal();
		$('#announcement').hide();
		$('#announcement').delay(500).slideDown('slow');
		$('.inline-comment-reply form').hide();
		$('.rbl-day-content, .rbl-week-content').hide();
	};
	
	/***********/
	/* RBL Day */
	/***********/
	
	$('.rbl-day-header').mouseover(function(){
		var parent = $(this).parent('.rbl-day');
		var target = $(this).parent('.rbl-day').children('.rbl-day-content');
		if ($(target).css('display') == 'none')
		{
			$(parent).children('.rbl-day-header').children('.rbl-day-header-right').css({
				'background-position' : '0 -30px'
			});
			$(parent).children('.rbl-day-header').children('.rbl-day-header-left').css({
				'background-position' : '0 -30px'
			});
		}
		else
		{
			$(parent).children('.rbl-day-header').children('.rbl-day-header-right').css({
				'background-position' : '0 -90px'
			});
			$(parent).children('.rbl-day-header').children('.rbl-day-header-left').css({
				'background-position' : '0 -30px'
			});
		};
	});
	
	$('.rbl-day-header').mouseout(function(){
		var parent = $(this).parent('.rbl-day');
		var target = $(this).parent('.rbl-day').children('.rbl-day-content');
		if ($(target).css('display') == 'none')
		{
			$(parent).children('.rbl-day-header').children('.rbl-day-header-right').css({
				'background-position' : '0 0'
			});
			$(parent).children('.rbl-day-header').children('.rbl-day-header-left').css({
				'background-position' : '0 0'
			});
		}
		else
		{
			$(parent).children('.rbl-day-header').children('.rbl-day-header-right').css({
				'background-position' : '0 -60px'
			});
			$(parent).children('.rbl-day-header').children('.rbl-day-header-left').css({
				'background-position' : '0 0'
			});
		};
	});
	
	$('.rbl-day-header').click(function(){
		var parent = $(this).parent('.rbl-day');
		var target = $(this).parent('.rbl-day').children('.rbl-day-content');
		$(target).toggle();
		if ($(target).css('display') == 'none')
		{
			$(parent).children('.rbl-day-header').children('.rbl-day-header-right').css({
				'background-position' : '0 -30px'
			});
		}
		else
		{
			$(parent).children('.rbl-day-header').children('.rbl-day-header-right').css({
				'background-position' : '0 -90px'
			});
		};
	});
	
	/************/
	/* RBL Week */
	/************/
	
	$('.rbl-week-header').mouseover(function(){
		var parent = $(this).parent('.rbl-week');
		var target = $(this).parent('.rbl-week').children('.rbl-week-content');
		if ($(target).css('display') == 'none')
		{
			$(parent).children('.rbl-week-header').children('.rbl-week-header-right').css({
				'background-position' : '0 -30px'
			});
			$(parent).children('.rbl-week-header').children('.rbl-week-header-left').css({
				'background-position' : '0 -30px'
			});
		}
		else
		{
			$(parent).children('.rbl-week-header').children('.rbl-week-header-right').css({
				'background-position' : '0 -90px'
			});
			$(parent).children('.rbl-week-header').children('.rbl-week-header-left').css({
				'background-position' : '0 -30px'
			});
		};
	});
	
	$('.rbl-week-header').mouseout(function(){
		var parent = $(this).parent('.rbl-week');
		var target = $(this).parent('.rbl-week').children('.rbl-week-content');
		if ($(target).css('display') == 'none')
		{
			$(parent).children('.rbl-week-header').children('.rbl-week-header-right').css({
				'background-position' : '0 0'
			});
			$(parent).children('.rbl-week-header').children('.rbl-week-header-left').css({
				'background-position' : '0 0'
			});
		}
		else
		{
			$(parent).children('.rbl-week-header').children('.rbl-week-header-right').css({
				'background-position' : '0 -60px'
			});
			$(parent).children('.rbl-week-header').children('.rbl-week-header-left').css({
				'background-position' : '0 0'
			});
		};
	});
	
	$('.rbl-week-header').click(function(){
		var parent = $(this).parent('.rbl-week');
		var target = $(this).parent('.rbl-week').children('.rbl-week-content');
		$(target).toggle();
		if ($(target).css('display') == 'none')
		{
			$(parent).children('.rbl-week-header').children('.rbl-week-header-right').css({
				'background-position' : '0 -30px'
			});
		}
		else
		{
			$(parent).children('.rbl-week-header').children('.rbl-week-header-right').css({
				'background-position' : '0 -90px'
			});
		};
	});
	
	/**************/
	/* Navigation */
	/**************/
	
	/*$('.nav-item').mouseover(function() {
		$(this).css({
			'background-position' : '0 -30px'
		});
	});
	
	$('.nav-item').mouseout(function() {
		$(this).css({
			'background-position' : '0 0'
		});
	});*/
	
	/*********/
	/* Modal */
	/*********/
	
	modal = function(obj)
	{
		$('#modal-guts').empty();
		$('#modal-guts').append(obj.data);
		$('#overlay').show();
		$('#overlay').css({
			'height'	: $(document).height(),
			'width'		: $(document).width()
		});
		$('#modal-title').empty();
		$('#modal-title').append(obj.title);
		$('#modal').show();
		
		/* Fix for IE7 and IE8 */
		$('#modal').css({
			'display'	: 'block'
		});
	};
	
	$('#overlay').click(function(){
		hide_modal();
	});
	
	hide_modal = function()
	{
		$('#modal').hide();
		$('#overlay').hide();
		
		/* Fix for IE7 and IE8 */
		$('#modal').css({
			'display'	: 'none'
		});
		$('#overlay').css({
			'display'	: 'none'
		});
	};
	
	/*******************/
	/* Form Submission */
	/*******************/
	
	submit_form = function(form)
	{
		$('#' + form + ' .ajax-loader').css({
			'background-image' : 'url(' + install_path + '/public/images/browser/doodads/ajax-loader.gif)'
		});
		$('#' + form + '-submit').attr({
			disabled : 'disabled'
		});
		$.ajax({
			type		: 'POST',
			url			: path_to_ajax + form,
			data		: $('#' + form).serialize(),
			dataType	: 'html',
			success		: function(data)
				{
					//console.log('submit_form() success');
					$('#' + form + '-response').fadeTo('fast', 0, function() {
						$(this).empty();
						$(this).append(data);
						$(this).fadeTo('fast', 1, function() {
							$('#' + form + ' .ajax-loader').css({
								'background-image' : 'none'
							});
							$('#' + form + '-submit').attr({
								'disabled'	: ''
							});
							//$(':input','#' + form).val('');
						});
					});
				},
			error		: function(data)
				{
					//console.log('submit_form() error');
				},
			complete	: function(data)
				{
					//console.log('submit_form() complete');
				}
		});
		//return false;
	};
	
	$('#contact').submit(function(event) {
		event.preventDefault();
	});
	
	/***************/
	/* Form Inputs */
	/***************/
	
	$('input, textarea, select').focus(function(){
		if ($(this).val() == $(this)[0].defaultValue)
		{
			//$(this).val($(this)[0].defaultValue);
			$(this).val('');
		}
	});
	
	$('input, textarea, select').keydown(function(){
		if ($(this).val() == $(this)[0].defaultValue)
		{
			$(this).val('');
		}
	});
	
	$('input, textarea, select').blur(function(){
		if ($(this).val() == '')
		{
			$(this).val($(this)[0].defaultValue);
		}
	});
	
	/**********/
	/* Banner */
	/**********/
	
	rotate_banner_image = function()
	{	
		$('.banner').css({
			'background-image' : 'url(' + path_to_banner_images + banner_items[banner_init].bg_image + ')'
		});
		if (banner_items[banner_init].url != '')
		{
			var goto_url = banner_items[banner_init].url;
			$('.banner-content').css({
				'cursor'	: 'pointer'
			});
			$('.banner-content').click(function(){
				window.location = goto_url;
			});
		}
		if (banner_items[banner_init].effect == 'fade' || 'Fade')
		{
			$('.banner-content').fadeTo(banner_items[banner_init].effect_speed, 0, function()
			{
				$(this).css({
					'background-image' : 'url(' + path_to_banner_images + banner_items[banner_init].image + ')'
				});
				$(this).fadeTo(banner_items[banner_init].effect_speed, 1, function()
				{
					if (banner_items.length > 1)
					{
						setTimeout('rotate_banner_image()', banner_items[banner_init].timer * 1000);
						if (banner_init < (banner_items.length - 1)) {
							banner_init++;
						} else {
							banner_init = 0;
						};
					};
				});
			});
		};
	};
	
	/***********/
	/* Cookies */
	/***********/
	
	set_cookie = function(name, value, expire)
	{
		if (name && value)
		{
			var cookie_string = name + "=" + escape(value);
			
			if (expire)
			{
				var cookie_expire_offset = 1000 * 60 * 60 * 24 * expire; // ms, sec, min, hours, days
				var cookie_date = new Date();
				cookie_date.setTime(cookie_date.getTime() + cookie_expire_offset);
				cookie_string += "; expires=" + cookie_date.toUTCString();
			};
		
			document.cookie = cookie_string;
		}
		else
		{
			//console.log('Error: cookie name and value not set.');
			return false;
		}
	};
	
	get_cookie = function(name)
	{
		var results = document.cookie.match ('(^|;) ?' + name + '=([^;]*)(;|$)');
		
		if (results)
		{
			return unescape(results[2]);
			//console.log('Retrieved cookie:', name);
		}
		else
		{
			//console.log('Could not retrieve cookie:', name);
			return false;
		}
	}
	
	cookie_exists = function(name)
	{
		var results = document.cookie.match ('(^|;) ?' + name + '=([^;]*)(;|$)');
		if (results)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	/****************************/
	/* Generic DOM Manipulation */
	/****************************/
	
	//$('#paypal-continue').attr('src', install_path + '/public/images/browser/controls/continue_off.png');
	
	$('#paypal-continue').live('mouseover', function(){
		$(this).attr('src', install_path + '/public/images/browser/controls/continue_on.png');
	});
	
	$('#paypal-continue').live('mouseout', function(){
		$(this).attr('src', install_path + '/public/images/browser/controls/continue_off.png');
	});
	
	$('#announcement-close').click(function(){
		$('#announcement').slideUp('slow');
	});
	
	/************/
	/* Comments */
	/************/
	
	submit_comment = function(form_id)
	{
		$('#comment-reply-' + form_id + ' .ajax-loader').css({
			'background-image' : 'url(' + install_path + '/public/images/browser/doodads/ajax-loader.gif)'
		});
		$('#comment-reply-' + form_id + '-submit').attr({
			disabled : 'disabled'
		});
		$.ajax({
			type		: 'POST',
			url			: path_to_ajax + 'submit_comment',
			data		: $('#comment-reply-' + form_id).serialize(),
			dataType	: 'json',
			success		: function(data)
				{
					//console.log('submit_form() success', data);
					$('#comment-reply-' + form_id + '-response').fadeTo('fast', 0, function() {
						$(this).empty();
						$(this).append(data.response);
						$(this).fadeTo('fast', 1, function() {
							$('#comment-reply-' + form_id + ' .ajax-loader').css({
								'background-image' : 'none'
							});
							$('#comment-reply-' + form_id + '-submit').attr({
								'disabled'	: ''
							});
							if (data.status != 500)
							{
								$('#comment-reply-' + form_id)[0].reset();
							}
							if (form_id != 0 && data.status != 500)
							{
								//$('#comment-reply-' + form_id).hide();
							}
						});
					});
				},
			error		: function(data)
				{
					//console.log('submit_comment() error', data);
				},
			complete	: function(data)
				{
					//console.log('submit_comment() complete', data);
				}
		});
		//return false;
	};
	
	$('.comment-reply-form').submit(function(event){
		event.preventDefault();
	});
	
	$('.comment-reply').click(function(event){
		event.preventDefault();
		$('.comment-reply').show();
		$(this).parent().hide();
		$('.inline-comment-reply form').hide();
		var parent = $(this).parent().parent().attr('id');
		$('#' + parent + ' form').show();
	});
	
	$('.inline-comment-cancel').click(function(){
		var parent = $(this).parent().parent().parent().attr('id');
		$('#' + parent + ' .comment-controls').show();
		$('#' + parent + ' form').hide();
	});
	
	/*******************/
	/* Generic Methods */
	/*******************/
	
	slide_down = function(element)
	{
		$(element).slideDown('slow');
	}
	
	slide_up = function(element)
	{
		$(element).slideUp('slow');
	};
	
	show = function(element)
	{
		$(element).css({
			'display' : 'block'
		});
	};
	
	hide = function(element)
	{
		$(element).css({
			'display' : 'none'
		});
	};
	
	do_ajax = function(obj)
	{
		if (obj.modal == 'true' || obj.modal == true)
		{
			//$('html, body').animate({scrollTop : 0}, 'fast');
		}
		// set defaults if not defined in obj
		if (!obj.type || obj.type == null)
		{
			obj.type = 'POST';
		}
		if (!obj.cache || obj.cache == null)
		{
			obj.cache = false;
		}
		if (obj.form && obj.form != null && obj.form != '')
		{
			obj.data = "$('" + obj.form + "').serialize()";
		}
		if (!obj.data || obj.data == null)
		{
			obj.data = '';
		}
		if (!obj.dataType || obj.dataType == null)
		{
			obj.dataType = ''
		}
		if (!obj.callBack || obj.callBack == null)
		{
			obj.callBack = '';
		}
		
		$.ajax({
			type		: obj.type,
			cache		: obj.cache,
			data		: eval(obj.data),
			url			: path_to_ajax + obj.action,
			dataType	: obj.dataType,
			success		: function(data)
				{
					//console.log('do_ajax() success');
					if (obj.callBack != '')
					{
						eval(obj.callBack + "(" + data + ")");
					}
				},
			error		: function(data)
				{
					//console.log('do_ajax() error');
					
				},
			complete	: function(data)
				{
					//console.log('do_ajax() complete');
				}
		});
	}
	
	update_dom = function(obj)
	{
		$(obj.target).empty();
		$(obj.target).append(obj.data);
	}
	
	/***********/
	/* Execute */
	/***********/
	
	init();	
	rotate_banner_image();
	
});
