var portfolioItemWidth = 192;
var portfolioNrVisible = 3;
var portfolioIsAnimating = false;


$(document).ready(function() {
	/*if (document.getElementById("kaus-balloon")) {
		setTimeout("kausLevitate()", 1000);
	}*/

	if ( $("#portfolio-homepage").length ) {
		portfolioHomepageSwitcher();
	}

	// portfolio functionality
	$(".widget-kausportfolio div.left").each(function(i) {
		var ID = "kaus-portfolio-left-" + i;
		$(this).addClass(ID);
		$(this).children("div:first").attr("id", ID);
		showArrow("leftDisabled", ID, "portfolioPrev");
	});
	$(".widget-kausportfolio div.right").each(function(i) {
		var ID = "kaus-portfolio-right-" + i;
		$(this).addClass(ID);
		$(this).children("div:first").attr("id", ID);
		var nrItem = $(this).nextAll("div.center:first").find("li").length;
		showArrow( (nrItem <= portfolioNrVisible) ? "rightDisabled" : "right", ID, "portfolioNext");
	});

	// portfolio pagination functionality
	if ( document.getElementById("pgn") ) {
		addPaginationFunctionality();
	}

	$(".our-services-wrapper:first h4").each(function() {
		$(this).nextUntil("h4").css("width", "650px").css("margin-left", "40px").hide();
	});
	$(".our-services-wrapper:first h4").each(function() {
		$(this).click(function() {
			var $this		= $(this);
			var isClosed	= $this.hasClass("closed");

			if ( $(".our-services-wrapper:first h4:not(.closed)").length ) {
				$(".our-services-wrapper:first h4:not(.closed)").addClass("closed");
				var nrOpened = $(".our-services-wrapper:first h4:first").nextAll("p, ul").length;
				$(".our-services-wrapper:first h4:first").nextAll("p, ul").slideUp(400, function() {
					if (--nrOpened == 0 && isClosed) {
						$this.nextUntil("h4").slideDown();
						$this.removeClass("closed");
					}
				});
			}
			else {
				$this.nextUntil("h4").slideDown();
				$this.removeClass("closed");
			}
		});
	});
});


var kausLevitatePath = new Array(new Array(3, 7), new Array(3, -7), new Array(-3, -7), new Array(-3, 7));
var kausLevitatePathIndex = 0;
var kausLevitateSpeedMS = 3000;
function kausLevitate() {
	var x = kausLevitatePath[kausLevitatePathIndex][0];
	var y = kausLevitatePath[kausLevitatePathIndex][1];

	$("#kaus-balloon").animate({
			left: ((x < 0) ? "-" : "+") + "=" + Math.abs(x),
			top: ((y < 0) ? "-" : "+") + "=" + Math.abs(y)
		},
		kausLevitateSpeedMS, function() {
			if (++kausLevitatePathIndex > 3) kausLevitatePathIndex = 0;
			setTimeout("kausLevitate()", 500);
		}
	);
}



function portfolioHomepageSwitcher() {
	var tag = ($.browser.msie) ? 'span' : 'img';
	var $active = $('#portfolio-homepage ' + tag + '.active');

	if ($active.length == 0) $active = $('#portfolio-homepage ' + tag + ':last');

	var $next = $active.next().length ? $active.next() : $('#portfolio-homepage ' + tag + ':first');

	//$active.addClass('last-active');

	$active.fadeOut(300, function() {
		$active.removeClass("active");
		$next.fadeIn(300, function() {
			$next.addClass("active");
			setTimeout("portfolioHomepageSwitcher()", 4000);
		});
	});
}


function portfolioPrev(objID) {
	if (portfolioIsAnimating) return false;

	var $ul = $("." + objID + ":first").nextAll("div.center:first").children("ul:first");
	var curLeft = Math.round($ul.position().left / portfolioItemWidth) * portfolioItemWidth; // IE correction
	if (-curLeft >= portfolioItemWidth) {
		portfolioIsAnimating = true;
		getMovieName("SWF-" + objID.replace("left", "right")).showRight();
		var newLeft = curLeft + portfolioItemWidth;
		if (newLeft == 0) getMovieName("SWF-" + objID).showLeftDisabled();
		$ul.animate({left: newLeft}, 400, function() {
			portfolioIsAnimating = false;
		});
	}
	return false;
}

function portfolioNext(objID) {
	if (portfolioIsAnimating) return false;

	var $ul = $("." + objID + ":first").nextAll("div.center:first").children("ul:first");
	var curLeft = Math.round($ul.position().left / portfolioItemWidth) * portfolioItemWidth; // IE correction
	var maxLeft = ($ul.children("li").length - 1 - portfolioNrVisible) * portfolioItemWidth;
	if (-curLeft <= maxLeft) {
		portfolioIsAnimating = true;
		getMovieName("SWF-" + objID.replace("right", "left")).showLeft();
		var newLeft = curLeft - portfolioItemWidth;
		if (-newLeft == maxLeft + portfolioItemWidth) getMovieName("SWF-" + objID).showRightDisabled();
		$ul.animate({left: newLeft}, 400, function() {
			portfolioIsAnimating = false;
		});
	}
	return false;
}


function showArrow(direction, containerID, fnName) {
	var swfObjectId = "SWF-" + containerID;
	flashVars = {
		arrowDir: direction,
		htmlObjectId: swfObjectId,
		callFunction: fnName,
		callFunctionVariable: containerID
	};
	var params = {
		allowScriptAccess: "always",
		wmode: "transparent"
	};
	var attributes = {
		id: swfObjectId,
		name: swfObjectId
	};
	swfobject.embedSWF(baseURL + "/wp-content/themes/kaus/flash/arrows.swf", containerID, "55", "56", "10.0.0", baseURL + "/wp-content/themes/kaus/flash/expressInstall.swf", flashVars, params, attributes);
}


function getMovieName(movieName) {
	return (navigator.appName.indexOf("Microsoft") != -1) ? window[movieName] : document[movieName];
}



var portfolioNrItemPerPage = 3;
var portfolioNrPage = 0;
var portfolioCurPage = 1;

function addPaginationFunctionality() {
	portfolioNrPage = Math.ceil($(".widget-kausportfolio").length / portfolioNrItemPerPage);
	if (portfolioNrPage > 1) {
		for (var i=1; i<=portfolioNrPage; i++) {
			$("#pgn div:first span:first").append('<a href="#page-' + i + '" class="pgn-' + i + ((i==1) ? ' sel' : '') + '" onclick="return pgnGoTo(' + i + ')">' + i + '</a> ');
		}
		$("#pgn").show();
	}

	$(".widget-kausportfolio:gt(" + (portfolioNrItemPerPage-1) + ")").hide();
}


function pgnFirst() {
	pgnGoTo(1);
	return false;
}

function pgnPrev() {
	if (portfolioCurPage > 1)
		pgnGoTo( --portfolioCurPage );
	return false;
}

function pgnNext() {
	if (portfolioCurPage < portfolioNrPage)
		pgnGoTo( ++portfolioCurPage );
	return false;
}

function pgnLast() {
	pgnGoTo(portfolioNrPage);
	return false;
}

function pgnGoTo(page) {
	portfolioCurPage = page; // page will contain the new page value

	if (page == 1) $("#pgn .first:not(.dsbl), #pgn .prev:not(.dsbl)").addClass("dsbl");
	else $("#pgn .first, #pgn .prev").removeClass("dsbl");
	if (page == portfolioNrPage) $("#pgn .last:not(.dsbl), #pgn .next:not(.dsbl)").addClass("dsbl");
	else $("#pgn .last, #pgn .next").removeClass("dsbl");
	$("#pgn div:first span:first a").removeClass("sel");
	$("#pgn div:first span:first a.pgn-" + page).addClass("sel");

	$("html, body").animate({ scrollTop: $(".post:first").offset().top }, 300, function() {
		$("#portfolio_list").slideUp(600, function() {
			$(".widget-kausportfolio").hide();
			for (var i = (page-1) * portfolioNrItemPerPage; i < page * portfolioNrItemPerPage; i++)
				$(".widget-kausportfolio:eq(" + i + ")").show();
			$("#portfolio_list").slideDown(600);
		});
	});

	return false;
}
