﻿var $j = jQuery.noConflict();

var commentsTransitionTime = 500;

var templateUri = '.';
function initGlobals($){
	match = $('head script[src*=bcwc.js]').attr('src').match(/(?:(?:\?|&)templateUri=)([^&]*)/);
	if(match != null)
		templateUri = unescape(match[1]);
		
	$j('#navbar > li:not(:has(ul))').addClass('no-children');
	initHeaderSlideshow($);
	initGalleries($);
	initComments($);
	initWatermark($);
	
	$('.show-comments-controls').each(function(i, e){ $(e).height($(e).children().height()); });

	if($j.browser.msie && $j.browser.version < 8)
		$j('#navbar li > ul').css('position', 'static');
}

function initComments($){
	$('.comments-wrapper').each(function(i, footer){
		var $footer = $(footer);
		var $noComments = $footer.find('.no-comments');
		var $show = $footer.find('.show');
		var $hide = $footer.find('.hide');
		var $comments = $footer.find('ol.comments');
		var $showForm = $footer.find('.show-comments-form');
		var $commentsForm = $footer.find('div.comments-form');
		var $formControl = $commentsForm.children('form');
		var $controls = $commentsForm.find('.controls');
		var $inputs = $formControl.find(':input');
		var $info = $commentsForm.find('.info :input');
		var $progress = $commentsForm.find('.progress');
		var $success = $commentsForm.find('.success');
		var $error = $commentsForm.find('.error');
		
		var fadeOut = function(e, callback){ $(e).fadeOut(500, callback); };
		var fadeIn = function(e, callback){ $(e).fadeIn(500, callback); };
		var slideUp = function(e, callback) { $(e).slideUp(500, callback); };
		var slideDown = function(e, callback) { $(e).slideDown(500, callback); };
		
		var showComments = function(callback){ fadeOut($show); fadeIn($hide); slideDown($comments, callback); };
		var hideComments = function(callback){ fadeOut($hide); fadeIn($show); slideUp($comments, callback); };
		var showCommentsForm = function(callback){ slideUp($showForm); slideDown($commentsForm, callback); };
		var resetCommentsForm = function(){
			slideDown($showForm);
			slideUp($commentsForm, function(){
				$formControl[0].reset();
				$error.text('');
				$inputs.removeAttr('disabled');
			});
		};
		var submitCommentsForm = function(){
			slideUp($controls);
			
			var firstComment = $comments.children().length == 0;
			
			var req = {};
			req.type = 'POST';
			req.url = templateUri+'/comments-ajax.php';
			req.dataType = 'html';
			
			req.data = {};
			$inputs.each(function(){ req.data[$j(this).attr('name')] = $(this).val(); });
			
			req.success = function(data){
				if(firstComment){
					fadeOut($noComments);
					fadeIn($show);
				}
			
				$comments.append(data);
				slideUp($progress);
				slideDown($success);
				
				$info.each(function(){ $(this).attr('defaultValue', $(this).val()); });
				
				$comments.children().removeClass('alt');
				$comments.children(':odd').addClass('alt');
				
				var $newComments = $comments.children('li.new');
				slideDown($newComments,
					function(){
						$newComments.removeClass('new');
						$comments.scrollTo('max', commentsTransitionTime);
					});
			}
			
			req.error = function(req, status, error){
				slideUp($progress);
				$error.text(req.responseText);
				slideDown($error);
				slideDown($controls);
				$inputs.removeAttr('disabled');
			};
			
			$.ajax(req);
			
			$inputs.attr('disabled', true);
			slideUp($error);
			$error.text('');
			slideDown($progress);
			
			slideUp($controls);
		};
		
		$show.children('a').click(function(){ showComments(); });
		$hide.children('a').click(function(){ hideComments(); });
		$showForm.children('a').click(function(){ showCommentsForm(); });
		$formControl.find(':reset').click(function(){ resetCommentsForm() });
		$success.find('button').click(function(){ resetCommentsForm(); });
		$formControl.submit(function(){ submitCommentsForm(); });
		
		$inputs.removeAttr('disabled');
		if($formControl.length > 0)
			$formControl[0].reset();
	});
}

/*

function getCommentsElemnts(id){
	return {
		comments: $j('#comments-'+id),
		noComments: $j('#no-comments-'+id),
		show: $j('#show-comments-'+id),
		hide: $j('#hide-comments-'+id),
		form: $j('#form-comments-'+id),
		showForm: $j('#show-form-comments-'+id),
		info: $j('#form-comments-'+id+' > form .info input'),
		formControl: $j('#form-comments-'+id+' > form')[0],
		controls: $j('#form-comments-'+id+' > form .controls'),
		inputs: $j('#form-comments-'+id+' > form :input'),
		progress:  $j('#form-comments-'+id+' > .progress'),
		success:  $j('#form-comments-'+id+' > .success'),
		error:  $j('#form-comments-'+id+' > .error')
	};
}

function showComments(id) {
	var elems = getCommentsElemnts(id);
	elems.show.fadeOut(commentsTransitionTime);
	elems.hide.fadeIn(commentsTransitionTime);
	elems.comments.slideDown(commentsTransitionTime);
}

function hideComments(id) {
	var elems = getCommentsElemnts(id);
	elems.hide.fadeOut(commentsTransitionTime);
	elems.show.fadeIn(commentsTransitionTime);
	elems.comments.slideUp(commentsTransitionTime);
}

function showCommentsForm(id) {
	var elems = getCommentsElemnts(id);
	elems.showForm.slideUp(commentsTransitionTime);
	
	elems.form.slideDown(commentsTransitionTime);
}

function hideCommentsForm(id, callback){
	var elems = getCommentsElemnts(id);
	elems.showForm.slideDown(commentsTransitionTime);
		
	elems.form.slideUp(commentsTransitionTime, callback);
}

function resetCommentsForm(id) {
	var elems = getCommentsElemnts(id);
	hideCommentsForm(id, function(){
		elems.formControl.reset();
		elems.error.text('');
		elems.inputs.attr('disabled', false);
	});
	elems.controls.slideDown(commentsTransitionTime);
	elems.success.slideUp(commentsTransitionTime);
}

function submitCommentsForm(id) {
	var elems = getCommentsElemnts(id);
	elems.controls.slideUp(commentsTransitionTime);
	
	var firstComment = elems.comments.children().length == 0;
	
	var req = {};
	req.type = 'POST';
	req.url = templateUri+'/comments-ajax.php';
	req.dataType = 'html';
	
	req.data = {};
	elems.inputs.each(function(){ req.data[$j(this).attr('name')] = $j(this).val(); });
	
	req.success = function(data){
		if(firstComment){
			elems.noComments.fadeOut(commentsTransitionTime);
			elems.show.fadeIn(commentsTransitionTime);
		}
	
		elems.comments.append(data);
		elems.progress.slideUp(commentsTransitionTime);
		elems.success.slideDown(commentsTransitionTime);
		
		elems.info.each(function(){ $j(this).attr('defaultValue', $j(this).val()); });
		
		elems.comments.children(':even').removeClass('alt');
		elems.comments.children(':odd').addClass('alt');
		
		var $newComments = elems.comments.children('li.new');
		$newComments.slideDown(commentsTransitionTime,
			function(){
				$newComments.removeClass('new');
				elems.comments.scrollTo('max', commentsTransitionTime);
			});
	};
	
	req.error = function(req, status, error){
		elems.progress.slideUp(commentsTransitionTime);
		elems.error.text(req.responseText);
		elems.error.slideDown(commentsTransitionTime);
		elems.controls.slideDown(commentsTransitionTime);
		elems.inputs.attr('disabled', false);
	};
	
	$j.ajax(req);
	
	elems.inputs.attr('disabled', true);
	elems.error.slideUp(commentsTransitionTime);
	elems.error.text('');
	elems.progress.slideDown(commentsTransitionTime);
	
	elems.controls.slideUp(commentsTransitionTime);
}

*/

function initWatermark($){
	$('.watermark').each(function (i,elem){
		var $elem = $(elem);
		var watermarkText = $elem.val();
		
		$elem.blur(function(){
			if($elem.val() == '') {
				$elem.val(watermarkText);
				$elem.addClass('watermark');
			}
		});
		
		$elem.focus(function(){
			if($elem.val() == watermarkText){
				$elem.val('');
				$elem.removeClass('watermark');
			}
		});
	});
}

function initGalleries($){
	var imgSrc = templateUri+'/images/light-square.jpg';
	var selImgSrc = templateUri+'/images/dark-square.jpg';
	
	$j.fn.cycle.updateActivePagerLink = function(pager, currSlideIndex){
		$j(pager).find('a img').attr('src', imgSrc);
		$j(pager).find('a').removeClass('active').eq(currSlideIndex).addClass('active');
		$j(pager).find('a.active img').attr('src', selImgSrc);
	};
	
	$('#content .gallery').each(
		function(i, g){
			var $image = $j(g).find('.image');
			var $pause = $j(g).find('.pause');
			var $play =  $j(g).find('.play');
			
			var pause = function(){ $pause.hide(); $play.show(); $image.cycle('pause'); };
			var play = function(){ $pause.show(); $play.hide(); $image.cycle('resume'); };
			
			$pause.click(pause);
			$play.click(play);
			
			$play.hide();
		
			$image.cycle({
				fx: 'fade',
				timeout: '8000',
				next: $j(g).find('.next'),
				prev: $j(g).find('.prev'),
				pager: $j(g).find('.pages'),
				pagerAnchorBuilder: function(i, slide){
					return '<a href="javascript: void(0);"><img src="'+(i == 0 ? selImgSrc : imgSrc)+'" alt="Jump to image '+(i + 1)+'"/></a>';
				},
				prevNextClick: pause,
				pagerClick: pause
			});
		});
}

function initHeaderSlideshow($){
	$ss = $('#header-image');
	
	$ss.cycle({
		fx: 'fade',
		timeout: '10000',
		height: $ss.css('height'),
		fit: 0,
		containerResize: 0
	});
}

$j(document).ready(initGlobals);
