var current = 1;
var total = 3;
var timerCounter=1;
var secondTimeout = 10;
var secondFadeIn = 0.3;
var secondFadeOut = 0.3;
var timer,timerBar;

function initPage()
{
	initNav({
		menuId: "nav",
		cleverMode: true,
		hoverClass: "hover",
		flexibility: true,
		sideClasses: true,
		menuPaddings: 0,
		minWidth: 100
	});
	
	jQuery('.tabset a').click(function() {
		current=parseInt(jQuery(this).attr("id"));
		switchAreaContent(current);
		return false;
	});
	
	jQuery(".area-section").mouseover(function() {
		jQuery('#loadingBarForeground').stop(true);
		timerCounter = (30 - parseInt(jQuery('#loadingBarForeground').css("width")))/30;
	}).mouseout(function(){
		doTimerBar();
	});
	
	doTimerBar();
}

function doTimerBar()
{

	jQuery('#loadingBarForeground').animate({
		width: '30px'
	}, {
	duration: (secondTimeout * timerCounter) * 1000,
	complete: function() {
		timerCounter = 1;
		jQuery("#loadingBarForeground").css("width","0px");
		cycleArea();
		doTimerBar();	
	}
	});	
	
}

function cycleArea()
{
		jQuery(".area").fadeOut(secondFadeOut*1000, function() {
		current++
		if(current > total)
			current = 1;
		switchAreaContent(current);
		jQuery(".area").fadeIn(secondFadeIn*1000);
      });
}

function switchAreaContent(num)
{
	switch(num)
	{
		case 1:
			jQuery("#handMarketing").show();
			jQuery("#handInteractive,#handMobile").hide();
			jQuery(".area-holder").removeClass("personInteractive")
									.removeClass("personMobile")
									.addClass("personMarketing");
			break;
		case 2:
			jQuery("#handMarketing,#handMobile").hide();
			jQuery("#handInteractive").show();
			jQuery(".area-holder").removeClass("personMobile")
									.removeClass("personMarketing")
									.addClass("personInteractive");
			break;
		case 3:
			jQuery("#handMarketing,#handInteractive").hide();
			jQuery("#handMobile").show();
			jQuery(".area-holder").removeClass("personMarketing")
									.removeClass("personInteractive")
									.addClass("personMobile");
			break;
		default:
			break;
	}

	jQuery(".tabset").children().removeClass("active");
	jQuery(".holder-section .tab").children(".blockContent").hide();
	jQuery(".holder-section .tab .blockContent:nth-child("+current+")").show();
	jQuery(".tabset li:nth-child("+current+")").addClass("active");
}

function initNav(o)
{
	if (!o.menuId) o.menuId = "main-nav";
	if (!o.cleverMode) o.cleverMode = false;
	if (!o.flexibility) o.flexibility = false;
	if (!o.dropExistenceClass) o.dropExistenceClass = false;
	if (!o.hoverClass) o.hoverClass = "";
	if (!o.menuHardCodeClass) o.menuHardCodeClass = "menu-hard-code";
	if (!o.sideClasses) o.sideClasses = false;
	if (!o.center) o.center = false;
	if (!o.menuPaddings) o.menuPaddings = 0;
	if (!o.minWidth) o.minWidth = 0;
	if (!o.coeff) o.coeff = 1.7;
	var n = document.getElementById(o.menuId);
	if(n)
	{
		n.className = n.className.replace(o.menuHardCodeClass, "");
		var lfl = [];
		var li = n.getElementsByTagName("li");
		for (var i=0; i<li.length; i++)
		{
			li[i].className += (" " + o.hoverClass);
			var d = li[i].getElementsByTagName("div").item(0);
			if(d)
			{
				if(o.flexibility)
				{
					var a = d.getElementsByTagName("a");
					for (var j=0; j<a.length; j++)
					{
						var w = a[j].parentNode.parentNode.offsetWidth;
						if(w > 0)
						{
							if(typeof(o.minWidth) == "number" && w < o.minWidth)
								w = o.minWidth;
							else if(typeof(o.minWidth) == "string" && li[i].parentNode == n && w < li[i].offsetWidth)
								w = li[i].offsetWidth - 3;
							a[j].style.width = w - o.menuPaddings + "px";
						}
					}
					d.style.width = li[i].getElementsByTagName("div").item(1).clientWidth + "px";
				}
				var t = document.documentElement.clientWidth/o.coeff;
				if(li[i].parentNode != n && (!o.cleverMode || fPX(li[i]) < t))
				{
					d.style.right = "auto";
					d.style.left = li[i].parentNode.offsetWidth + "px";
					d.parentNode.className += " left-side";
				}	
				else if(li[i].parentNode != n && (o.cleverMode || fPX(li[i]) >= t))
				{
					d.style.left = "auto";
					d.style.right = li[i].parentNode.offsetWidth + "px";
					d.parentNode.className += " right-side";
				}
				else if(li[i].parentNode == n && o.cleverMode && fPX(li[i]) >= t)
				{
					li[i].className += " right-side";
				}
				if(li[i].parentNode == n && o.center)
					d.style.left = -li[i].getElementsByTagName("div").item(1).clientWidth/2 + li[i].clientWidth/2 + "px";
			}
			if(o.dropExistenceClass && li[i].getElementsByTagName("ul").length > 0)
			{
				li[i].className += (" " + o.dropExistenceClass);
				li[i].getElementsByTagName("a").item(0).className += (" " + o.dropExistenceClass + "-link");
				li[i].innerHTML += "<em class='pointer'></em>";
			}
			if(li[i].parentNode == n) lfl.push(li[i]);
		}
		if(o.sideClasses)
		{
			lfl[0].className += " first-child";
			lfl[0].getElementsByTagName("a").item(0).className += " first-child-link";
			lfl[lfl.length-1].className += " last-child";
			lfl[lfl.length-1].getElementsByTagName("a").item(0).className += " last-child-link";
		}
		for (var i=0; i<li.length; i++)
		{
		}
	}
	_time = setTimeout(function(){			}, 1000);
	(function($) {
		var nav = $('#nav');
		var nav_L = nav.offset().left;
		var nav_W = nav.width();
		nav.find('li').each(function(){
			var this_el = $(this);
			var hoverClass = 'hover';
			this_el.removeClass(hoverClass);
			var drop = this_el.find('.drop');
			if(drop.length){
				var this_el_W = this_el.outerWidth(true);
				var this_el_L = this_el.offset().left;
				var drop_W = drop.outerWidth();
				var drop_L = drop.offset().left;
				var drop_M = parseInt(drop.css('marginRight'));
				if($.browser.msie && $.browser.version<7){
					if(drop_W > this_el_W && drop_W > nav_W+nav_L-this_el_L+20){
						drop.css({
							marginRight: -(nav_L+nav_W-this_el_L-this_el_W-drop_M)
						});
					}
				}else{
					if(drop_W > this_el_W && drop_W > nav_W+nav_L-this_el_L){
						drop.css({
							marginRight: -(nav_L+nav_W-this_el_L-this_el_W-drop_M)
						});
					}
				}
			}
			this_el.mouseenter(function(){
				this_el.addClass(hoverClass);
				if(typeof(Cufon)=='function'){
					Cufon.refresh(' .btn-link, .footer-section h2, .article h2, .info-block h3, .sidebar-box .share, .btn-link2, .contact-box h2, .gallery h2, .content-section h3, .form-box h3, .info-content h2');
				}
			}).mouseleave(function(){
				if(typeof(Cufon)=='function'){
					Cufon.refresh(' .btn-link, .footer-section h2, .article h2, .info-block h3, .sidebar-box .share, .btn-link2, .contact-box h2, .gallery h2, .content-section h3, .form-box h3, .info-content h2');
				}
				this_el.removeClass(hoverClass);
			});
		})
		setInterval(function(){
			if(typeof(Cufon)=='function'){
				Cufon.refresh('.cufon-link, .btn-link, .footer-section h2, .article h2, .info-block h3, .sidebar-box .share, .btn-link2, .contact-box h2, .gallery h2, .content-section h3, .form-box h3, .info-content h2');
			}		}, 100);
	}(jQuery));
	
	function fPX(a)
	{
		var b = 0;
		while (a.offsetParent) {b += a.offsetLeft; a = a.offsetParent;}
		return b;
	}
	
}
if (window.addEventListener)
	window.addEventListener("load", initPage, false);
else if (window.attachEvent)
	window.attachEvent("onload", initPage);