//cursorMessage
function showCursorMessage(e) {
	$.cursorMessage('You just entered '+$(this).val().length+' characters...');
}

$(document).ready(function() {
	var timer, orignalBgColor, portfolioInitialized, rgbStringParts;

	var loadInIframeModal = function(hash){
		var trigger = $(hash.t);
		var modal = $(hash.w);

		var myUrl = trigger.attr('href');
		var myTitle= trigger.attr('title');
		var modalContent = $("#jqmContent", modal);

		modalContent.html('').attr('src', myUrl);
		//let's use the anchor "title" attribute as modal window title
		$('#jqmTitleText').text(myTitle);
		$('#jqmLink').attr('href', myUrl);
		modal.show();
	}

	//general js fixes
	$('#content.doubleColumn p:first').addClass('first');
	originalBgColor = $('#menu').css('background-color');


	$('#page10921648').mouseover(function() { $('#menu, #menu li').animate({ backgroundColor: '#0055A0' }, { duration: 500, queue: false }); }); //portfolio
	$('#page10921652').mouseover(function() { $('#menu, #menu li').animate({ backgroundColor: '#5F9CD1' }, { duration: 500, queue: false }); }); //over kingsquare
	$('#page10921657').mouseover(function() { $('#menu, #menu li').animate({ backgroundColor: '#C78A09' }, { duration: 500, queue: false }); }); //product
	$('#page10921660').mouseover(function() { $('#menu, #menu li').animate({ backgroundColor: '#AB3075' }, { duration: 500, queue: false }); }); //partners
	$('#page10921671').mouseover(function() { $('#menu, #menu li').animate({ backgroundColor: '#15970E' }, { duration: 500, queue: false }); }); //contact
	$('#page12302966').mouseover(function() { $('#menu, #menu li').animate({ backgroundColor: '#AD3838' }, { duration: 500, queue: false }); }); //blog
	$('#page10921648, #page10921652, #page10921657, #page10921660, #page10921671, #page12302966').mouseout(function() { $('#menu, #menu li').animate({ backgroundColor: originalBgColor }, { duration: 500, queue: false }); });

	//home
	$('#promoReel .promoLink').click(function(){
		if ($(this).next(':visible').length == 1) {
			$(this).next().slideUp('normal', function(){
				$(this).parent().removeClass('selected');
			});
		} else {
			$('#promoReel .promoContent').slideUp('normal');
			$(this).next().slideDown('normal', function(){
				$('#promoReel li').removeClass('selected');
				$(this).parent().addClass('selected');
			});
		}
	});
	$('.promoLink:first').click();

	timer = $.timer(5000, function(){
		var currentPromo = $('#promoReel .selected .promoContent');
		currentPromo.slideUp('normal')
		if (currentPromo.parent().next().length == 1) {
			currentPromo.parent().removeClass('selected').next().find('.promoContent').slideDown('normal', function(){
				$(this).parent().addClass('selected');
			});
		} else {
			currentPromo.parent().removeClass('selected');
			$('#promoReel li:first .promoContent').slideDown('normal', function(){
				$(this).parent().addClass('selected');
			});
		}
	});

	$('#promoReel li').hover(function(){
		timer.stop();
	}, function(){
		timer.reset(5000);
	});

	//portfolio
	$('#keyword, #address').labelHider();

	$('#portfolioColumn form').submit(function() {
		//we have javascript! Let's reset the portfolio list
		$('.portfolioList').html('<li><img src="/img/loading.gif" /></li>');

		$.post('/gateway/portfolio', ($(this).serializeArray()), function(data) {
			if (data.projects.length>0) { //fast click bug prevention
				var from, to, newProjectList, oldProjectList, startPosition;

				from = ((parseInt(data.page)*5)+1);
				$('#prevList').toggle(from>1);
				to = from + 4;
				if (to >= parseInt(data.totalResults)) {
					to = parseInt(data.totalResults);
					$('#nextList').hide();
				} else {
					$('#nextList').show();
				}

				$('#projectCounter').html(from+'-'+to);
				$('#projectTotal').html('('+data.totalResults+')');

				oldProjectList = $('#portfolioContainer .portfolioList');
				newProjectList = $('<ul class="portfolioList" style="left: '+(($('#forward').val()==1)?282:-282)+'px">');
				$.each(data.projects, function(key, project) {
					newProjectList.append('<li id="project_'+project.id+'"><a href="'+project.url+'" class="portfolioTitle"><span class="portfolioDate">'+project.date+'</span>'+project.title+'<img src="/download/?id='+project.imageId+'&amp;width=230&amp;height=53" alt="Website '+project.title+'" /></a></li>');
				});

				newProjectList.appendTo('#portfolioContainer');
				newProjectList.find('a.portfolioTitle').click(function(){
					var id, uriArray;
					$('#portfolioItem').html('<div class="loadingImg"><img src="/img/loading.gif" alt="Pagina wordt geladen." /></div>');
					uriArray = $(this).attr('href').split('/');
					if (uriArray[0] == 'http:') {  // IE < 8
						uriArray.shift();
						uriArray.shift();
					}
					id = uriArray[2];
					if (uriArray[1] == 'project'){
						$.get('/gateway/portfolio?localAction=details&id='+id, '', function(data){
							$('#portfolioItem').html(data);
							$('#linkWindow').jqmAddTrigger('#portfolioImage'); // jqModal trigger
						});
					} else {
						$.get('/gateway/partner?localAction=details&id='+id, '', function(data){
							$('#portfolioItem').html(data);
						});
					}
					return false;
				});
				oldProjectList.animate({left: (($('#forward').val()==1)?'-=282px':'+=282px')}, 'slow', function() {
					$(this).remove();
				});
				newProjectList.animate({left: (($('#forward').val()==1)?'-=282px':'+=282px')}, 'slow', function() {
					if (typeof portfolioInitialized == 'undefined') {
						portfolioInitialized = true;
					} else {
						// select new project if neccesary
						if ($('.portfolioList li:first').is(':not(#project_'+$('#projectId').val()+')')) {
							$('a.portfolioTitle:first').click();
						}
					}
					$('#portfolioColumn form #forward').val(1);
				});
			}
		}, 'json');
		return false;
	});
	$('#portfolioColumn input:not(#page)').click(function() {
		$('#portfolioColumn form #page').val(0);
		$('#portfolioColumn form').submit();
	});
	$('#portfolioColumn form').submit();

	$('#searchBar').click(function() {
		$('#portfolioColumn form').slideToggle("slow");
		$(this).toggleClass('open');
	});

	$('#prevList').click(function(){
		$('#portfolioColumn form #page').val(parseInt($('#portfolioColumn form #page').val())-1);
		$('#portfolioColumn form #forward').val(0);
		$('#portfolioColumn form').submit();
		return false;
	});

	$('#nextList').click(function(){
		$('#portfolioColumn form #page').val(parseInt($('#portfolioColumn form #page').val())+1);
		$('#portfolioColumn form #forward').val(1);
		$('#portfolioColumn form').submit();
		return false;
	});
	$('#linkWindow').jqm({
		modal: true,
		trigger: '#portfolioImage',
		target: '#jqmContent',
		onShow:  loadInIframeModal
	});

	$.beautyOfCode.init({
		brushes: ['Plain', 'Php', 'Bash', 'Xml' , 'JScript', 'Css', 'AS3'],
		defaults: { 'wrap-lines':true },
		ready: function() {
			$.beautyOfCode.beautifyAll();
		}
	});
	$('#changeMe')
			.keypress(showCursorMessage)
			.keyup(showCursorMessage);
	$('#changeMe2')
			.mouseover(function() {
				$.cursorMessage('hello', {hideTimeout:0})
			})
			.mouseout($.hideCursorMessage);
});

// use window.load for googlemap to load etc. /////////////////////////////////////////////////////
$(window).load(function() {
	if ($('#page_10921671 #content').length > 0) {
		var map, gdir, fromMarker;

		function handleErrors(){
			alert('Adres niet gevonden, probeer het nogmaals');
		}

		if (GBrowserIsCompatible()) {
			$('#content').prepend('<div id="map_canvas"></div><div id="directions"></div>');
			map = new GMap2(document.getElementById("map_canvas"));
			gdir = new GDirections(map, document.getElementById("directions"));
			GEvent.addListener(gdir, "error", handleErrors);

			var kingsquareIcon = new GIcon();
			kingsquareIcon.image = '/download/?id=12668227';
			kingsquareIcon.iconSize = new GSize(28, 32);
			kingsquareIcon.iconAnchor = new GPoint(6, 20);

			var markerOptions = { icon:kingsquareIcon };
			map.addOverlay(new GMarker( new GLatLng(52.479668,4.654716), markerOptions));
			map.setCenter(new GLatLng(52.479668,4.654716),8);
			map.addControl(new GSmallMapControl());

			$('#routePlanForm').submit(function() {
				gdir.load("from: " + $('#address').val() + ' to: Breestraat 2d, 1941 EJ Beverwijk', { "locale": 'nl_NL' , "getSteps":true});
				return false;
			});
		}

		$(window).unload(GUnload);
	}
});

Cufon.now();