var focus = true;
if(typeof(console) === 'undefined') {
    var console = {}
    console.log = console.error = console.info = console.debug = console.warn = console.trace = console.dir = console.dirxml = console.group = console.groupEnd = console.time = console.timeEnd = console.assert = console.profile = function() {};
}
//HOME PAGE CENTER DIV
(function($){
     $.fn.extend({
          centerAnimate: function () {
                return this.each(function() {
                        var top = ($(window).height() - $(this).outerHeight()) / 2;
                        var left = ($(window).width() - $(this).outerWidth()) / 2;
						$(this).css({position:'absolute', margin:0})
                        $(this).stop().animate({top: (top > 0 ? top : 0)+'px', left: (left > 0 ? left : 0)+'px'},600,"easeInOutCirc");
                });
        }
     });
})(jQuery);

(function($){
     $.fn.extend({
          center: function () {
                return this.each(function() {
                        var top = ($(window).height() - $(this).outerHeight()) / 2;
                        var left = ($(window).width() - $(this).outerWidth()) / 2;
                        $(this).css({position:'absolute', margin:0, top: (top > 0 ? top : 0)+'px', left: (left > 0 ? left : 0)+'px'});
                });
        }
     });
})(jQuery);
function resizeHome(){
	$('#homeWrapper').centerAnimate();
}

//Rotation crossbrowser
(function($) {
    function getTransformProperty(element) {
        var properties = ['transform', 'WebkitTransform',
                          'MozTransform', 'msTransform',
                          'OTransform'];
        var p;
        while (p = properties.shift()) {
            if (element.style[p] !== undefined) {
                return p;
            }
        }
        return false;
    }
    $.cssHooks['rotate'] = {
        get: function(elem, computed, extra){
            var property = getTransformProperty(elem);
            if (property) {
                return elem.style[property].replace(/.*rotate\((.*)deg\).*/, '$1');
            } else {
                return '';
            }
        },
        set: function(elem, value){
            var property = getTransformProperty(elem);
            if (property) {
                value = parseInt(value);
                $(elem).data('rotatation', value);
                if (value == 0) {
                    elem.style[property] = '';
                } else {
                    elem.style[property] = 'rotate(' + value%360 + 'deg)';
                }
            } else {
                return '';
            }
        }
    };
    $.fx.step['rotate'] = function(fx){
        $.cssHooks['rotate'].set(fx.elem, fx.now);
    };
})(jQuery);

//Preloader function
preload = (function () {
    var images = [];

    return function () {
        var args = Array.prototype.slice.call(arguments);

        while (args.length > 0) {
            images.unshift(new Image());
            images[0].src = args.shift();
        }
    }
}());
//Flow effect references
function fadeThemIn(tofade){
	var next = parseFloat(tofade)+1;
	$("#references div:nth-child("+tofade+")").fadeIn(400);
	if (tofade != 12) {
		timer = setTimeout('fadeThemIn(' + next + ')', 100);
	} else {
		$("#aboutText").fadeIn(400);
		//Kick in the actual flowing
		timer = setTimeout('startFlow()', 4000);
	}
}
function startFlow(){
	if(current >= parseFloat(logos.length)-1){
		current = -1;
	}
	var random = Math.round(Math.random()*12);
	$("#references div:nth-child("+random+")").find("img").fadeTo(500,0.001,function(){
		$(this).attr("src","/content/images/references/"+logos[current+=1]).fadeTo(500,1)
	});
	timer = setTimeout('startFlow()', 4000);
}

function polaroidFlow(num){
	var randomRot = ((Math.random()*20)-10)+360;
	$(".polaroid:eq("+num+")").animate({opacity:1,rotate:randomRot},200,function(){
	});
	if($(".polaroid").size() != num-1){
		timer = setTimeout('polaroidFlow('+(num+1)+')', 100);
	}

}

function referenceFlow(num){
	var randomRot = ((Math.random()*20)-10)+360;
	$("#referencesWrapper UL LI:eq("+num+")").animate({opacity:1,rotate:randomRot},200,function(){
	});
	if($("#referencesWrapper UL LI").size() != num-1){
		timer = setTimeout('referenceFlow('+(num+1)+')', 100);
	} else {
		$("#pagination").load('/ajax/referencesPaging.cfm?type1=' + type[0] + '&type2=' + type[1] + '&type3=' + type[2] + '&type4=' + type[3] + '&page=' + page, paginationSetup).fadeIn();
	}

}

function polaroid_click(){
	$(this).addClass("rot");
	var randomRot = ((Math.random()*20)-10);
	$(this).animate({rotate:-360},200,function(){
		$(this).find(".close").unbind("click");
		$(this).find('.polaroid_image').hide();
		$(this).find('.polaroid_caption').hide();
		$(this).find('.polaroid_hover').hide();
		$(this).find(".polaroid_click").fadeIn();
		$(this).css("backgroundImage", "url(/images/BG-polaroid-grunge.png)");
		$(this).animate({rotate:randomRot},200);
		$(this).unbind("click").css("cursor","inherit");
		var pola = $(this);
		$(this).find(".close").click(function(){
			pola.animate({rotate:-360},200,function(){
				pola.find(".polaroid_click").hide();
				pola.find('.polaroid_image').fadeIn();
				pola.find('.polaroid_caption').fadeIn();
				pola.animate({rotate:randomRot},200);
				pola.css("backgroundImage", "url(/images/BG-polaroid.png)");
				pola.css("cursor","pointer");
				pola.removeClass("rot");
				pola.click(polaroid_click);
			});
		})
	});
}

//Setup pagination bindings
function paginationSetup(){

	$("#referencesWrapper #nextArrow").remove();
	$("#referencesWrapper #prevArrow").remove();
	if($("#pagination #nextPage").size()>0){
		$("#referencesWrapper").prepend('<div id="nextArrow" data-page="'+$("#pagination #nextPage").data("page")+'"><img src="/images/nextSlide.png" /></div>');
		$("#referencesWrapper").find("#nextArrow").click(function(){
			page = $(this).attr("data-page");
			$("#pagination").load('/ajax/referencesPaging.cfm?type1=' + type[0] + '&type2=' + type[1] + '&type3=' + type[2] + '&type4=' + type[3] + '&page=' + page, paginationSetup).fadeIn();
			$.get('/ajax/referencesState.cfm?type1=' + type[0] + '&type2=' + type[1] + '&type3=' + type[2] + '&type4=' + type[3] + '&page=' + page );
			$.get('/ajax/referencesFilter.cfm?type1=' + type[0] + '&type2=' + type[1] + '&type3=' + type[2] + '&type4=' + type[3] + '&page=' + page, function(data){
				$('#referencesWrapper UL').quicksand($(data).find('li'), {duration:0}, function(){
					$("#referencesWrapper UL LI").each(function(){
						var randomRot = ((Math.random() * 20) - 10);
						$(this).animate({
							rotate: randomRot
						}, 300);
						$(this).click(function(){
							var screenside = ($(window).width()/2)+480;
							var reference = $(this).attr("data-id");
							$("section").animate({marginLeft:-screenside,opacity:0},500,"easeInBack",function(){
								document.location = "/"+lang+"/reference/"+reference;
							});
							return false;
						});
					})
				})
			})
		})
	}
	if($("#pagination #prevPage").size()>0){
		$("#referencesWrapper").prepend('<div id="prevArrow" data-page="'+$("#pagination #prevPage").data("page")+'"><img src="/images/prevSlide.png" /></div>');
		$("#referencesWrapper").find("#prevArrow").click(function(){
			page = $(this).attr("data-page");
			$("#pagination").load('/ajax/referencesPaging.cfm?type1=' + type[0] + '&type2=' + type[1] + '&type3=' + type[2] + '&type4=' + type[3] + '&page=' + page, paginationSetup).fadeIn();
			$.get('/ajax/referencesState.cfm?type1=' + type[0] + '&type2=' + type[1] + '&type3=' + type[2] + '&type4=' + type[3] + '&page=' + page );
			$.get('/ajax/referencesFilter.cfm?type1=' + type[0] + '&type2=' + type[1] + '&type3=' + type[2] + '&type4=' + type[3] + '&page=' + page, function(data){
				$('#referencesWrapper UL').quicksand($(data).find('li'), {duration:0}, function(){
					$("#referencesWrapper UL LI").each(function(){
						var randomRot = ((Math.random() * 20) - 10);
						$(this).animate({
							rotate: randomRot
						}, 300);
						$(this).click(function(){
							var screenside = ($(window).width()/2)+480;
							var reference = $(this).attr("data-id");
							$("section").animate({marginLeft:-screenside,opacity:0},500,"easeInBack",function(){
								document.location = "/"+lang+"/reference/"+reference;
							});
							return false;
						});
					})
				})
			})
		})
	}

	$("#pagination div").each(function(){
		$(this).unbind("click");
		$(this).click(function(){
			if (!$(this).hasClass("active")) {
				page = $(this).attr("data-page");
				$("#pagination").load('/ajax/referencesPaging.cfm?type1=' + type[0] + '&type2=' + type[1] + '&type3=' + type[2] + '&type4=' + type[3] + '&page=' + page, paginationSetup).fadeIn();
				$.get('/ajax/referencesState.cfm?type1=' + type[0] + '&type2=' + type[1] + '&type3=' + type[2] + '&type4=' + type[3] + '&page=' + page );
				$.get('/ajax/referencesFilter.cfm?type1=' + type[0] + '&type2=' + type[1] + '&type3=' + type[2] + '&type4=' + type[3] + '&page=' + page, function(data){
					$('#referencesWrapper UL').quicksand($(data).find('li'), {duration:0}, function(){
						$("#referencesWrapper UL LI").each(function(){
							var randomRot = ((Math.random() * 20) - 10);
							$(this).animate({
								rotate: randomRot
							}, 300);
							$(this).click(function(){
								var screenside = ($(window).width()/2)+480;
								var reference = $(this).attr("data-id");
								$("section").animate({marginLeft:-screenside,opacity:0},500,"easeInBack",function(){
									document.location = "/"+lang+"/reference/"+reference;
								});
								return false;
							});
						})
					})
				})
			}
		})
	});
}
var logos  = [];
var current = 12;
var timer;
var page = 1;
var lang = "nl";
var type = [1,1,1,1]

$.getJSON('/ajax/referencesStateFetch.cfm',function(data){
	type = [data[0],data[1],data[2],data[3]];
	page = data[4];
	lang = data[5];
});


///DOCUMENT READY//////////////DOCUMENT READY///////////DOCUMENT READY///////////DOCUMENT READY///////////DOCUMENT READY///
///DOCUMENT READY//////////////DOCUMENT READY///////////DOCUMENT READY///////////DOCUMENT READY///////////DOCUMENT READY///
///DOCUMENT READY//////////////DOCUMENT READY///////////DOCUMENT READY///////////DOCUMENT READY///////////DOCUMENT READY///
///DOCUMENT READY//////////////DOCUMENT READY///////////DOCUMENT READY///////////DOCUMENT READY///////////DOCUMENT READY///
///DOCUMENT READY//////////////DOCUMENT READY///////////DOCUMENT READY///////////DOCUMENT READY///////////DOCUMENT READY///
///DOCUMENT READY//////////////DOCUMENT READY///////////DOCUMENT READY///////////DOCUMENT READY///////////DOCUMENT READY///
Cufon.replace(".sticky");
$(document).ready(function(){
	
	function onBlur() {
    	focus = false;
	};
	function onFocus(){
	   	focus = true;
	};
	
	if (/*@cc_on!@*/false) { // check for Internet Explorer
	    document.onfocusin = onFocus;
	    document.onfocusout = onBlur;
	} else {
	    window.onfocus = onFocus;
	    window.onblur = onBlur;
	}
	//Stickynote
	Cufon.now();
	preload("/images/postitHover.png","/images/postitClose.png");
	$(".sticky").each(function(){
		var stickyRot = Math.floor((Math.random()*20)-10)
		var stickyX = Math.floor((Math.random()*($(window).width()-233)));
		var stickyY = Math.floor((Math.random()*($(window).height()-233)));
		if($(this).find(".link").size() > 0){
			$(this).find("p").click(function(){
				window.location = $(this).parent().find(".link").attr("href");
			})
		}
		$(this).draggable({containment:'window',stack: ".sticky"}).css("rotate",stickyRot).css("top",stickyY).css("left",stickyX).show();
		$(this)
		.hover(function(){$(this).css("background-image","url(/images/postitHover.png)");},function(){$(this).css("background-image","url(/images/postit.png)");})
		$(this).find(".close")
		.hover(function(){$(this).find("img").attr("src","/images/postitCloseHover.png");},function(){$(this).find("img").attr("src","/images/postitClose.png");})
		.click(function(){
			$(this).parent().fadeOut();
			$.get("/ajax/stickyClose.cfm?id="+$(this).parent().data("id"));
			return false;
		});
	});
	
	
	//Reference overview session reset controller
	$("#referencesLink").click(function(){
		$.get("/ajax/kill.cfm")
	})
	//Reference Detail Behaviour
	var currentSlide = 0;
	var totalSlides = $("#slideList LI").length;
	
	//Bind the buttons
	$("#next").click(nextSlide);
	$("#prev").click(prevSlide);
	
	function nextSlide(){
		if(currentSlide>(totalSlides-2))
		{
			currentSlide = 0;	
		} else {
			currentSlide++
		}
		goTo(currentSlide);
	}
	function prevSlide(){
		if(currentSlide==0)
		{
			currentSlide = totalSlides-1;	
		} else {
			currentSlide--
		}
		goTo(currentSlide);
	}
	
	function goTo(current){
		currentSlide = current;
		loadImg($("#slideList LI:eq("+current+")").html());
		$("#slidePaging LI").removeClass("active");
		$("#slidePaging LI[data-slide="+(current+1)+"]").addClass("active");
	}
	function loadImg(html){
		if($("#slideContainer .slide").length>1){
			$("#slideContainer .slide:eq(0)").remove();
		}
		$("#slideContainer").append(html).find(".slide:last-child").hide().fadeIn();
		return false;
	}
	$("#slidePaging LI").each(function(){
		$(this).click(function(){
			goTo($(this).attr("data-slide")-1);
		})
	});
	$("#prev").fadeTo(300,0.2);
	$("#next").fadeTo(300,0.2);
	$("#slideWrap").hover(function(){
		$("#prev").fadeTo(300,1);
		$("#next").fadeTo(300,1);
	},function(){
		$("#prev").fadeTo(300,0.2);
		$("#next").fadeTo(300,0.2);
	});
	//Reference Behaviour
	//Filter Controls
	$("#referencesFilters li").each(function(){
		$(this).disableSelection();
		if ($(this).attr("data-state") == 0) {
			$(this).fadeTo(0,0.3);
			type[$(this).attr("data-type")-1] = 0;
		}
		
		$(this).click(function(){
			if($(this).attr("data-state")==1){
				$(this).fadeTo(300,0.3);
				$(this).attr("data-state","0");
				type[$(this).attr("data-type")-1] = 0;
				$("#pagination").hide().load('/ajax/referencesPaging.cfm?type1='+type[0]+'&type2='+type[1]+'&type3='+type[2]+'&type4='+type[3]+'&page=1',paginationSetup).fadeIn();
				$.get('/ajax/referencesState.cfm?type1=' + type[0] + '&type2=' + type[1] + '&type3=' + type[2] + '&type4=' + type[3] + '&page=1' );
				$.get('/ajax/referencesFilter.cfm?type1='+type[0]+'&type2='+type[1]+'&type3='+type[2]+'&type4='+type[3]+'&page=1',function(data){
					$('#referencesWrapper UL').quicksand( $(data).find('li'), { adjustHeight: 'false', useScaling: 'false'}, function(){
						$("#referencesWrapper UL LI").each(function(){
							var randomRot = ((Math.random()*20)-10);
							$(this).animate({rotate:randomRot},300);
							$(this).click(function(){
								var screenside = ($(window).width()/2)+480;
								var reference = $(this).attr("data-id");
								$("section").animate({marginLeft:-screenside,opacity:0},500,"easeInBack",function(){
									document.location = "/"+lang+"/reference/"+reference;
								});
								return false;
							});
						})
					});
				});
			} else {
				$(this).fadeTo(300,1);
				$(this).attr("data-state","1");
				type[$(this).attr("data-type")-1] = 1;
				$("#pagination").hide().load('/ajax/referencesPaging.cfm?type1='+type[0]+'&type2='+type[1]+'&type3='+type[2]+'&type4='+type[3]+'&page=1',paginationSetup).fadeIn();
				$.get('/ajax/referencesState.cfm?type1=' + type[0] + '&type2=' + type[1] + '&type3=' + type[2] + '&type4=' + type[3] + '&page=1');
				$.get('/ajax/referencesFilter.cfm?type1='+type[0]+'&type2='+type[1]+'&type3='+type[2]+'&type4='+type[3]+'&page=1',function(data){
					$('#referencesWrapper UL').quicksand( $(data).find('li'), { adjustHeight: 'false', useScaling: 'false'}, function(){
						$("#referencesWrapper UL LI").each(function(){
							var randomRot = ((Math.random()*20)-10);
							$(this).animate({rotate:randomRot},300);
							$(this).click(function(){
								var screenside = ($(window).width()/2)+480;
								var reference = $(this).attr("data-id");
								$("section").animate({marginLeft:-screenside,opacity:0},500,"easeInBack",function(){
									document.location = "/"+lang+"/reference/"+reference;
								});
								return false;
							});
						})
						
					});
				});
			}
			
		});
	})
	//Paging Controls

	$("#referencesWrapper UL LI").each(function(){
		$(this).click(function(){
			var screenside = ($(window).width()/2)+480;
			var reference = $(this).attr("data-id");
			$("section").animate({marginLeft:-screenside,opacity:0},500,"easeInBack",function(){
				document.location = "/"+lang+"/reference/"+reference;
			});
			return false;
		});
		$(this).css("opacity",0);
	});
	referenceFlow(0);
	//Polaroid Behaviour
	$(".polaroid").each(function(){
		$(this).css("opacity",0);
		$(this).hover(function(){
			if (!$(this).hasClass('rot')) {
				$(this).find('.polaroid_hover').show();
				$(this).find(".polaroid_caption").hide();
				$(this).css("backgroundImage", "none");
			}
		},function(){
			if (!$(this).hasClass('rot')) {
				$(this).find('.polaroid_caption').show();
				$(this).find(".polaroid_hover").hide();
				$(this).css("backgroundImage", "url(/images/BG-polaroid.png)");
			}
		});
		$(this).click(polaroid_click);
	});
	polaroidFlow(0);
	//Link click behaviour
	$("nav a").click(function(){
	var screenside = ($(window).width()/2)+480;
	var link = $(this).attr("href");
	$("section").animate({marginLeft:-screenside,opacity:0},500,"easeInBack",function(){
		document.location = link;
	});
		return false;
	})
	$('#homeWrapper').hide();
	//do some hacking for the crossbrawser resize event
	 var TO = false;
	$(window).resize(function(){
	 if(TO !== false)
	    clearTimeout(TO);
	 TO = setTimeout(resizeHome, 200); //200 is time in miliseconds
	});
	
	//SET UP HOVERS ON LANGUAGE LINKS
	preload("/images/lang_fr_hover.png","/images/lang_nl_hover.png","/images/lang_en_hover.png");
	$("#nlSelect").hover(function(){
		$(this).find("img").attr("src","/images/lang_nl_hover.png");
	},function(){
		$(this).find("img").attr("src","/images/lang_nl.png");
	});
	$("#enSelect").hover(function(){
		$(this).find("img").attr("src","/images/lang_en_hover.png");
	},function(){
		$(this).find("img").attr("src","/images/lang_en.png");
	});
	$("#frSelect").hover(function(){
		$(this).find("img").attr("src","/images/lang_fr_hover.png");
	},function(){
		$(this).find("img").attr("src","/images/lang_fr.png");
	});
	$("#langSelect a").click(function(){
		var link = $(this);
		$('#homeWrapper').animate({
			top:"-500px"
		},500,"easeInBack");
		$('#overlay').fadeIn(500,function(){
			window.location=$(link).attr("href");
		});
		return false;
	})
	//REFERENCE LOGOFLOW
	if ($("#references").html()) {
		$("#aboutText").hide();
		var references = {}
		$.ajax({
			url: "/ajax/references.cfm",
			success: function(data){
				data = $.parseJSON(data);
				logos = data;
				for(var i = 1 ; i <=12 ; i++){
					$("#references div:nth-child("+i+")").html('<img src="/content/images/references/'+data[i]+'" />').hide();
				}
				fadeThemIn(1);
			}
		})
	}
	
	//TEAM FILTERS
	$("#teamFilter ul li a").each(function(){
		$(this).disableSelection();
		$(this).click(function(){
			$("#teamFilter ul li a").css("backgroundColor","#EE6716");
			$(this).css("backgroundColor","white");
			if($(this).is(".active")){
				var button = $(this);
				$("#teamWrapper .teamMember").each(function(){
					$(this).show();
					button.css("backgroundColor","#EE6716");
					button.removeClass('active');
				})
			}else{
				$(this).addClass('active');
				$("#teamFilter ul li a").not(this).removeClass("active")
				var pics = $(this).data("type");
				var button = $(this);
				$("#teamWrapper .teamMember").each(function(){
					if($(this).data("type") != pics){
						$(this).hide();
					} else {
						$(this).hide().fadeIn();
					}
				});	
			}
			return false;
		})
	})
});

//WINDOW LOAD EVENT
$(window).load(function(){
	$('#homeWrapper').center().fadeIn(300);
});

