/*
	check for the console object
*/

try { console.log('init:	The console object is present.'); } catch(e) { console = { log: function() {} } };

/*
	extend jQuery
*/

$.extend($.easing, {
	easeInExpo: function (x, t, b, c, d) {
			return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;

	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;

	},
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;

	}
});


/*!
 * jQuery viewportOffset - v0.3 - 2/3/2010
 * http://benalman.com/projects/jquery-misc-plugins/
 *
 * Copyright (c) 2010 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */

// Like the built-in jQuery .offset() method, but calculates left and top from
// the element's position relative to the viewport, not the document.

(function($){
  '$:nomunge'; // Used by YUI compressor.

  var win = $(window);

  $.fn.viewportOffset = function() {
	var offset = $(this).offset();

	return {
	  left: offset.left - win.scrollLeft(),
	  top: offset.top - win.scrollTop()
	};
  };

})(jQuery);


/*
	jQuery limit plugin
	This adds a limit(int) method to input and textarea fields

	Handy!
*/

(function($){
     $.fn.extend({
         limit: function(limit,element) {

			var interval, f;
			var self = $(this);

			$(this).focus(function(){
				interval = window.setInterval(substring,100);
			});

			$(this).blur(function(){
				clearInterval(interval);
				substring();
			});

			substringFunction = "function substring(){ var val = $(self).val();var length = val.length;if(length > limit){$(self).val($(self).val().substring(0,limit));}";
			if(typeof element != 'undefined')
				substringFunction += "if($(element).html() != limit-length){$(element).html((limit-length<=0)?'0':limit-length);}"

			substringFunction += "}";

			eval(substringFunction);



			substring();

        }
    });
})(jQuery);

/*
	textMetrics method. handy!
	syntax:  $.textMetrics(($(elem)) -> returns object containing width & height
*/

(function($) {

 $.textMetrics = function(el) {

  var h = 0, w = 0;

  var div = document.createElement('div');
  document.body.appendChild(div);
  $(div).css({
   position: 'absolute',
   left: -1000,
   top: -1000,
   display: 'none'
  });

  $(div).html($(el).html());
  var styles = ['font-size','font-style', 'font-weight', 'font-family','line-height', 'text-transform', 'letter-spacing'];
  $(styles).each(function() {
   var s = this.toString();
   $(div).css({
	s: $(el).css(s)
   });
  });

  h = $(div).outerHeight();
  w = $(div).outerWidth();

  $(div).remove();

  var ret = {
   height: h,
   width: w
  };

  return ret;
 }

})($);




if(typeof(onMDVWidgetsReadyStack) == 'undefined') {
	window.onMDVWidgetsReadyStack = [];
}




/*
	create website.home obj
*/

var website = {};

$(document).ready(function() {
	var $mainMenu = $('#mainMenu');

	if($mainMenu.length > 0)
		var menu = new Menu($mainMenu);

	$('form').jqTransform({
		button: false,
		text: false,
		textarea: false
	});

	var $serviceMenu = $('#serviceMenu');
	if($serviceMenu.length > 0) {
		website.serviceMenu = new mdv.servicemenu.ServiceMenu($serviceMenu);
	}

	var $personalHeader = $('#personalHeader');
	if($personalHeader.length > 0) {
		website.personalHeader = new mdv.personalheader.PersonalHeader($personalHeader, 'website.personalHeader');

	}

	var $footer = $('#footer');
	if($footer.length > 0) {
		website.footer = new mdv.footer.Footer($footer);
	}
});


var useFallbackMenu = function(sElement, nActiveElement, sActiveColor, sInactiveColor) {

	// dom updates
	$(sElement).append('<div id="fallback-menu" style="margin-top: 20px; margin-left: 29px; font-family: MetaBlack,Arial,sans-serif; font-size: 48px; line-height: 48px; text-transform: uppercase; -moz-transform: rotate(-5deg) skew(-6deg); -webkit-transform: rotate(-5deg) skew(-5deg); -o-transform: rotate(-5deg) skew(-6deg); -ms-transform: rotate(-5deg) skew(-6deg);"></div>');

	if (nActiveElement == 0) {
		$(sElement + " > #fallback-menu").append('<div class="home" style="cursor: pointer; color: '+sActiveColor+';">home</div>');
	} else {
		$(sElement + " > #fallback-menu").append('<div class="home" style="cursor: pointer; color: '+sInactiveColor+';">home</div>');
	}

	if (nActiveElement == 1) {
		$(sElement + " > #fallback-menu").append('<div class="activiteiten" style="margin-top: -11px; color: '+sActiveColor+'; cursor: pointer;">activiteiten</div>');
	} else {
		$(sElement + " > #fallback-menu").append('<div class="activiteiten" style="margin-top: -11px; color: '+sInactiveColor+'; cursor: pointer;">activiteiten</div>');
	}

	if (nActiveElement == 2) {
		$(sElement + " > #fallback-menu").append('<div class="aandeslag" style="margin-top: -11px; color: '+sActiveColor+'; cursor: pointer;">aan de slag</div>');
	} else {
		$(sElement + " > #fallback-menu").append('<div class="aandeslag" style="margin-top: -11px; color: '+sInactiveColor+'; cursor: pointer;">aan de slag</div>');
	}

	if (nActiveElement == 3) {
		$(sElement + " > #fallback-menu").append('<div class="inzenden" style="margin-top: -11px; color: '+sActiveColor+'; cursor: pointer;">inzenden</div>');
	} else {
		$(sElement + " > #fallback-menu").append('<div class="inzenden" style="margin-top: -11px; color: '+sInactiveColor+'; cursor: pointer;">inzenden</div>');
	}

	if (nActiveElement == 4) {
		$(sElement + " > #fallback-menu").append('<div class="meerdanvoetballers" style="margin-top: 10px; font-size: 29px; color: '+sActiveColor+'; line-height: 29px; cursor: pointer;">meer dan voetballers</div>');
	} else {
		$(sElement + " > #fallback-menu").append('<div class="meerdanvoetballers" style="margin-top: 10px; font-size: 29px; color: '+sInactiveColor+'; line-height: 29px; cursor: pointer;">meer dan voetballers</div>');
	}

	if (nActiveElement == 5) {
		$(sElement + " > #fallback-menu").append('<div class="motto" style="margin-top: -2px; font-size: 29px; color: '+sActiveColor+'; line-height: 29px; cursor: pointer;">maak je eigen motto</div>');
	} else {
		$(sElement + " > #fallback-menu").append('<div class="motto" style="margin-top: -2px; font-size: 29px; color: '+sInactiveColor+'; line-height: 29px; cursor: pointer;">maak je eigen motto</div>');
	}

	if (nActiveElement == 6) {
		$(sElement + " > #fallback-menu").append('<div class="nieuws" style="margin-top: -2px; font-size: 29px; color: '+sActiveColor+'; line-height: 29px; cursor: pointer;">nieuws</div>');
	} else {
		$(sElement + " > #fallback-menu").append('<div class="nieuws" style="margin-top: -2px; font-size: 29px; color: '+sInactiveColor+'; line-height: 29px; cursor: pointer;">nieuws</div>');
	}

	if (nActiveElement == 7) {
		$(sElement + " > #fallback-menu").append('<div class="clubs" style="margin-top: -2px; font-size: 29px; color: '+sActiveColor+'; line-height: 29px; cursor: pointer;">clubs</div>');
	} else {
		$(sElement + " > #fallback-menu").append('<div class="clubs" style="margin-top: -2px; font-size: 29px; color: '+sInactiveColor+'; line-height: 29px; cursor: pointer;">clubs</div>');
	}

	// events
	$(sElement + " > #fallback-menu > .home," +
	sElement + " > #fallback-menu > .activiteiten," +
	sElement + " > #fallback-menu > .aandeslag," +
	sElement + " > #fallback-menu > .inzenden," +
	sElement + " > #fallback-menu > .meerdanvoetballers," +
	sElement + " > #fallback-menu > .motto," +
    sElement + " > #fallback-menu > .nieuws," +
	sElement + " > #fallback-menu > .clubs" +
	"").live('mouseenter', function() {

		$(this).animate({
			'margin-left' : 20
		}, { 'queue' : false, 'duration' : 350, 'easing' : 'easeOutExpo' });
	});

	$(sElement + " > #fallback-menu > .home," +
	sElement + " > #fallback-menu > .activiteiten," +
	sElement + " > #fallback-menu > .aandeslag," +
	sElement + " > #fallback-menu > .inzenden," +
	sElement + " > #fallback-menu > .meerdanvoetballers," +
    sElement + " > #fallback-menu > .motto," +
	sElement + " > #fallback-menu > .nieuws," +
	sElement + " > #fallback-menu > .clubs" +
	"").live('mouseleave', function() {

		$(this).animate({
			'margin-left' : 0
		}, { 'queue' : false, 'duration' : 350, 'easing' : 'easeOutExpo' });
	});

	$(sElement + " > #fallback-menu > .home," +
	sElement + " > #fallback-menu > .activiteiten," +
	sElement + " > #fallback-menu > .aandeslag," +
	sElement + " > #fallback-menu > .inzenden," +
	sElement + " > #fallback-menu > .meerdanvoetballers," +
    sElement + " > #fallback-menu > .motto," +
	sElement + " > #fallback-menu > .nieuws," +
	sElement + " > #fallback-menu > .clubs" +
	"").live('click', function() {

		switch($(this).attr('class')) {

			case "home" :
				window.location = 'http://' + window.location.host;
				break;

			case "activiteiten" :
				window.location = 'http://' + window.location.host + '/activiteiten';
				break;

			case "aandeslag" :
				window.location = 'http://' + window.location.host + '/aandeslag';
				break;

			case "inzenden" :
				window.location = 'http://' + window.location.host + '/inzenden';
				break;

			case "meerdanvoetballers" :
				window.location = 'http://' + window.location.host + '/mdvers';
				break;

			case "motto" :
				window.location = 'http://' + window.location.host + '/motto';
				break;

			case "nieuws" :
				window.location = 'http://' + window.location.host + '/nieuws';
				break;

			case "clubs" :
				window.location = 'http://' + window.location.host + '/clubs';
				break;
		}
	});
}

/*
 * GWT stuff
 */
var widgetsReady = false;



function OnGwtReady(stack) {
	this.stack = stack;

	this.push = function(f) {
		exec(f);
	}

	this.process = function() {
		for(f in this.stack) {
			exec(f);
		}
	}

	function exec(fnName) {
		if(window.readyFunctions[fnName])
				window.readyFunctions[fnName]();
	}

	this.process();
};

var readyFunctions = {
	WidgetsReady: function() {
		window.widgetsReady = true;

		var calendarCssClass = 'calendarWidget'

		var calendarBoxes = $('.'+calendarCssClass);
		for(var i = 0; i < calendarBoxes.length; i++) {
			var calendarWidget = new Calendar(calendarBoxes.get(i));

			var extraStyles = $(calendarBoxes.get(i)).attr('class').split(' ');
			for(var i = 0; i < extraStyles.length; i++) {
				if(extraStyles[i].length > 0 && extraStyles[i] != calendarCssClass) {
					calendarWidget.addStyle(extraStyles[i]);
					console.log(extraStyles[i]);
				}
			}
		}

		var commentBoxes = $('.comments');
		if(commentBoxes.length > 0) {
			if(typeof(window.commentsData) == 'undefined') {
				window.commentsData = [];
			}

			new Comments(commentBoxes.get(0), window.commentsData, mdvSettings.loggedIn);
		}

		$('.jLoginBtn').bind('click', function(event) {
			event.preventDefault();

			var popUp = new LoginPopUp();
			var href = $(this).attr('href');
			// Only redirect links starting with a slash. This is a lazy way to ignore '#' and 'javascript:void' links
			if(href && href.charAt(0) == "/") {
				popUp.setRedirectUrl(href);
			}

			popUp.show();
		});


		$('.btnContact').live('click', function(event) {
			var $this = $(this);

			var id = $this.attr('m:id');
			var name = $this.attr('m:name');

			if(id && name) {
				event.preventDefault();
				var popUp = new ContactPopUp(id, name);
				popUp.show();
			}
		});
	}
};

if(window.onGwtReady == undefined)
	window.onGwtReady = [];

window.onGwtReady = new OnGwtReady(window.onGwtReady);




$(document).ready(function() {
	var lists = $('.wAjaxList');

	for(var i = 0; i < lists.length; i++) {
		new AjaxList(lists[i]);
	}
	
	//
	
	var twitterFountains = $('.jTwitterFountain');
	
	for(var i = 0; i < twitterFountains.length; i++) {
		new TwitterFountain(twitterFountains[i]);
	}
});
