/* jQuery.Listen - Light and fast event handling, using event delegation.
 * http://flesler.blogspot.com/2007/10/jquerylisten.html */
;(function($){$.fn.indexer=function(a){return this[0]&&k(this[0],a)||null};$.indexer=function(a){return k(document,a)};var g=$.event,h=g.special,j=$.listen=function(c,d,e,f){if(typeof d!='object'){f=e;e=d;d=document}n(c.split(/\s+/),function(a){a=j.fixes[a]||a;var b=k(d,a)||k(d,a,new m(a,d));b.append(e,f);b.start()})},k=function(a,b,c){return $.data(a,b+'.indexer',c)};j.regex=/^((?:\w*?|\*))(?:([#.])([\w-]+))?$/;j.fixes={focus:'focusin',blur:'focusout'};$.each(j.fixes,function(a,b){h[b]={setup:function(){if($.browser.msie)return!1;this.addEventListener(a,h[b].handler,!0)},teardown:function(){if($.browser.msie)return!1;this.removeEventListener(a,h[b].handler,!0)},handler:function(e){arguments[0]=e=g.fix(e);e.type=b;return g.handle.apply(this,arguments)}}});$.fn.listen=function(a,b,c){return this.each(function(){j(a,this,b,c)})};function m(a,b){$.extend(this,{ids:{},tags:{},listener:b,event:a});m.instances.push(this)};m.instances=[];m.prototype={constructor:m,handle:function(e){var a=e.stopPropagation;e.stopPropagation=function(){e.stopped=1;a.apply(this,arguments)};k(this,e.type).parse(e)},on:0,bubbles:0,start:function(){if(!this.on){g.add(this.listener,this.event,this.handle);this.on=1}},stop:function(){if(this.on){g.remove(this.listener,this.event,this.handle);this.on=0}},parse:function(e){var c=e.data||e.target,d=[],f=arguments;if(c.id&&this.ids[c.id])o(d,this.ids[c.id]);n([c.nodeName,'*'],function(a){var b=this.tags[a];if(b)n((c.className+' *').split(' '),function(a){if(a&&b[a])o(d,b[a])})},this);if(d[0])n(d,function(a){if(a.apply(c,f)===!1){e.preventDefault();e.stopPropagation()}});if(!e.stopped&&(c=c.parentNode)&&(c.nodeName=='A'||this.bubbles&&c!=this.listener)){e.data=c;this.parse(e)}},append:function(a,b){var c=j.regex.exec(a);if(!c)throw'$.listen > "'+a+'" is not a supported selector.';var d=c[2]=='#'&&c[3],e=c[1].toUpperCase()||'*',f=c[3]||'*';if(d)(this.ids[d]||(this.ids[d]=[])).push(b);else if(e){e=this.tags[e]=this.tags[e]||{};(e[f]||(e[f]=[])).push(b)}}};function n(a,b,c){for(var i=0,l=a.length;i<l;i++)b.call(c,a[i],i)};function o(a,b){a.push.apply(a,b);return a};$(window).unload(function(){if(typeof m=='function')n(m.instances,function(a){a.stop();$.removeData(a.listener,a.event+'.indexer');a.names=a.listener=0})})})(jQuery);

/* ON LOAD (jQuery) -- also see /editor/components/jquery/jquery-global.js */

/* use .log in jquery to write to console */
jQuery.fn.log = function (msg) {console.log("%s: %o", msg, this); return this;};

$(document).ready(function(){	

	// Hide stuff that Javascript will unhide later  
	$('.hidden').hide();	
	$('.dropdown').hide(); 
	
	//Dropdown menus and popup boxes 
	$('.dropdown_link').click(function(){
		dropdown_id = $(this).attr("rel");
		$('#'+dropdown_id).toggle();
		return false;
	});

	// Close dropdown menu or popup box 
	$('.close').click(function(){
		$(this).parents(".dropdown").hide();
	});
	

	// Toggle all checkboxes (requires jquery.checkboxes.min.js)
	// this could use further development -- such as toggle all checkboxes in a certain column
	$('#toggleCheckboxes').click(function(){
		$(this).parents("form").toggleCheckboxes();
	});
	
	// Add star to required fields
	$('form .required label:not(.radio)').prepend('* ');

	
	/*
	// Dashboard toggle node
	$('#my_dashboard li.expand').click( function(){
		$(this).children('ul').toggle();
		return false;
	});
	
	// Stop li.expand function when child links are clicked
	$('#my_dashboard li.expand * a').click( function(event){
		event.stopPropagation();
	});
	
	// Expand/Collapse all child nodes
	$('#my_dashboard:not(".groupshare") > li > a').click(function(){
		$(this).next().children('li.expand').click();
		return false;
	});
	
	*/
	
	/* SCROLL ALERTS */
	if ($('#my_alert ul li').length > 1){
		$('#my_alert ul').cycle({ 
			slideExpr: 'li',
			fx:     'fade', 
			speed:  'slow', 
			timeout: 10000
		});
	}
	
	
	/* ADJUST MENU TO FULL WIDTH */
	//assign class to each item in the nav
	var item_num = 0;
	var item_total = $('#primary_nav > li').size();
	//set button width
		var menuWidth = $('#primary_nav ').width();
		var buttonWidth = menuWidth / item_total;
		$('#primary_nav > li a').width(buttonWidth-21);//to accomodate for padding
	//
	$('#primary_nav > li').each(function(){
		item_num++;
		$(this).addClass('item_'+item_num);
		//add first item class
		if (item_num == 1){$(this).addClass('item_first');}
		//add last item class
		if (item_num == item_total){$(this).addClass('item_last');}
	});

/*	
	//disable links with submenus
	var disableLink = $('#primary_nav ul').parent().children('a');
	$(disableLink).removeAttr('href');
*/	
	//add hover class for IE6
	$('#primary_nav > li').hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	});


	
	/*CYCLE CONTROLS*/
	if($('#gallery_img').length != 0){	
		$('#gallery_img').cycle({ 
			slideExpr: 'li',
			fx:     'fade', 
			speed:  'slow', 
			timeout: 0, 
			next:   '#mhome_next', 
			prev:   '#mhome_previous',
			fit: 1
		});
	}
});


$(document).ready(function(){
	// Tabs!
	// <a href="#" rel="div_id">Tab</a>
	// class ".current" should contain display:block. other divs should have display:none
	$('.tabs a[rel]').click( function(){
		$('a.on').removeClass('on');
		$(this).addClass('on');
		el=$(this).attr('rel');
		$('div.current').removeClass('current').hide();
		$('#'+el).addClass('current');
	});
});


function tabs (){
	$('.tabs a').click(function(){
		$(this).siblings('a').removeClass('active');					 
		$(this).addClass('active');
		//get id from button and concat
		var activeLI = '#'+$(this).attr('id')+'_li';
		//hide other LI then show active
		$('.tabsections > li').hide();
		$(activeLI).show();
	});
}


/* FROM MOCKUP.JS */
//input focus blur
$(document).ready(function(){
	$('input').focus(function(){
		$(this).addClass('active');					  
	});
	$('input').blur(function(){
		$(this).removeClass('active');					  
	});
});

//OVERLAY
//open overlay
$('.popup').click(function(el){
	//prevent default link action
	el.preventDefault();
	//create layout elements
	$('body').append("<div id='shadow'>&nbsp;</div><div id='overlay'><button id='close_btn'>&nbsp;</button><div id='overlay_load'></div></div>");
	var shadow = $('#shadow');
	var overlay = $('#overlay');
	var page = $(this).attr('href');
	//load html from outside source
	//$('#overlay_load').load('/ama/mod.site/mod.classflow/login_register.htm');
	$.ajax({
		url: page,
		cache: false,
		success: function(html){
			$("#overlay_load").append(html);
			//make shadow full document height
			docHeight = $(document).height();
			$(shadow).css('height', docHeight);
			//center overlay vertically
			var posTop = ($(window).height()/2) - ($(overlay).height()/2);
			$(overlay).css('top', posTop);
			//center overlay horizontally
			var posLeft = ($(window).width()/2) - ($(overlay).width()/2);
			$(overlay).css('left', posLeft);
			//show overlay elements
			$('#shadow').show();
			$('#overlay').show();
		}
	});
});
//close overlay
$("#shadow").live("click", function(e){close_overlay();});
$("#close_btn").live("click", function(e){close_overlay();});
$(document).keydown(function(e){if(e.which == 27) {close_overlay();}}); 
//
function close_overlay (){
	$('#shadow').hide('fast').remove();
	$('#overlay').hide('fast').remove();
}
//
$('.refresh_popup').live("click", function(el){
	//prevent default link action
	el.preventDefault();
	//
	var shadow = $('#shadow');
	var overlay = $('#overlay');
	var page = $(this).attr('href');
	//
	$.ajax({
		url: page,
		cache: false,
		success: function(html){
			$("#overlay_load").html(html);
			//make shadow full document height
			docHeight = $(document).height();
			$(shadow).css('height', docHeight);
			//center overlay vertically
			var posTop = ($(window).height()/2) - ($(overlay).height()/2);
			$(overlay).css('top', posTop);
			//center overlay horizontally
			var posLeft = ($(window).width()/2) - ($(overlay).width()/2);
			$(overlay).css('left', posLeft);
			//show overlay elements
			$('#shadow').show();
			$('#overlay').show();
		}
	});
});


$('#customer_search_submit').live("click", function(){
	var overlay = $('#overlay');
	//
	var updatedTop = parseInt($(overlay).css('top')) - 120;
	//alert(updatedTop);
	//
	$(overlay).animate({top: updatedTop}, 200);
	$('#find_list').show('fast');
	$("#customer_search_submit").die("click");
	return false;
});
