// Treeview
$(document).ready(function(){
	if ($('ul#treeview').length)
	{
		$('ul#treeview a').each(function () { 
			var children=$('ul:first',$($(this).parent()));

			if ($(children).length)
			{
				if ($(this).hasClass('closed'))
				{
					$(children[0]).addClass('hidden');
				}
				else
				{
					$(this).addClass('open');
				}

				$(this).click(function (event) {
					if ($(this).hasClass('closed'))
					{
						$(this).removeClass('closed');
						$(this).addClass('open');
						$(children[0]).removeClass('hidden');
					}
					else
					{
						$(this).removeClass('open');
						$(this).addClass('closed');
						$(children[0]).addClass('hidden');
					}
					event.preventDefault();
					return false;	
				});
			}
		});
		$('ul#treeview input').each(function () {
			$(this).click(function () {
				if ($(this).attr('checked')==true)
				{
					$(this).parent().parents('li').each(function () {
						if ($(this).children('input').length)
						{
							$(this).children('input').attr('checked',false);
						}
					});
					$(':checkbox',$(this).parent().children('ul')).each(function () {
						$(this).attr('checked',false);
					});
				}
			});
		});
	}
});


// Tabs
$(document).ready(function(){
	if ($('div#tabs').length)
	{
		var ul=$('ul:first',$('div#tabs'));
		var lis=$('li',ul);
		var divs=$('div.tab');
		lis.each(function (i) {
			$(this).click(function (event) {
				if ($(divs[i]).hasClass('hidden'))
				{
				  $('.next_sib',$(this).parent()).removeClass('next_sib');
				  var next_sib = $('+ li',$(this));
				  next_sib.addClass('next_sib');
					$(divs).each(function () {
						$(this).addClass('hidden');	
					});
					$(lis).each(function () {
						$('a:first',$(this)).removeClass('active');	
					});
					$('a:first',$(this)).addClass('active');
					$(divs[i]).removeClass('hidden');
				}
				event.preventDefault();
				return false;	
			});
			
		});
	}
});

// Overlay
$(document).ready(function(){
	if ($('#oframe').length && $('#white-shadow').length)
	{
		$('#white-shadow').css('opacity','0.5');
		$('#oframe').css('opacity','0');
	}
});	

// Inputs
$(document).ready(function(){
	if ($('input').length)
	{
		var els=$('input,textarea');
		els.each(function () {
			if($(this).attr('type')=='text' || $(this).attr('type')=='password' || $(this).hasClass('text'))
			{
				$(this).focus(function () {
					$(this).addClass('active');
				});
				$(this).blur(function () {
					$(this).removeClass('active');
				});
			}
		});
	}
	
	if ($('#phone1').length)
	{
		var phone1=document.getElementById('phone1');
		var phone2=document.getElementById('phone2');
		var phone3=document.getElementById('phone3');
		
		phone1.onkeyup = function() {
			if (phone1.value.length==3)
			{
				phone2.focus();
			}
		}

		phone2.onkeyup = function() {
			if (phone2.value.length==3)
			{
				phone3.focus();
			}
		}
	}
});

// Rollovers
$(document).ready(function(){
	if ($('.rollover').length)
	{
		var els=$('.rollover');
		els.each(function () {
			$(this).mouseover(function () {
				if (!($(this).hasClass('active')))
				{
					var source = $(this).attr('src');
					var newSource = source.replace(/_off/, "_on");
					$(this).attr({src:newSource});
				}
			});
			$(this).mouseout(function () {
				if (!($(this).hasClass('active')))
				{
					var source = $(this).attr('src');
					var newSource = source.replace(/_on/, "_off");
					$(this).attr({src:newSource});
				}
			});
		});
	}
});

// Global Navigation
$(document).ready(function(){
	if ($('#global-nav').length)
	{
		var gn = $('#global-nav');
		var lis = $('li',$(gn)); 
		lis.each(function () {
			var href = $('a',$(this));
			$(this).hover(
			  function () {
				$(this).addClass('hover');
				if ($('img',$(this)).length)
				{
					var img=$('img',$(this));
					var source = img.attr('src');
					var newSource = source.replace(/_off/, "_on");
					img.attr({src:newSource});
				}
				if ($(this).next('li').length && !($(this).children('h3').length))
				{
					$(this).next('li').addClass('next');
				}
			  }, 
			  function () {
				$(this).removeClass('hover');
				if ($('img',$(this)).length)
				{
					var img=$('img',$(this));
					var source = img.attr('src');
					var newSource = source.replace(/_on/, "_off");
					img.attr({src:newSource});
				}
				if ($(this).next('li').length && !($(this).children('h3').length))
				{
					$(this).next('li').removeClass('next');
				}
			  }
			);
			$(href).focus(function () {
				$(this).parents('li').addClass('hover');
			});
			$(href).blur(function () {
				$(this).parents('li').removeClass('hover');
			});
		});

		$(gn).mouseover(function (event) {
			toggleOverlay('show');
		});
		$(gn).mouseout(function (event) {
			if (isMouseLeaveOrEnter(event,this))
			{
				toggleOverlay();
			}
		});
	}
});

// Buttons
$(document).ready(function(){
	if ($('#buttons').length)
	{
		var buttons = $('.has',$('#buttons'));
		var dropdowns = $('.dropdown',$('#buttons'));
		var controls = $('.control',$('#buttons'));
		buttons.each(function (i) {
			$(this).mouseover(function (event) {
				toggleOverlay('show');
				$(dropdowns[i]).removeClass('hidden');

				var source = $(controls[i]).attr('src');
				var newSource = source.replace(/_off/, "_on");
				$(controls[i]).attr({src:newSource});
			});
			$(this).mouseout(function (event) {
				if (isMouseLeaveOrEnter(event,this))
				{
					toggleOverlay();
					$(dropdowns[i]).addClass('hidden');
					var source = $(controls[i]).attr('src');
					var newSource = source.replace(/_on/, "_off");
					$(controls[i]).attr({src:newSource});
				}
			});
		});
	}
});

// Reason Textarea
$(document).ready(function(){
	if ($('#max').length)
	{
		$('#max').click(function () {
			if (($('#max:checked,#faster:checked').length))
			{
				$('#reason_text').removeClass('hidden');
			}
			else if (!($('#max:checked,#faster:checked').length))
			{
				$('#reason_text').addClass('hidden');
			}
		});
	}
	if ($('#faster').length)
	{
		$('#faster').click(function () {
			if (($('#max:checked,#faster:checked').length))
			{
				$('#reason_text').removeClass('hidden');
			}
			else if (!($('#max:checked,#faster:checked').length))
			{
				$('#reason_text').addClass('hidden');
			}
		});
	}
});

function isMouseLeaveOrEnter(e, handler) { if (e.type != 'mouseout' && e.type != 'mouseover') return false; var reltg = e.relatedTarget ? e.relatedTarget : e.type == 'mouseout' ? e.toElement : e.fromElement; while (reltg && reltg != handler) reltg = reltg.parentNode; return (reltg != handler); }

function toggleOverlay(showorhide) {
	var ol = document.getElementById('white-shadow');
	var of = document.getElementById('oframe');

	if (showorhide=='show')
	{
		var w=$(window).width();
		var h=$(window).height();
		var t=0;
		if ($('#top-nav').length)
		{
			t=$('#top-nav').height();
		}
		if ($('#header').length)
		{
			t=t+$('#header').height();
		}
		ol.style.width=w+'px';
		ol.style.height=(h-t)+'px';
		ol.style.top=t+'px';
		ol.style.display='block';
		
		of.style.width=w+'px';
		of.style.height=(h-t)+'px';
		of.style.top=t+'px';
		of.style.display='block';
	}
	else
	{
		ol.style.display='none';
		of.style.display='none';
	}
}
	
	
//toggler
var toggler = function(trigger,affected,text) {
  this.trigger = trigger;
  this.affected = $(affected);
  this.closedText = this.trigger.text();
  if(this.trigger.attr('rel')) {
    this.openedText = this.trigger.attr('rel');
  } else {
    this.openedText = this.closedText;
  }
  this.bindClicks();
}

toggler.prototype = {
  bindClicks:function() {
    var me = this;
    this.trigger.click(function() {
      me.toggle();
      return false;
    });
  },
  toggle:function() {
    var d = this.affected.css('display') === 'block'?'none':'block';
    this.affected.css('display',d);
    var k_add = d==='block'?'hide':'show';
    var k_rem = k_add === 'show'?'hide':'show';
    var t_text = k_add === 'show'?this.openedText:this.closedText;
    this.trigger.removeClass(k_rem);
    this.trigger.addClass(k_add);
    this.trigger.text(t_text);
  }
}

$(document).ready(function() {
  $('.box-toggler').each(function() {
    new toggler($(this),$(this).attr('href'));
  });
});
