jQuery(document).ready(function () {

	var bEnd = false;
	var preData = "";
	var nStart = 1;

	$('#globalNav ul li a').each(function () {
		bUse = true;

		if ($(this).hasClass('active')) {
			bEnd = true;
			bUse = false;
			$('#togglelist').prepend(preData);
		}
		if (bUse) {
			$.ajax({ url: $(this).attr("href"),
				data: 'nogui=true&sroot=' + _root
			, success: function (data) {
				sData = '<li class=\"toggleLi\">' + data + '</li>';
			},
				async: false
			});

			if (bEnd) {
				$('#togglelist').append(sData);
			} else {
				preData = preData + sData;
				nStart++;
			}
		}
	});

	swfobject.embedSWF(_root + 'flash/home.swf', 'homeFlash', '137', '427', '9.0.0', "", "", { wmode: 'transparent' });
	swfobject.embedSWF(_root + 'flash/creativity.swf', 'creativityFlash', '137', '427', '9.0.0', "", "", { wmode: 'transparent' });

	$('.content').each(function () {
		if (!$(this).hasClass('noColum')) {
			$(this).columnize({ columns: 2, balanced: false });
		}
	});

	jQuery('#togglelist').jcarousel({
		scroll: 1,
		buttonNextHTML: null,
		buttonPrevHTML: null,
		start: nStart,
		wrap: 'none',
		itemVisibleInCallback: { onAfterAnimation: mycarousel_after },
		initCallback: mycarousel_initCallback
	});

	function mycarousel_initCallback(carousel) {
		jQuery('#globalNav ul li a').bind('click', function () {
			carousel.scroll(jQuery.jcarousel.intval(jQuery(this).attr('id')));
			return false;
		});
		jQuery('#homenav li a').bind('click', function () {
			carousel.scroll(jQuery.jcarousel.intval(jQuery(this).attr('rel')));
			return false;
		});
		jQuery('#homenav li a').bind('click', function () {
			carousel.scroll(jQuery.jcarousel.intval(jQuery(this).attr('rel')));
			return false;
		});

		jQuery('#bindFlashcreativity').bind('click', function () {
			carousel.scroll(jQuery.jcarousel.intval(jQuery(this).attr('rel')));
			return false;
		});

		jQuery('#bindFlashHome').bind('click', function () {
			carousel.scroll(jQuery.jcarousel.intval(jQuery(this).attr('rel')));
			return false;
		});

		jQuery('#logo').bind('click', function () {
			carousel.scroll(jQuery.jcarousel.intval(1));
			return false;
		});
	};


	function mycarousel_after(carousel, item, i, state, evt) {
		$("#globalNav ul li > a").removeClass("active");
		$("#globalNav ul li a:eq(" + (i - 1) + ")").addClass("active");

		$('.image img').hide();
		$('.jcarousel-item-' + i + ' .image img').fadeIn("slow");

		jQuery('.jcarousel-item-' + i + ' a').each(function () {
			if ($(this).attr("target").toLowerCase() != '_blank' && $(this).attr("href") != '#' && $(this).attr("class").toLowerCase() != "download") {

				$(this).bind('click', function () {
					fillContent($(this).attr("href"), i);
					return false;
				});
			}
		});

		jQuery('.jcarousel-item-' + i + ' .embed span').each(function () {
			$(this).bind('click', function () {
				$('.embedwrapper').css({ 'display': 'block' });
				$('.embedbox').css({ 'margin-left': '-320px' });
				$('#embedplayer').html('<iframe src="http://player.vimeo.com/video/18529682" width="640" height="360" frameborder="0"></iframe>');
				return false;
			});
		});
	};

	//ajax in content verwerken


	function fillContent(sUrl, location) {
		$.ajax({ url: sUrl,
			data: 'nogui=true&sroot=' + _root
			, success: function (data) {
				jQuery('.jcarousel-item-' + location).html(data);
				$('.jcarousel-item-' + location + ' .content').columnize({ columns: 2, balanced: false });
				$('.jcarousel-item-' + location + ' .image img').fadeIn("slow");

				jQuery('.jcarousel-item-' + location + ' a').each(function () {
					if ($(this).attr("target").toLowerCase() != '_blank' && $(this).attr("href") != '#' && $(this).attr("class").toLowerCase() != "download") {
						$(this).bind('click', function () { fillContent($(this).attr("href"), location); return false; });
					}
				});

			},
			async: false
		});
	}

	/*$('.buildQtip').each(function () {
	$(this).qtip({
	position: { corner: 'center', target: $(document.body) },
	content: {
	url: _root + 'include/projectinfo.asp',
	data: { sRoot: _root, projectID: $(this).attr('rel') },
	method: 'get',
	title: {
	text: $(this).attr('title'),
	button: 'Close'
	}
	},
	screen: {
	screen: true
	},
	style: {
	width: 400,
	height: 350,
	padding: 5,
	background: '#2c2c2c',
	color: 'white',
	textAlign: 'center',
	border: {
	width: 2,
	color: '#ffffff'
	},
	title: {
	padding: 5,
	background: '#2c2c2c',
	color: 'white',
	'font-weight': 'normal'
	}
	},
	show: { solo: true, ready: false, when: 'click' },
	hide: { when: 'click', fixed: true }
	});
	});*/

	$('a.buildQtip').click(function () {
		var projectID = $(this).attr('rel'),
			root = _root;

		$.fancybox({
			'href': root + 'include/projectinfo.asp?root=' + root + '&projectID=' + projectID,
			'type': 'ajax',
			'overlayColor': '#000',
			'transitionIn': 'fade',
			'transitionOut': 'fade',
			'speedIn': 600,
			'speedOut': 200

		});

		return false;
	});





	$('.loading').fadeOut("slow");
});
