window.log = function(){
	log.history = log.history || [];   // store logs to an array for reference
	log.history.push(arguments);
	if(this.console){
		console.log( Array.prototype.slice.call(arguments) );
	}
};

// catch all document.write() calls
(function(doc){
	var write = doc.write;
	doc.write = function(q){ 
		log('document.write(): ',arguments); 
		if (/docwriteregexwhitelist/.test(q)) write.apply(doc,arguments);  
	};
})(document);



jQuery.fn.uniform.language = {
    required      : '<em>%s</em> is required',
    req_radio     : 'Please make a selection',
    req_checkbox  : 'You must select this checkbox to continue',
    minlength     : '<em>%s</em> should be at least %d characters long',
    min           : '<em>%s</em> should be greater than or equal to %d',
    maxlength     : '<em>%s</em> should not be longer than %d characters',
    max           : '<em>%s</em> should be less than or equal to %d',
    same_as       : '<em>%s</em> is expected to be same as <em><em>%s</em></em>',
    email         : '<em>%s</em> is not a valid email address',
    url           : '<em>%s</em> is not a valid URL',
    number        : '<em>%s</em> needs to be a number',
    integer       : '<em>%s</em> needs to be a whole number',
    alpha         : '<em>%s</em> should contain only letters (without special characters or numbers)',
    alphanum      : '<em>%s</em> should contain only numbers and letters (without special characters)',
    phrase        : '<em>%s</em> should contain only alphabetic characters, numbers, spaces, and the following: . , - _ () * # :',
    phone         : '<em>%s</em> should be a phone number',
    date          : '<em>%s</em> should be a date (mm/dd/yyyy)',
    callback      : 'Failed to validate <em>%s</em> field. Validator function (<em>%s</em>) is not defined!',
    on_leave      : 'Are you sure you want to leave this page without submitting this form?',
    submit_msg    : 'Sorry, this form needs to be corrected.',
    submit_help   : 'Please see the items marked below.',
    submit_success: 'Thank you, this form has been sent.'
};


jQuery.fn.uniform.defaults = {
    prevent_submit          : true,
    prevent_submit_callback : false,
    ask_on_leave            : false,
    on_leave_callback       : false,
    valid_class             : 'valid',
    invalid_class           : 'invalid',
    error_class             : 'error',
    focused_class           : 'focused',
    holder_class            : 'ctrlHolder',
    field_selector          : 'input, textarea, select',
    default_value_color     : "#AFAFAF"
};




	
// -------------------------------
// AJAX LOGIN AND LOGOUT FUNCTIONS
// -------------------------------

// Logout Function

$(document).ready(function() {
	
	$('a#loginLink').click(function(e) {
		e.preventDefault();

		$('#loginbox').show('slow', function() {
			// Animation complete.
		});
	});
	$('form.uniForm').uniform();

	$('form.uniForm').uniform();

	function member_logout() {

		$("#logout").click(function(e) {

			$.get('{path=LOGOUT}', function(data) {

				$('#login_status')
				.html('You are logged out!')
				.removeClass('loggedin')
				.addClass('loggedout');
				$('#member_loggedin').hide();
				$('#loginForm').fadeIn('slow');

			});

			e.preventDefault();

		});
	}
		
	member_logout();
		
	// // Login Function 

	$("#btnLogin").click(function(e){

		$.post($('#loginForm').attr('action'), $("#loginForm").serializeArray(), function(data){

			var title = $(data).filter(function(){ return $(this).is('title') });

			title.each(function() {

				// Check if the login page returns an error.
				if($(this).html() == "Error") {
					$('#login_status').html('Username or password incorrect. Please try again. <a href="{path=user/forgot_password}">Forgot password?</a>');
				}

				// If no error, you are logged in 
				if($(this).html() != "Error") {
					$('#login_status').html('You are logged in! <a href="{path=user/edit_account}">Edit Profile</a>. <a id="logout" href="#">Logout</a>').fadeIn('500');

					// Change class of box

					$('#login_status').removeClass('loggedout').addClass('loggedin');

					// remove login form when person is logged in
					$('#loginForm').fadeOut();

					// bind a logout function so that it can be used 
					member_logout();

				}

			})


		});

		e.preventDefault();

	}); /*end click*/


	//GLOBAL NOTIFICATIONS
	$(".notification").removeClass('noScript').addClass('hasScript').slideDown();

	$(".notification div").append("<a href='#' class='closethis'>&times;</a>");

	$(".notification").click(function(){
		$(".notification").fadeOut();
	});

	if(!$(".notification").hasClass('sticky'))
	{
	}
		

	//LIGHTBOX
	$('a.lightbox').lightBox({
		fixedNavigation : true
	}); // Select all links with lightbox class

	//SUPERFISH
	$('ul.sf-menu').superfish({
		// pathClass:     'current', // enable if you use horizontal "sticky" menus
		delay: 200 
	}); 
	
	$('.tabs-box').tabs();
	

	//SLIDESHOWS
	$('.promoSlide').each(function(e){
		makeSlideShow($(this), {pager : ".promoSlidePager"+e});
	});
	
	// makeSlideShow($('.flickr-feed'));
	
	function makeSlideShow(promoSlide, options){
		
		var h = promoSlide.find('img').height();
		var w = promoSlide.find('img').width();
		promoSlide.height(h);
		promoSlide.width(w);
		
		promoSlideCSS =	{
							'height': h+'px',
							'position' : 'relative',
							'z-index' : '5'
						};

		promoSlide.css(promoSlideCSS);	

		// $('#promoSlide .slideItem').height(h).width(w);

		SlideCaptionCSS =	{
								'bottom': -1*$(this).height(),
								'opacity' : '0.7',
								'width' : '100%',
								'position' : 'absolute'
							};
					
		$('#promoSlide .slideCaption').each(function(){
			$(this).css(SlideCaptionCSS);
		});

		function onBefore() {
		}

		function onAfter() {
			sCaption = $(this).find('.slideCaption');
			sCaptionH = -1*sCaption.height();
			sCaption.show()
				.animate({opacity: 0.7}, 500)
				.animate({bottom: "0px"}, { queue:false, duration: 500 })
				.animate({opacity: 0.7}, 2000)
				.animate({opacity: 0.0, bottom: sCaptionH}, 500);
		}

		function initSlideshow(slideShow)
		{
			// slideShow.find('img').fadeIn();
			slideShow.find(".hidden").removeClass('hidden');

			if(options.pager) slideShow.after("<div class='"+options.pager.substr(1)+"'></div>");
			
			defaultOptions = {
			    timeout: 5000, 
				fx: 'fade',
				speed: 1000,
				before: onBefore,
				after: onAfter,
				pause:   1
			}
			
			if(typeof(options) != 'undefined'){
				var cycleOptions = $.extend(defaultOptions, options);
			}
			
			log(cycleOptions);
			
			slideShow.cycle(cycleOptions);
		}
		
		function is_image_loaded(image) {
			if(!image.complete) {
				return false;
			}
			if(typeof image.naturalWidth != "undefined" && image.naturalWidth == 0) {
				return false;
			}
			return true;
		};

		var first_slide = $(promoSlide).find('img:first');

		if(first_slide.length > 0){
			if(is_image_loaded(first_slide.get(0))){
				initSlideshow($(promoSlide));
			} else {
				first_slide.load(function(e) {
					$('#loading').hide();
					initSlideshow($(promoSlide));
				});
			}
		}
		
	}
	

});


