jQuery(document).ready(function($){

	// main nav active
	var url = window.location.href.split("/");
	url3 = url[3];
	url30 = (url3.split("?"))[0];
	$('#main-menu .content > ul > li > a').each(function(){
		if ($(this).attr('href') == '/'+url30) {
			$(this).addClass('active');
		}
	});

	// twitter/facebook tabs
	$('ul.snf-tabs li a').click(function(){
		$('ul.snf-tabs li a').removeClass('active');
		$(this).addClass('active');
		class_name = $(this).parent('li').attr('class');
		$('.soc-net-feed div.feed').hide();
		$('.soc-net-feed div.'+class_name).show();
		return false;
	});

	// sidebar navigation
	$('#sidebar-first ul.menu a.active').parents('li').addClass('active-leaf');

	// stay in touch
	$('#block-views-newsletter-sign-up-block .stay-in-touch-btn, .newsletter-sign-up-btn').fancybox({
		'modal' : true,
		'padding' : 0,
		'overlayColor' : '#000'
	});
	$('#block-views-newsletter-sign-up-block .ns-heading .close').click(function(){
		$.fancybox.close();
	});

	// scrollable
	// $("#block-views-client-stories-block .scrollable").scrollable({circular: true}).autoscroll({
	// 	interval: 6000
	// });;

	var scrc = $('#block-views-client-stories-block .scrollable-container');
	if (scrc.length > 0) {
		var delay = 6000;
		var timer;
		var animating = false;
		var container = scrc.find('.scrollable');
		var items = scrc.find('.items');
		var current = 0;
		var total = items.find('.views-row').length;
		var itemWidth = items.find('.views-row').width();
		var itemsWidth = total * itemWidth;
		var visibleItems = Math.round(container.width() / itemWidth);
		var br = total - visibleItems;
		var left = scrc.find('.prev');
		var right = scrc.find('.next');

		items.width(itemsWidth)
		var items1 = items.clone().addClass('c1').css({left: -itemsWidth}).appendTo(container);

		function scrollLeft() {
			if (!animating) {
				animating = true;
				clearTimeout(timer);
				timer = window.setTimeout(scrollRight, delay);
				if (current == 0) {
					items.css({left: -itemsWidth});
					items1.css({left: 0});
				} else if (current == br) {
					items.css({left: -br*itemWidth});
					items1.css({left: container.width()});
				};
				items.add(items1).animate({left: '+='+itemWidth}, function(){
					animating = false;
				});
				current = (current - 1 + total) % total;
			}
		}

		function scrollRight() {
			if (!animating) {
				animating = true;
				clearTimeout(timer);
				timer = window.setTimeout(scrollRight, delay);
				if (current == 0) {
					items.css({left: 0});
					items1.css({left: -itemsWidth});
				} else if (current == br) {
					items.css({left: container.width()});
					items1.css({left: -br*itemWidth});
				};
				items.add(items1).animate({left: '-='+itemWidth}, function() {
					animating = false;
				});
				current++;
				current %= total;
			}
		}

		left.click(scrollLeft);
		right.click(scrollRight);
		timer = window.setTimeout(scrollRight, delay);
	};

	$("#block-block-15 .scrollable").scrollable({circular: true}).navigator().autoscroll({
		interval: 2500
	});;

	// scrollable - set tab titles
	$('#block-block-15 .scrollable-item:not(".cloned")').each(function(){
		item_index = $('#block-block-15 .scrollable-item').index(this) - 1;
		item_title = $(this).find('.tab-title').text();
		$('#block-block-15 .navi a:eq('+item_index+')').text(item_title);
	});

	//expand and collapse inline content
	$("span.collapse").live("click", function () {
		if($.browser.msie && parseFloat($.browser.version) < 8) {
			$('#footer-wrapper').hide();
			$(this).next(".expand-text, .view-id-events_certification_workshops, .view-id-events_webinars, .view-id-events_conferences, .view-id-events").hide('fast', function(){
				$('#footer-wrapper').show();
			});
				
		} else {
			$(this).next(".expand-text, .view-id-events_certification_workshops, .view-id-events_webinars, .view-id-events_conferences, .view-id-events").slideUp('fast');
		}		
		$(this).removeClass('collapse').addClass('expand');
		$(this).html('Expand');
	});
	$("span.expand").live("click", function () {
		if($.browser.msie && parseFloat($.browser.version) < 8) {
			$('#footer-wrapper').hide();
			$(this).next(".expand-text, .view-id-events_certification_workshops, .view-id-events_webinars, .view-id-events_conferences, .view-id-events").show('fast', function(){
				$('#footer-wrapper').show();
			});
		} else {
			$(this).next(".expand-text, .view-id-events_certification_workshops, .view-id-events_webinars, .view-id-events_conferences, .view-id-events").slideDown('fast');
		}		
		$(this).removeClass('expand').addClass('collapse');
		$(this).html('Collapse');
		
	});

	// on page load
	if ($('.page-node-61').length > 0 || $('.page-node-91').length > 0) {		
		var anchor_url = window.location.href.split("#");
		$('#'+anchor_url[1]+' .expand').click();
	}
	// click menu item from the same page
	$('#block-system-main-menu .expanded ul a').click(function(){
		link_with_anchor = $(this).attr('href');
		anchor = link_with_anchor.split('#');
		$('#'+anchor[1]+' .expand').click();
	});

	// h1 with too much text fix
	var h1_length = $("h1#page-title").text().length;
	if ( h1_length > 60 ) {
		$("h1#page-title").height("74px");
	}

	// footer text shadow / utility nav text / sidebar h2
	$('#footer-wrapper h2 a, #block-block-11 h2, #block-menu-menu-utility-nav .menu li a, #sidebar-second h2, #featured-2 h2 span, #featured-2 h2 b, #featured-2 .text-wrp p, #featured-2 .text-wrp a, #featured-2 .text-wrp a.more').FontEffect({
		shadow:true,
		shadowColor :"#000",
		shadowOffsetTop :1,
		shadowOffsetLeft:1,
		shadowBlur :1,
		shadowOpacity :0.1
	});

	// button text shadow
	$('.orange-btn span.text-shadow, .orange-front-btn span.big-text-shadow, .orange-front-btn span.text-shadow, .white-btn span.text-shadow, .download-btn span.text-shadow, .ordinary-btn span.text-shadow').FontEffect({
		shadow:true,
		shadowColor :"#fff",
		shadowOffsetTop :1,
		shadowOffsetLeft:1,
		shadowBlur :1,
		shadowOpacity :0.1
	});

	// page title text shadow
	$('#main #page-title, #main #page-title-image').FontEffect({
		shadow:true,
		shadowColor :"#000",
		shadowOffsetTop :2,
		shadowOffsetLeft:2,
		shadowBlur :1,
		shadowOpacity :0.05
	});

	// contact us form validation
	$('#webform-client-form-110').submit(function(){
		form_id = $(this).attr('id');
		if (validation(form_id) != true) {
			return false;
		}
	});

	// newsletter submission - ajax form / custom validation
	$('#webform-client-form-131').submit(function(){
		form_id = $(this).attr('id');
		if (validation(form_id) != false) {
			$(this).ajaxSubmit({
				target: '#node-131'
			});
		}
		return false;
	});

	$('<div id="busy"></div>')
		.ajaxStart(function() {$(this).show();})
		.ajaxStop(function() {$(this).hide();})
		.appendTo('#node-131');


	function validation(form_id) {
		var errors = 0;
		$('#'+form_id+" input.required").each(function(){
			if ($(this).val() == "") {
				$(this).addClass("error");
				errors++;
			} else {
				$(this).removeClass("error");
			}
		});
		$('#'+form_id+" select.required").each(function(){
			if ($(this).val() == "none") {
				$(this).addClass("error");
				errors++;
			} else {
				$(this).removeClass("error");
			}
		});
		$('#'+form_id+" input.email").each(function(){
			var email_value = $(this).val();
			if (email_value.indexOf("@") < 0 || email_value.indexOf(".") < 0) {
				$(this).addClass("error");
				errors++;
			}else {
				$(this).removeClass("error");
			}
		});
		$('#'+form_id+" textarea.required").each(function(){
			if ($(this).val() == "") {
				$(this).addClass("error");
				errors++;
			} else {
				$(this).removeClass("error");
			}
		});

		//return errors;
		if (errors > 0) {
			return false;
		} else {
			return true;
		}
	}

	// contact us form  - industry type additional field
	$('#webform-component-about-your-company--other-industry-type label').append('<span class="form-required">*</span>');
	$('#edit-submitted-about-your-company-other-industry-type').addClass('required');
	$('#edit-submitted-about-your-company-industry-type').change(function(){
		if ($(this).val() == 'other') {
			$('#webform-component-about-your-company--other-industry-type').show();
			$('#edit-submitted-about-your-company-other-industry-type').addClass('required');
		} else {
			$('#webform-component-about-your-company--other-industry-type').hide();
			$('#edit-submitted-about-your-company-other-industry-type').removeClass('required');
		}
	});
	
	// right rail experts image aligning
	if ($('#block-views-experts-block-1').length > 0 || $('#block-related-experts-related-experts').length > 0) {
		expert_img_height = $('.expert-info img').height();
		expert_info_height = $('.expert-info').height();
		expert_info_txt_height = $('.expert-info-txt').height();
		if (expert_img_height < expert_info_height) {
			margin_top = expert_img_height - expert_info_height + 1;
			if (margin_top < -5) margin_top = -5;
			$('.expert-info').css('marginTop', margin_top+'px');
		}
	}
	

	// add a link over the whole our experts block
	var elem = $('.read-bio-link');
	var link = $('.read-bio-link .expert-fields a:last');
	var href = link.attr('href');
	$('<a class="read-bio-link-link" href="' + href + '"> </a>').appendTo(elem).css({
			display: 'block',
			position: 'absolute',
			top: 0,
			left: 0,
			width: elem.width(),
			height: elem.height(),
			zIndex: 9999
		}).mouseenter(function(){
			link.css({textDecoration: 'underline'});
		}).mouseleave(function(){
			link.css({textDecoration: 'none'});
		})
	
	//sitemap titles sanitation
	$(".page-sitemap h2.title").each(function(){
		var title = $(this).text();
		title = title.replace(/ menu/gi,"");
		$(this).text(title);
	});
	
	// external links 
	$('a[rel="external"]').attr('target', '_blank');
	
	//fb wall module
	$('#fb-wall').fbWall({id:'PDINinthHouse',accessToken:'206158599425293|341b453e941ca71fa77ecdb9.1-1028862273|7Mp_ITfnx2Gy7dkw_6vpW5PzyD8',showGuestEntries:false,showComments:false,max:5,timeConversion:24});
	
});

