/**
 * @author dt.john
 */
function toggleCats(){
	$('a.catTab').click(function(){
		if($(this).hasClass('active')){
			return false;
		}else{
			hrefVal = $(this).attr('href').substr(1);
			$('a.catTab').removeClass('active');
			$(this).addClass('active');
			$('.catArcList').hide();
			$('#'+hrefVal).show();
			$.cookie("catActive",hrefVal,{expires:7});
			return false;
		}
	});
	if($.cookie("catActive")!=null){
		currentTab = $.cookie("catActive");
		//remove Active Class and hide lists
		$('a.catTab').removeClass('active');
		$('.catArcList').hide();
		//Add active class and show list
		$('#tab_'+currentTab).addClass('active');
		$('#'+currentTab).show();
	}
}
$(document).ready(function(){
	DOMUtilities.init();
 	CufonFonts.init();
	FancyBox.init();
	if($('#sidebar_categories').length) toggleCats();
 });
/**
 * Cufon font initializer
 * @author					Dave Shepard
 * @version					0.2
 * @required libraries:		JQuery 1.3.2, Cufon 
 * 
 * Usage:
 *     $(document).ready(function(){
 *         CufonFonts.init();
 *     });
 * 
 * An initialization script for Simo Kinnunen's Cufon font replacer available 
 * at: http://wiki.github.com/sorccu/cufon. This is an independent
 * script from Cufon and is unsupported by Sorccu.
 * 
 * Fill the CufonInitializer.fonts array with objects containing the font CSS
 * name and the elements to apply the font to in the CufonInitializer.fonts.elements
 * array.
 */
var CufonFonts = {
	fonts: [
		{
			name: 'chalet-london',
			elements: [
				'a.mainNav'
			]
		}
	],
	init: function(){
		for(i = 0; i < this.fonts.length; i++) {
			var f = this.fonts[i];
			var existingElements = [];
			for (ie = 0; ie < f.elements.length; ie++) {
				var cfe = f.elements[ie];
				if ($(cfe).length > 0) {
					existingElements.push(cfe);
				}
			}
			var cufonOptions = {
				fontFamily : f.name
			}
			if(f.options != undefined){
				var optionKeys = $.keys(f.options);
				for(w=0 ; w < optionKeys.length ; w++){
					cufonOptions[optionKeys[w]] = f.options[optionKeys[w]];
				}
			}
			if(existingElements.length > 0){
				Cufon.replace(existingElements.join(","),cufonOptions);
			}
		}
	}
}
$.extend({
	keys: function(obj){
		var a = [];
		$.each(obj, function(k){ a.push(k) });
		return a;
	}
});
/**
 * DOM Utilies Object for handling basic DOM actions
 * @author					Dave Shepard
 * @version					1.0
 * @required libraries:		JQuery 1.3.2 or later
 * 
 * Usage:
 *     $(document).ready(function(){
 *     		DOMUtilities.init();
 *     });
 *     
 * Can be initialized via the init(); method to apply to entire <body> or
 * a scope can be passed to limit the initialization to the child elements
 * of a particular element. Individual methods can als be called and passed
 * a scope.
 */
var DOMUtilities = {
	targetBlank: function(locality){
		// XHTML 1.0 Strict work around for external links
		$(locality+' a[rel*="external"]').attr("target","_blank");
	},
	inputAutoClear: function(locality){
		$(locality+' input.clearField').focus(function(){
			if(this.defaultValue == this.value) this.value='';
		}).blur(function(){
			if(this.value == '') this.value = this.defaultValue;
		});
	},
	imgRollover: function(locality){
		// Image roll-over setup
		$(locality+' img.rollOver, '+locality+' input[type="image"].rollOver')
			.mouseover(function(){
				if (this.src.indexOf("_i.") != -1) {
					this.src = this.src.replace("_i.", "_o.");
				}
			}).mouseout(function(){
				if (this.src.indexOf("_o.") != -1) {
					this.src = this.src.replace("_o.", "_i.");
				}
				if(this.src.indexOf("_a.")) {
					this.src = this.src.replace("_a.","_i.");
				}
			}).filter("input").mousedown(function(){
				this.src = this.src.replace("_o.","_a.");
			}).mouseup(function(){
				this.src = this.src.replace("_a.","_i.");
			});
	},
	init: function(locality){
		if(locality == null) {
			locality = "body";
		}
		this.targetBlank(locality);
		this.inputAutoClear(locality);
		this.imgRollover(locality);
	}
}
var FancyBox = {
	boxen: function(){
		$("a.fancybox").fancybox({
			'hideOnContentClick':true,
			'zoomSpeedIn': 300,
			'zoomSpeedOut': 300
		});
		$('a#subscribe-link').fancybox({
			hideOnContentClick: false,
			zoomSpeedIn: 300,
			zoomSpeedOut: 300,
			centerOnScroll: true,
			frameWidth: 420,
			frameHeight: 135,
			autoDimensions: true,
			callbackOnShow: function(){
				InitializeSubscribeForm();
			},
			overlayOpacity: 0.75
		});
	},	
	init: function(){
		this.boxen();	
	}
}

function InitializeSubscribeForm(yr) {
	
	// Set text field behavior
	var txtfields = $('#fancy_content #subscribe-form .subscribe-form-text input');
	txtfields.each(function() {
		if ($(this).val()) {
			$(this).siblings('label').hide();
		}
	});
	txtfields.load(function() {
		if ($(this).val()) {
			$(this).siblings('label').hide();
		}
	});
	txtfields.focus(function() {
		if (!$(this).val()) {
			$(this).siblings('label').hide();
		}
	});
	txtfields.keydown(function(ev) {
		$(this).siblings('label').hide();
	});
	txtfields.keyup(function() {
		if (!$(this).val()) {
			$(this).siblings('label').show();
		}
	});
	txtfields.blur(function() {
		if (!$(this).val()) {
			$(this).siblings('label').fadeTo(0, 1);
			$(this).siblings('label').show();
		}
	});
	
	
	// Bind close action to close link
	$('#fancy_content #subscribe-modal a.close-link').click(function(event) {
		event.preventDefault();
		$.fn.fancybox.close();
	});
	
	// Bind submit button handling
	$('#fancy_content #subscribe-form').submit(function(event) {
		event.preventDefault();
		
		var formFields = $(this).serializeArray();
		var nlOpt = $('#subscribe-form #subscribe-form-newsletter').is(':checked');
		var blOpt = $('#subscribe-form #subscribe-form-blog').is(':checked');
		var errCnt = 0;
		if (!nlOpt && !blOpt) errCnt++;
		if (!$.trim($('#subscribe-form #subscribe-form-email').val()).match(/^([^@.]+[.]?)+@([^@.]+[.])+[^@.]{2,6}$/)) errCnt++;
		
		$.fn.fancybox.showLoading();
		
		$.post('/subscribe-form', formFields, function(data) {
			// adjust dims depending on subscr options
			var xDelta = 0; var yDelta = 0;
			yDelta += (!errCnt && nlOpt && blOpt) ? 225 : (!errCnt && nlOpt) ? 170 : 0;
			yDelta += errCnt * 24;
			yDelta += yr ? yr : 0;
			xDelta += (!errCnt && nlOpt) ? 125 : 0;
			$('#fancy_outer').animate({
				'height': '+=' + yDelta,
				'top': '-=' + Math.floor(yDelta/2),
				'width': '+=' + xDelta,
				'left': '-=' + Math.floor(xDelta/2)
			});
			
			var yRedux = -1 * errCnt * 24;

			$('#fancy_ajax').html(data);
			InitializeSubscribeForm(yRedux);
		});
		
	});
	
	
	// Do this last, because it takes a mo'
	// Do Cufon font replacement
	//Cufon.replace('#fancy_content #subscribe-form h2',{fontFamily: 'chalet-paris'});
}

