﻿var slider = {
	openClass: 'slide-open',
	closeClass: 'slide-close',
	getHash: function(str) {
		var hashIndex = str.lastIndexOf('#'),
			tgt = str.substring(hashIndex);
		return tgt;
	},
	init: function() {
		var self = this,
			$body = $('body'),
			$opens = $body.find('a.' + self.openClass),
			$closes = $body.find('a.' + self.closeClass);
		$opens.click(function(e) {
			var tgt = self.getHash(this.href);
			self.open(tgt);
			e.preventDefault();
		});
		$closes.click(function(e) {
			var tgt = self.getHash(this.href);
			self.close(tgt);
			e.preventDefault();
		});
	},
	close: function(tgt) {
		$(tgt).slideUp();
	},
	open: function(tgt) {
		$(tgt).slideDown();
	}
};
var placeholder = {
	init: function() {
		var self = this;
		this._supportTest();
		if ( !$.support.placeholder ) {
			self.fakinIt();
		}
	},
	_supportTest: function() {
		$.support.placeholder = false;
		test = document.createElement('input');
		if ('placeholder' in test) {
			$.support.placeholder = true;
		}
	},
	fakinIt: function() {
		var self = this,
			active = document.activeElement;
		$(':text')
			.focus( function() {
				var $this = $(this);
				if ( $this.attr('placeholder') !== '' && $this.val() === $this.attr('placeholder') ) {
					$this.val('').removeClass('hasPlaceholder');
				}
			})
			.blur( function() {
				var $this = $(this);
				if ( $this.attr('placeholder') !== '' && ( $this.val() === '' || $this.val() === $this.attr('placeholder') ) ) {
					$this.val($this.attr('placeholder')).addClass('hasPlaceholder');
				}
			});
		$(':text').blur();
		$(active).focus();
		/* $('form').submit( function() {
			self.clearDefaults();
		});*/
	},
	clearDefaults: function() {
		$('body').find('.hasPlaceholder').each(function() {
			$(this).val('').removeClass('hasPlaceholder');
		});
	}
};
var showField = {
	selectClass: 'cat-select',
	defaultText: 'Select One',
	textClass: 'cat-nr',
	init: function() {
		var self = this,
			el, val, $el;
		
		$('body').find('.' + self.selectClass).change(function() {
			el = this, val = el.value, $el = $(this);
			if ( val === '' || val === null || val === self.defaultText ) {
				$el.next('.' + self.textClass).hide();
			} else {
				$el.next('.' + self.textClass).show();
			}
		});
	}
};
function newWindow() {
	$('.newWindow').click(function(event) {
		var $this = $(this),
			url = $this.attr('href'),
			windowName = $this.attr('name');

		window.open(url, windowName, "width=600,height=800,scrollbars=yes,location=no");
		
		event.preventDefault();
		return false;
	});
};

$(document).ready(function() {
	slider.init();
	placeholder.init();
	newWindow();
	showField.init();
});
