$(function(){
	$(".header-dropdown a.btn-select").click(function(){
		$(this).parents(".header-dropdown").toggleClass("open");
		$('.header-dropdown .scroll-area').jScrollPane();
	});
	// fade gallery
	var i=1000;
	$('.visual-gallery li').each(function(){
		$(this).css('z-index',i);
		i--;
	})
	$('.visual').gallery({
		duration: 700,
		autoRotation: 4000,
		listOfSlides: 'ul.visual-gallery > li',
		switcher: 'ul.switcher > li',
		effect: true
	});
	

	$('.slider').gallery({
		duration: 700,
		autoRotation: 4000,
		listOfSlides: '.slider-gallery > .content-box',
		switcher: '.slider-switcher > li',
		effect: true
	});
	
	
	
	//alert('[href="' + thishref  +'"]');
	
	//jQuery('.main-nav [href="' + thishref  +'"]').parent("li.mainhead").addClass("active");
	
	//console.log(jQuery('.main-nav a[href="' + thishref  +'"]').parentsUntil(".main-nav").last().addClass("active"));
	
	// init accordion
	$('.accordion').not(".noajax").accordion({
		active: 'active', 
		header: 'h3', 
		autoheight: false 
	});
	// init bizier
	/*
	$('.tab-layout .tabset .content-box').bezier({
		mediumPos:{x:0, y:0},
		endPos   :{x:200, y:100},
		duration : 10,
		frameRate: 25
	})*/
	
	$('.tab-layout .content-box.grid a.btn-learn-more').live('click',function(){
		var $me=$(this).closest('.content-box');
		$me.addClass('clicked');
		
		$('.tab-layout .content-box').not('.clicked').each(function(){
			$(this).fadeOut('fast',function(){
				$(this).removeClass('grid').addClass('tab').show();
			});
		});
	
		$me.fadeOut('fast',function(){
			$(this).removeClass('grid').removeClass('clicked').addClass('tab').show();
			$me.click();
		});
		
		return false;

	});
	//$('.tab-layout .content-box.grid:first').click();
	
	$('.tab-layout .content-box .btn-next').click(function(e){
		if($(this).closest('.content-box').next().length > 0) {
			$(this).closest('.content-box').next().click();
		} else {
			//$(this).closest('.content-box').first().click();
			$('.tab-layout .content-box.tab').first().click();
			$(this).parent(".tabset").children('tab').first().click();
			
		}
		return false;
	})
	
	var i=0;
	$('.tab-layout .content-box.tab').each(function(){
		$(this).attr('id','content_tab_'+i);
		i++;
	});


		
		//0 - calculating the current top/left position
		//1 - switching present item into absolute model
		//2 - adding margin-top for the next sibling
		//3 - assiging calculated top/left position
		
		//4 - take the default coordinates of big box
		//5 - apply the opacity = 0.3
		//6 - hide the inner content
		//7 - bezier path with following grow
		//8
		//fin - reset the op

	$('.tab-layout .content-box.tab').live('click',function(){
		
		var $me=$(this);
		var $parent=$me.closest('.tabset');

		var pos2={top:0,left:250};
		var w2=436; // 353
		var h2=360;
		var h=$me.outerHeight();
		h+=(h-$me.height())/4; //to compensate the bottom margin

		//$('.tab-layout .content-box.big-box').removeClass('big-box').detach().appendTo($parent).addClass('tab');
		
		if($('.tab-layout .content-box.big-box').size()>0){
			//hiding the box
			var $box=$('.tab-layout .content-box.big-box');
			//$box.detach().removeClass('big-box').addClass('tab').appendTo($parent);
			$box.removeClass('big-box').addClass('tab');
			
			var pos0=$box.position();
			pos0.top-=h;
			var w1=$box.width();
			var h1=$box.height();
			
			$box.removeClass('tab').addClass('big-box');
			$box.css({opacity:0.7,'background':'#e4e3e3','z-index':7,padding:''});
			$box.find('.inner').hide();
			
			
			var path0 = new $.path.bezier({
			    start: {x:pos2.left, y:pos2.top, angle: 120, length: 0.1},
			    end: {x:pos0.left, y:pos0.top, angle: 90, length: 0.1}
			 });


			var $next=$box.next('.content-box');
			
			if($next.attr('id')==$me.attr('id')){
				$next=$me.next();
			}

			if($next.size()>0){
				$next.animate({'margin-top':h+'px'},400,function(){
					$(this).removeAttr('style');					
				});
			}
			
			$box.animate(
				{
					path: path0,	
					width:w1,
					height:h1
				}, 500,function(){
				$box.removeClass('big-box').addClass('tab').removeAttr('style').find('.inner').fadeIn('fast');
				
			});
			
		}
		
		var pos1=$me.position();
		
		
		var $next=$me.next();
		if($next.size()>0){
			$next.css('margin-top',h+'px');
		}

		$me.css({position:'absolute',top:pos1.top,left:pos1.left,opacity:0.7,'background':'#e4e3e3','z-index':15});		
		$me.find('.inner').hide();
		
		var path = new $.path.bezier({
		    start: {x:pos1.left, y:pos2.top, angle: -45, length: 2},
		    end:   {x:pos2.left, y:pos2.top, angle: 20, length: 0}
		 });
		
		 
		$me.animate(
			{
				path: path,	
				width:w2,
				height:h2
			}, 500,function(){
			$me.removeAttr('style').removeClass('tab').addClass('big-box').find('.inner').fadeIn('fast');
		});	
		
		$next.animate({'margin-top':0},400,function(){$(this).removeAttr('style')});
			
	});
	
	// init tabs
	$(".tabs").tabs();
	// init fancybox
	$(".video").click(function() {
		$.fancybox({
			'autoscale': false,
			'transitionIn': 'none',
			'transitionOut': 'none',
			'padding': 0,
			'title': this.title,
			'width': 610,
			'height': 360, /* 275 optimal for vid w/ playlist */
			'type': 'swf',
			'href': this.href,
			'swf': {
				'wmode': 'transparent',
				'allowfullscreen': 'true'
			}
		});
		return false;
	});
	
	$(".video_multiple").click(function() {$.fancybox({
			'autoscale': false,
			'transitionIn': 'none',
			'transitionOut': 'none',
			'padding': 0,
			'title': this.title,
			'width': 640,
			'height': 270, /* 275 optimal for vid w/ playlist */
			'type': 'swf',
			'href': this.href,
			'swf': {
				'wmode': 'transparent',
				'allowfullscreen': 'true'
		
			}
		});
		return false;
	});
	
	$(".readbio").click(function() {
		$.fancybox({
			'autoscale': false,
			'transitionIn': 'none',
			'transitionOut': 'none',
			'padding': 0,
			'title': this.title,
			'width': 600,
			//'type': 'swf',
			'href': this.href
		});
		return false;
	});
	
	
	$(".pageNav").each(function() {
		var cur = null;
		
		function showMenu() {
			var w = $(this).data("menuWidth"),
				h = $(this).data("menuHeight"),
				menu = $(">.submenu-helper", this),
				menuSub = $(".sub-menu", this),
				menuLink = $(">a", this);
				
			if ( menu.length && w && h ) {
				menuLink.addClass('hover');
				menu.css({
					height: h + 'px'
				});
				menuSub.css({
					display: 'block'
				});
				//menu.stop().animate({height:h}, {queue:false, duration:600, easing:"easeOutExpo"});
			}
		}
		
		function hideMenu() {
			var w = $(this).data("menuWidth"),
				h = $(this).data("menuHeight"),
				menu = $(">.submenu-helper", this),
				menuSub2 = $(".sub-menu", this),
				menuLink2 = $(">a", this);
				
			if ( menu.length && w && h ) {
				menu.css({
					height: '0px'
				});
				//menu.stop().animate({height:0}, {queue:false, duration:600, easing:"easeInExpo"});
				menuLink2.removeClass('hover');
				menuSub2.css({
					display: 'none'
				});
			}
		}
		
		//jQuery('.main-nav a[href="' + thishref  +'"]').parentsUntil(".main-nav").last().addClass("active");
		//Set active header
		var fullhref = window.location.href.split("/");
		var thishref = fullhref[fullhref.length - 1];
		
		$(".pageNav>ul>li").last().addClass("lastMenu");
		$('.pageNavMenus a[href="' + thishref  +'"]').parentsUntil(".pageNavMenus").last().addClass("active");
		$('.sub-menu a[href="' + thishref  +'"]').parent('li').addClass("active");
		
		//jQuery('.main-nav a[href="' + thishref  +'"]').parentsUntil(".main-nav").last().addClass("active");
		//jQuery('.sub-nav a[href="' + thishref  +'"]').parent('li').addClass("active");
	
		$(".pageNav>ul>li>ul").each(function(){
			var w = $(this).outerWidth(true),
				h = $(this).outerHeight(true);
			
			$(this).parent().data("menuWidth", w);
			$(this).parent().data("menuHeight", h);				
			
			$(this).wrap("<div class='submenu-helper' style='width:"+w+"px;height: " + h + "px;overflow:hidden;'><div class='submenu-holder' style='width:" + w + "px; height:" + h +"px;'></div></div>");
			
			
		});
		
		$(".pageNav>ul>li").bind({
			"mouseover": showMenu,
			"mouseout": hideMenu
		});
	});
	
	
});

