/* 
	GENERAL JAVASCRIPT
	www.23g.nl
	by bubi blomer
	ver 0.3 | 14/08/2010
*/

$(document).ready(function() {
	
	// Internet Explorer 6 Message
	/*
if($.browser.msie && $.browser.version == 6){
		var ie6MsgCookie = $.cookieJar('ie6MsgCookie', {cookie: {path: '/', expires: 31}});
		//ie6MsgCookie.destroy();
		if(ie6MsgCookie.get('show') === "no"){
			//do nothing
		} else {
			$('#ie6Msg').fancybox({
				'frameHeight': 480,
				'overlayShow': true,
				'hideOnContentClick': false
			}).trigger('click');
			ie6MsgCookie.set('show', 'no');
		}
	}
*/

	//message depending on window.location.href
	var location = window.location.href;
	var msgFromPHP = new RegExp("message=");
	var msgErrorFromPHP = new RegExp("error=");
	var msgSucessFromPHP = new RegExp("success=");
	if(msgFromPHP.test(location)){
		flashMessage(unescape(RegExp.rightContext),'message');
	} else if(msgErrorFromPHP.test(location)){
		flashMessage(unescape(RegExp.rightContext),'error');
	} else if(msgSucessFromPHP.test(location)){
		flashMessage(unescape(RegExp.rightContext),'success');
	}

	/* !form validation defaults */
	if(jQuery.validator){
			
		// !run validation on each form
		$('form.validate').each(function() {
			var $this = $(this);
			$this.validate();
		});
	}
	
	//input text value magic ;)
	$('input[type="text"].magic, textarea.magic').each(function(){
		var $this = $(this);
		var currValue = $this.val();
		$this.blur(function() {
			if($this.val() === ""){
				$this.val(currValue);
			}
		}).focus(function() {
			if($this.val() === currValue){
				$this.val("");
			}
		});
	});
	
	$('a[rel="external"]').live('click', function(){
		$(this).attr('target','_blank');
		return true;
	});
	
	$.fn.equalizeHeights = function(){
		return this.height( Math.max.apply(this, $(this).map(function(i,e){ return $(e).height() }).get() ) )
	}

}); //end jQuery Ready

// usage: log('inside coolFunc',this,arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function(){
  log.history = log.history || [];   // store logs to an array for reference
  log.history.push(arguments);
  if(this.console){
    console.log( Array.prototype.slice.call(arguments) );
  }
};

// catch all document.write() calls
(function(doc){
  var write = doc.write;
  doc.write = function(q){ 
    log('document.write(): ',arguments); 
    if (/docwriteregexwhitelist/.test(q)) write.apply(doc,arguments);  
  };
})(document);

function doFadeMsg(msg,type) { // type['notice','error','success']
	var $container =  $('#flashMessageContainer');
	var msgContainer = $container.find('.msg');
	var $containerHeight = $container.outerHeight();
	var $windowHight = $(window).height();
	$container.addClass(type);
	msgContainer.html(msg);
	$container.css({'top':(($windowHight-$containerHeight)/2)});
	$container.fadeIn(1000).animate({opacity: 1.0}, 3000).delay(5000).fadeOut(1000);
	$container.click(function(){
		$(this).hide();
	});
}

function flashMessage(msg,type){ // type['notice','error','success']
	if(type === "message") {
		doFadeMsg(msg,type);
	} else if(type === "error") {
		doFadeMsg(msg,type);
	} else if(type === "success") {
		doFadeMsg(msg,type);
	}
}

function sortArryNumDesc(a,b) {
	return b - a;
}

Array.prototype.sum = function() {
  return (! this.length) ? 0 : this.slice(1).sum() +
      ((typeof this[0] == 'number') ? this[0] : 0);
};

