/**
 * @name       jQuery.drawer
 * @version    1.00, 2008/05/01
 * @author     inamorix <inamorix@metatype.jp>
 * @copyright  Copyright (c) 2008, metatype.jp.
 * @license    The MIT-style license.
 */



(function ($, loaded) {
	$.extend({ drawer: {
	init: function () {
		this.self   = null;
		//this.closer = '<div id="drw_close"><a href="#" class="drw_close" title="close">close</a></div>';
		this.loader = '<div id="drw_loader">loading...</div>';
		this.setTabs();
		$('#drw').html('<div id="drw_content"></div>');
		
		
		//set up logo click
		/*
		$('#logo').click(function () {
			var d = $.drawer.self.id;
			$.drawer.close2(d, ($.drawer.self != this) ? this : null);
			//$.drawer.close($.drawer.self);
			this.blur();				
			return false;
		});	
		*/
		
		//show =/hide comments
		$('div.c-showhide').hide();
	    $('a.c-toggle').click(function() {
	        $('div.c-showhide').slideToggle(400);
	        //$(this).text($(this).text() == 'Open' ? 'Close' : 'Open');
	        return false;
	    });
	    $('div.r-showhide').hide();
	    $('a.r-toggle').click(function() {
	        $('div.r-showhide').slideToggle(400);
	        //$(this).text($(this).text() == 'Open' ? 'Close' : 'Open');
	        return false;
	    });
	    
	    function checkcomment(form) {
			var formAuthor = form.author.value;
			var formEmail = form.email.value;
			var formComment = form.comment.value;
			if(formAuthor == "") {
				alert("Please Enter Your Name");
				form.author.focus();
				return false;
			} else if((formEmail.length<6) || (formEmail.indexOf(",")>=0) || (formEmail.indexOf(";")>=0) || (formEmail.indexOf(":")>=0) || (formEmail.indexOf("/")>=0) || (formEmail.indexOf(" ")>=0) || (formEmail.indexOf("@")<=0) || (formEmail.indexOf("@") != formEmail.lastIndexOf("@")) || (formEmail.lastIndexOf(".")<formEmail.indexOf("@")) || ((formEmail.lastIndexOf(".")+3)>formEmail.length)) {
				alert("Please Enter A Valid Email Address");
				form.email.focus();
				return false;
			} else if(formComment == "") {
				alert("Please Enter A Comment");
				form.comment.focus();
				return false;
			} else {
				form.submit();
				return true;
			}
		}


			
	},
	
	
	
	setTabs: function () {
		/*
var tabs  = $('#drw_tabs li');
		var tabs2  = $('#drw_tabs2 li');
		var w     = Math.floor(100 / tabs.size());
		var w2    = Math.floor(100 / tabs2.size());
		var w_rem = 100 % tabs.size();
		var w_rem2 = 100 % tabs2.size();
*/ 
		/*tabs.each(function () {
			 $(this).css({ width: (w + ((w_rem-- > 0) ? 1 : 0)) + '%' }); 
		});*/
		
		
		/* First Row */
		$('#drw_tabs li:first').addClass('first');		
		$('#drw_tabs li:last').addClass('last');		
		
		$('#drw_tabs a[rel*=drw]').each(function () {
			$('#' + this.href.split('#')[1]).hide();
			$(this).click(function () {
				$.drawer.close(($.drawer.self != this) ? this : null);
				this.blur();
				return false;
			});
		});
		
		/* Second Row */
		$('#drw_tabs2 li:first').addClass('first');
		$('#drw_tabs2 li:last').addClass('last');
		$('#drw_tabs2 a[rel*=drw]').each(function () {
			$('#' + this.href.split('#')[1]).hide();
			$(this).click(function () {
				var d = $.drawer.self.id;
				$.drawer.close2(d, ($.drawer.self != this) ? this : null);
				//$.drawer.close($.drawer.self);
				this.blur();				
				return false;
			});
		});
		
	},
	
	
	open: function (pDrawer) {
	
		var anchor = $(this);
		var content = $('#drw_content').empty();
		
		//alert(this.self.id + ", " + pDrawer)
		
		if(pDrawer == "drw_tabs_focus"){
			//this is to link directly to the href
			//$.get(anchor.attr('href');
			//return;
			
			var url = $.drawer.self.href;
			window.location.href = url;
			
		} else {
		
			
					
			$('#drw_tabs_focus').removeAttr('id');
			if (!this.self) return;
			
			this.self.id = 'drw_tabs_focus';
			
			
			loaded       = /(#[\w\-]+)$/.test(this.self.href);
			//loaded = true / false
			content.append(loaded ? $(RegExp.$1).clone().show() : this.loader);
			
			
			//content.append(this.closer);
			/*
			content.find('.drw_close').click(function () {
				$.drawer.close(null);
				this.blur();
				return false;
			});
			*/
		}
		
		
		content.css({ marginTop: content.height() * -1 }).animate({ marginTop: 0 }, function () {
			if (!loaded) {
				$.ajax({
					url     : $.drawer.self.href,
					type    : 'GET',
					dataType: 'html',
					cache   : true,
					success : function (html) {
						if (!loaded) {
							content.prepend('<div id="drw_ajax"></div>');
							$('#drw_ajax').hide().append(html);
							//alert($('#drw_ajax').height())
							$('#drw_loader').animate({ height: $('#drw_ajax').height() }, function () {
								$('#drw_loader').fadeOut(function () { $('#drw_ajax').fadeIn(); });
							});
						}
					}
				});
			}
		});
	},
	
	
	
	close: function (el) {
		//alert(el)
		loaded    = true;
		this.self = el;
		var content = $('#drw_content');
		content.animate({ marginTop: content.height() * -1 }, function () { $.drawer.open(this.self); });
	},
	
	close2: function (pDrawer, el) {
		//alert(el)
		loaded    = true;
		this.self = el;
		var content = $('#drw_content');
		content.animate({ marginTop: content.height() * -1 }, function () { $.drawer.open(pDrawer); });
	}
	
	
	
}});
$(function () { $.drawer.init(); });
})(jQuery);
