/*
	CIG  - CENTRALINDUSTRYGROUP.COM

	(c) 2010 - TRES internet
	http://www.tres.nl/
*/

Shadowbox.init({
	troubleElements: ["embed", "object"],
	overlayColor: "#FFF",
	overlayOpacity: 0.75
});

/**
  * Cufon replacements
**/
$(function() {
	Cufon.replace("h1, h2, h3, h4, h5, h6");
	Cufon.replace("#topMenu a", {hover: true});
	Cufon.replace("#tagcloud a", {hover: true});
});



/**
  * Alert configuration
**/
$(function() {
	$.alerts.overlayOpacity = 0.75;
	$.alerts.overlayColor = "#FFF";
});

/**
  * Project images
**/
$(function() {
	$("div.image.project").filter(function(index) {
		return index % 3 == 2;
	}).addClass("last");
});


/**
  * IE workarounds
**/
$(function() {
	$("nav#buMenu li:last-child").addClass("lastChild");
});


/**
  * Equal column heights
**/
$(function() {
	
	/**
	  * Currently disabled - columns / rows all have fixed heights on homepage
	// Blocks on homepage
	var itemHeight = 0;
	$items = $("body.home aside#items section.item");
	$items.each(function() {
		if ($(this).height() > itemHeight) {
			itemHeight = $(this).height();
		}
	});
	$items.height(itemHeight);
	*/

	// Content / items on pages
	itemHeight = 0;
	$columns = $("body.page #content, body.page #items");
	$columns.each(function() {
		if ($(this).height() > itemHeight)
			itemHeight = $(this).height();
	});
	$columns.css({"min-height": itemHeight + 30});
});


/**
  * Force popup in content
**/
$(function() {
	$header = $("#header");

	$("nav#buMenu li").mouseover(function() {
		$popup = $(this).find(".popup");

		if ($(this).offset().left - 100 < $header.offset().left)
			$popup.css({"left": 10});
		
		var right = $(this).offset().left + $popup.width();
		var totalWidth = $header.offset().left + $header.width();
		if (right > totalWidth - 10)
			$popup.css({"left": "auto", "right": "-20px" });
	});
});


/**
  * Header image scroller
**/
function scrollHeader() {
	if ($("#headerImageWrapper").children().length <= 1)
		return ;
	$current = $("#headerImage img.active");
	$next = $current.next();
	if ($next.length <= 0)
		$next = $("#headerImage img:first-child");

	$("#headerImage").scrollTo($next, 500);
	$current.removeClass("active");
	$next.addClass("active");
	headerSliderTimeout = window.setTimeout(scrollHeader, 6000);
}

$(function() {
	$("#headerImage img:first-child").addClass("active");
});
var headerSliderTimeout = window.setTimeout(scrollHeader, 6000);

/**
  * Image scrollers
**/
$(function() {
	$("#items div.scrollerContainer.projects").tresScroller({itemsPerPage: 1});
	$("#items div.scrollerContainer.quote").tresScroller({itemsPerPage: 1, timeout: 10000});
	$("#content div.project.detail div.scrollerContainer").tresScroller({itemsPerPage: 1, autoscroll: false});
	$("section.footer div.scrollerContainer").tresScroller({itemsPerPage: 3});
});


/**
  * Items on homepage - borders
  *   Due the lack of nth-child-support, this is best done with jQuery
**/
$(function() {
	$("body.bu.home #items > .item").filter(function(index) {
		return index >= 3;
	}).addClass("borderTop");
	$("body.bu.home #items > .item").filter(function(index) {
		return index % 3 == 0;
	}).css({"borderLeft": 0});
});


/**
  * Minor style adjustments for some content
**/
$(function() {
	// Hide contentcell on homepages if content is empty
	if ($("body.home #content").children().length <= 1)
		$("body.home #content").hide();
});

/**
  * Toggle images / video on project detail
**/
$(function() {
	$("div.project.detail a.toggleImages").toggle(function() {
		$(this).closest("div.project.detail").find("div.scrollerContainer").slideDown();
		$(this).html($(this).html().replace("View", "Hide"));
	}, function() {
		$(this).closest("div.project.detail").find("div.scrollerContainer").slideUp();
		$(this).html($(this).html().replace("Hide", "View"));
	});
});


/**
  * Make titles of items that contain readMore-links clickable
**/
$(function() {
	$("section.item .readMore").each(function() {
		var $link = $(this).attr("href");
		$(this).closest("section.item").find("h2").css({"cursor": "pointer"}).click(function() {
			document.location = $link;
		});
	});

	// Remove readMore-link for projects on homepage
	$("section.item .scrollerContainer").each(function() {
		$(this).closest("section.item").find("a.readMore").hide();
	});
});

$(function() {
	$('a.track-download').click(function(){
		_gaq.push(['_trackPageview', '/downloads/' + $(this).attr('data-filename')])
	});
});
