// COMMENTS VALIDATION (THIS MUST COME FIRST)
// ----------------------------------------------------------------- //
$(document).ready(function() {
	$('form#commentform').submit(function() {
		$('form#commentform .error').remove();
		var hasError = false;
		$('.requiredField').each(function() {
			if(jQuery.trim($(this).val()) == '') {
				var labelText = $(this).prev('label').text().replace('','');
				$(this).parent().append('<span class="error">Please fill in the required field.</span>');
				hasError = true;
			}
		});
		if(hasError) {
			return false;
		}
	});
});
// HOMEPAGE TOGGLE
// ----------------------------------------------------------------- //
$(document).ready(function(){
	$(".trigger").click(function(){
			triggerHeaderAnimation();	
	})
});
function checkStatus() {
	if($('.toggle-container').css('display') == 'none') {disableVideo();} 
	if($('.toggle-container').css('display') == 'block') {createVideo();} 
}
function createVideo() {
	document.getElementById('couch-videos').innerHTML ='<div id=\'flashContent\'><p>To view this page ensure that Adobe Flash Player version 10.0.0 or greater is installed.</p></div><script type=\'text/javascript\'> var pageHost = ((document.location.protocol == \'https:\') ? \'https://\' : \'http://\'); <a href=\'http://www.adobe.com/go/getflashplayer\'><img src=\'\' + pageHost + \'www.adobe.com/images/shared/download_buttons/get_flash_player.gif\' alt=\'Get Adobe Flash player\' /></a></script>';
	swfobject.embedSWF(
		"http://www.adcetera.com/wp-content/themes/Adcetera2011/flash/AdceteraFlashVideoPlayer.swf", "flashContent", 
		"919", "360", 
		swfVersionStr, xiSwfUrlStr, 
		flashvars, params, attributes);
	}
function disableVideo() {
	document.getElementById('couch-videos').innerHTML ='';
	swfobject.embedSWF(
		"http://www.adcetera.com/wp-content/themes/Adcetera2011/flash/AdceteraFlashVideoPlayer.swf", "flashContent", 
		"919", "360", 
		swfVersionStr, xiSwfUrlStr, 
		flashvars, params, attributes);
};
function triggerHeaderAnimation(){
	$("#toggle-hide").fadeTo(0, 1);
	$(".toggle-container").slideToggle(function(){checkStatus();});
	$("#toggle-hide").delay(300).fadeTo(1000, 0.15);
	$("#toggle-hide").hover(
		function(){ //mouseover
			$(this).fadeTo(400, 1)},
		function(){ //mouseleave
			$(this).fadeTo(400, 0.15).clearQueue()}
		);
	return false;
}
$(document).ready(function(){
	$(".trigger-sec").click(function(){
		$(".toggle-container").slideToggle(function(){checkStatus();});
	})
});
// CONTACT FROM NAVIGATION MENU TOGGLE
// ----------------------------------------------------------------- //
$(document).ready(function(){
	$(".page-item-15").click(function(){
		$(".toggle-container-contact").slideToggle();
		$("html, body").animate({ scrollTop: 60000 }, "normal");
		return false;
	})
});	
// CONTACT FROM FOOTER TOGGLE
// ----------------------------------------------------------------- //
$(document).ready(function(){
	$(".trigger-contact").click(function(){
		$(".toggle-container-contact").slideToggle();
		$("html, body").animate({ scrollTop: 60000 }, "normal");
		return false;
	})
});
// TOOLTIP OPTIONS
// ----------------------------------------------------------------- //
$(document).ready(function(){
  $(".tt").tooltip({
	  predelay: 500,
	  delay: 0,
	  effect: 'slide',
	  position: 'top center',
	  offset: [20, 95]
  });
});
// INTERIOR PAGE SIDEMENU TOGGLE
// ----------------------------------------------------------------- //
$(document).ready(function(){
  $("#sidemenu-trigger").click(function(e){
	e.preventDefault();
	var slidepx=$("#sidemenu").width();
	if ( !$("#sidemenu-wrap").is(':animated')){ 
		if (parseInt($("#sidemenu-wrap").css('marginLeft'), 10) < slidepx){
			$(this).removeClass('close');
			margin = "+=" + slidepx;
		} else {
			$(this).addClass('close');
			margin = "-=" + slidepx;
		}
		$("#sidemenu-wrap").animate({marginLeft: margin}, {duration: 'slow', easing: 'easeOutQuint'});
	} 
  }); 
});
// EMAIL PAGE TO SOMEONE
// ----------------------------------------------------------------- //
function mailpage(){
	mail_str = "mailto:?subject=Link from Adcetera.com";
	mail_str += "&body=I thought you would enjoy looking at this page.";
	mail_str += " " + parent.location.href;
	location.href = mail_str;
}
// AUTOCLEAR FORMS
// ----------------------------------------------------------------- //
function init(){
	var inp = document.getElementsByTagName('input');
	for(var i = 0; i < inp.length; i++) {
		if(inp[i].type == 'text') {
			inp[i].setAttribute('rel',inp[i].defaultValue)
			inp[i].onfocus = function() {
				if(this.value == this.getAttribute('rel')) {
					this.value = '';
				} else {
					return false;
				}
			}
			inp[i].onblur = function() {
				if(this.value == '') {
					this.value = this.getAttribute('rel');
				} else {
					return false;
				}
			}
			inp[i].ondblclick = function() {
				this.value = this.getAttribute('rel')
			}
		}
	}
}
if(document.childNodes) {
	window.onload = init
}
// LEADERSHIP TABS
// ----------------------------------------------------------------- //
$(document).ready(function() {
	//Default Action
	$(".tab_content").hide(); //Hide all content
	$("ul.tabs li:first").addClass("active").show(); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content
	//On Click Event
	$("ul.tabs li").click(function() {
		$("ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide().clearQueue(); //Hide all tab content
		var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
		$(activeTab).delay("500").slideToggle("fast"); //Fade in the active content
		return false;
	});
});
// ACCORDION (CAREERS)
// ----------------------------------------------------------------- //
$(function() {
    $('#outer h4.expand').wrapInner('<a style="display:block" href="#" title="expand/collapse"></a>');
    $('#outer div.demo')
    .find('div.collapse').hide().end()
    .find('h4.expand').each(function() {
          $(this).click(function() {
              var $thisCllps = $(this).next('div.collapse');
              var $cllpsVisible = $(this).siblings('h4.expand').next('div.collapse:visible');
              ($cllpsVisible.length) ? $(this).toggleClass('open').siblings('h4.expand').removeClass('open')
                  .next('div.collapse:visible').slideUp('normal', function(){
                  $thisCllps.slideDown();
                  }) : $(this).toggleClass('open').next('div.collapse').slideToggle();
              return false;
          });
	});
	return false;
});
// UP TOP BOUNCE
// ----------------------------------------------------------------- //
function backToTop() {
    var x1 = x2 = x3 = 0;
    var y1 = y2 = y3 = 0;
    if (document.documentElement) {
        x1 = document.documentElement.scrollLeft || 0;
        y1 = document.documentElement.scrollTop || 0;
    }
    if (document.body) {
        x2 = document.body.scrollLeft || 0;
        y2 = document.body.scrollTop || 0;
    }
    x3 = window.scrollX || 0;
    y3 = window.scrollY || 0;
    var x = Math.max(x1, Math.max(x2, x3));
    var y = Math.max(y1, Math.max(y2, y3));
    window.scrollTo(Math.floor(x / 2), Math.floor(y / 2));
    if (x > 0 || y > 0) {
        window.setTimeout("backToTop()", 25);
    }
}
