var vpro = vpro || {};
    vpro.version = "1.0";
    vpro.extend = function(base, extendWith){		
  		for(var p in extendWith){
  			if(extendWith.hasOwnProperty(p)){
  				base[p] = extendWith[p];
  			}
  		}
  	};
	 vpro.checkDependency = function(dep){
  		var chk = dep.split(".");
  		var parent = self;
  		var el=chk.length;
  		for(var e=0;e<el;e++){
  			if(typeof parent[chk[e]] != "undefined"){				
  				if(e==el-1){return true;}
  				parent = parent[chk[e]];
  			}else{
  				break;
  			}
  		}
  		alert("missing dependency: "+ dep);
  		return false;
	};
	 vpro.checkDependencies = function(deps){		
		if(deps.constructor == Array){			
			var cl = deps.length;
			for(var c=0;c<cl;c++){
				var d = deps[c];
				if(!this.checkDependency(d)){					
					return false;
				}
			}			
		}else if(deps.constructor == String){
			return this.checkDependency(deps);		
		}
		return true;
	};

vpro.utils = {

	padString : function(string, padd, upTo){
		var ret = string+"";
		while(ret.length < upTo){
			ret = padd + ret;
		}			
		return ret;			
	}
	
	,shortenStringMid : function(str, limit, glue){
		if(str.length > limit){
			glue = glue || '...';
			limit = Math.floor(limit/2);
			str = str.substring(0, limit-1) + glue + str.substring(str.length, str.length - limit);
		}
		return str;
	}
	
	/*currently couchdb format :: */
	,datestringToDate : function(datestring){
		var date = datestring.match(/(\d{4}-\d{2}-\d{2})T/i)[1];/*yyyy-mm-dd*/
			date = date.replace(/(\d{4})-(\d{2})-(\d{2})/, "$2/$3/$1");
		var time = datestring.match(/T(\d{2}:\d{2}:\d{2})\+/i)[1];/*hh:mm:ss*/
		var timezone = datestring.match(/.{6}$/i)[0];
		var ndatestring = date +" "+ time +" GMT"+ timezone.split(":").join("");
		var ndate = new Date();
			ndate.setTime(Date.parse(ndatestring));		
		return ndate;			
	}

	,dateToDatestring : function(date, returnGmt){
		
		returnGmt = (typeof returnGmt != "undefined")? returnGmt : true;
		var datestring = "";
		datestring += date.getFullYear()+"-"+
					(vpro.utils.padString(date.getMonth()+1, "0", 2))+"-"+
					(vpro.utils.padString(date.getDate(), "0", 2)) +"T"+
					(vpro.utils.padString(date.getHours(), "0", 2)) +":"+
					(vpro.utils.padString(date.getMinutes(), "0", 2)) +":"+
					(vpro.utils.padString(date.getSeconds(), "0", 2));
		
		if(returnGmt){
			var gmtOff = date.getTimezoneOffset();
			datestring += (gmtOff<0)? "+" : "-";
			var gmthours =  Math.floor(Math.abs(gmtOff) / 60);
			var gntmins = Math.abs(gmtOff) - (60*gmthours);

			datestring += vpro.utils.padString(gmthours, "0", 2)+":"+
							vpro.utils.padString(gntmins, "0", 2);
		}
		return datestring;
	}
	
	,onBodyResize : function(callback){		
		window.onresize = (function(){
			var cb = callback;
			return function(){
				cb({ innerHeight: vpro.utils.getInnerHeight(),
							innerWidth: vpro.utils.getInnerWidth()});
			};
		})();
	}
	
	,getInnerHeight : function(){
		if(self.innerHeight){
			return self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight)
		{
			return document.documentElement.clientHeight;
		}
		else if (document.body)
		{
			return document.body.clientHeight;
		}
		else return 0;
	}
	
	,getInnerWidth : function(){
		if(self.innerWidth){
			return self.innerWidth;
		}
		else if (document.documentElement && document.documentElement.clientWidth)
		{
			return document.documentElement.clientWidth;
		}
		else if (document.body)
		{
			return document.body.clientWidth;
		}
		else return 0;
	}
	
	,getQueryParams : function(){
		if(/^\?/.test(document.location.search)){
			var search = {};
			$(document.location.search.substr(1).split('&')).each(function(){
				var kv = this.split('=');
				search[kv[0]] = (kv.length == 2)? unescape(kv[1]) : "";
			});
			return search;
		}
		return {};
	}

	,getQueryParam : function(param){
		var qp = vpro.utils.getQueryParams();
		if(typeof qp[param] != "undefined"){
			return qp[param];
		}
		return "";
	}	
};	

vpro.utils.Clock = function(config){
		
	this.onSecondsChange = config.onSeconds || function(){};
	this.onMinutesChange = config.onMinutes || function(){};
	this.onHoursChange = config.onHours || function(){};
};
vpro.utils.Clock.prototype = {

	 interval : null

	,SECONDS : "seconds"
	,MINUTES : "minutes"
	,HOURS : "hours"
	
	,start : function(){
		var that = this;		
		this.stop();
		this.startDate = new Date();
		this.interval = setInterval(function(){			
			that.check.call(that);			
		}, 150);
	}
	
	,check : function(){
		var now = new Date();
		if( now.getSeconds() != this.startDate.getSeconds()){
			this.onSecondsChange(now);
		}
		if( now.getMinutes() != this.startDate.getMinutes()){
			this.onMinutesChange(now);
		}
		if( now.getHours() != this.startDate.getHours()){
			this.onHoursChange(now);
		}
		this.startDate = now;/* is last checked, certainly not 'now' anymore :) */
	}

	,stop : function(){
		if(typeof this.interval == "number"){
			clearInterval(this.interval);
		}		
	}
};
 
vpro.events = {
	 CLICKED : "click"
	,ACTIVATED : "activated"
	,DEACTIVATED : "deactivated"
	,OPENED : "opened"
	,CLOSED : "closed"
	,MOUSEOVER : "mover"
	,MOUSEOUT : "mout"
	,MOUSEENTER : "menter"
	,MOUSELEAVE : "mleave"	
	,MOUSEDOWN : "mdown"

};

vpro.events.EventDispatcher = {
	
	 dispatcher:null
	
	,initEventDispatcher : function(on){
		on.eventListeners = new Array();
		this.dispatcher = on;
	}

	,addEventListener : function(event, listener){		
		if(!this.dispatcherHasEventListener(event, listener)){
			this.eventListeners[event] = this.eventListeners[event] || new Array();
			this.eventListeners[event].push(listener);
			return true;
		}
		return false;
	}
	,dispatcherHasEvent : function(event){
		return (typeof this.eventListeners[event] != "undefined" && this.eventListeners[event].constructor == Array);
	}

	,dispatcherHasEventListener : function(event, listener){
		if(this.dispatcherHasEvent(event)){
			var els = this.eventListeners[event];
			var el = els.length;
			for(var e=0; e<el; e++){
				if(els[e] == listener){
					return true;
				}
			}
		}
		return false;
	}		
	,removeEventListener : function(){
		if(this.dispatcherHasEventListener(event, listener)){
			var els = this.eventListeners[event];
			var el = els.length;
			for(var e=0; e<el; e++){
				if(els[e] == listener){
					delete els[e];
					return true;
				}
			}
		}
		return false;
	}
	,dispatchEvent : function(event){
		if(this.dispatcherHasEvent(event)){
			var els = this.eventListeners[event];
			var el = els.length;
			var args = this.getEventArguments.apply(this, arguments);
			for(var e=0; e<el; e++){
				els[e].apply(els[e], [{
					target: this.dispatcher,
					data: args
				}]);						
			}			
		}
	}
	,getEventArguments : function(){
		var n = new Array();
		var al = arguments.length;
		if( al>1){
			for(var a=1;a<al;a++){
				n.push(arguments[a]);
			}
		}
		return n;
	}
};

/**
 * jQuery Cookie 
 */
	/**
	 * Get the value of a cookie with the given name.
	 *
	 * @example $.cookie('the_cookie');
	 * @desc Get the value of a cookie.
	 *
	 * @param String name The name of the cookie.
	 * @return The value of the cookie.
	 * @type String
	 *
	 * @name $.cookie
	 * @cat Plugins/Cookie
	 * @author Klaus Hartl/klaus.hartl@stilbuero.de
	 */
	jQuery.cookie = function(name, value, options) {
	    if (typeof value != 'undefined') { /* name and value given, set cookie */
	        options = options || {};
	        var expires = '';
	        if (options.expires && (typeof options.expires == 'number' || options.expires.toGMTString)) {
	            var date;				
	            if(options.expires.toGMTString){
					 date = options.expires;
				}else if(typeof options.expires == 'number') {
					date = new Date();
	                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
	            }
	            expires = '; expires=' + date.toGMTString(); /* use expires attribute, max-age is not supported by IE */
	        }
	        var path = options.path ? '; path=' + encodeURIComponent(options.path) : '';
	        var domain = options.domain ? '; domain=' + options.domain : '';
	        var secure = options.secure ? '; secure' : '';
	      	
			var cookiet = [name, '=', (options.unescaped)? value : encodeURIComponent(value), expires, path, domain, secure].join('');
		    document.cookie = cookiet;	

			
	    } else { /* only name given, get cookie */
	        var cookieValue = null;
	        if (document.cookie && document.cookie != '') {
				cookieValue = document.cookie;
				if(name){
					cookieValue = "";			
				    var cookies = document.cookie.split(';');
		            for (var i = 0; i < cookies.length; i++) {
		                var cookie = jQuery.trim(cookies[i]);
		                /* Does this cookie string begin with the name we want? */
		                if (cookie.substring(0, name.length + 1) == (name + '=')) {
							cookieValue = decodeURIComponent(cookie.substring(name.length + 1));							
		                    break;
		                }
		            }
					
				}
	        }
	        return cookieValue;
	    }
	};
