// Check status of jquery
if(typeof(jQuery)==="undefined"){
	var head= document.getElementsByTagName('head')[0];
  var script= document.createElement('script');
  script.type= 'text/javascript';
  script.src= 'http://code.jquery.com/jquery-1.4.2.min.js';
  head.appendChild(script);
}

// Initializiaton function if dublib
function init_dublib() {
	window.dublib = {
	  bruteClosest: function(element,selector) {
	    parent = $(element).parent();
      while (!parent.is(selector)) {
				console.log(parent);
          if(parent.is('html') || parent.length == 0){
              return false;
              break;
          }
          parent = parent.parent();
      }
      return parent;
	  },
		hasDupes: function (arr) {
			var i, j, n;
			n = arr.length;
			for (i=0; i<n; i++) {
				for (j=i+1; j<n; j++) {
					if (arr[i]==arr[j]) return true;
				}
			}
			return false;
		},
		collect: function() {
			if(arguments.length == 3){
				arr = arguments[0];
				f = arguments[1];
				key = arguments[2];
				var map = {};
			}else{
				arr = arguments[0];
				f = arguments[1];
				key = null;
				var map = [];
			}
		
			if (typeof(arr.jquery) !== 'undefined'){
				for ( var i = 0; i<arr.length; i++){
					val = f(arr.get(i));
					if(key!=null){ map[val[key]] = val; }else{ map[i] = val; }
				}
			}else{
				for ( var i in arr ) {
					val = f(arr[i]);
					if(key!=null){ map[val[key]] = val; }else{ map[i] = val; }
				}
			}
			return map;
		},
		pad: function (number,length) {
	    var str = "" + number;
	    while(str.length<length){
	       str = '0'+str;
	    }
	    return str;		
		},
		menuHighligher: function (selector,classname,parent_classname) {
			
			classname = typeof(classname) != 'undefined' ? classname : 'active';
			parent_classname = typeof(parent_classname) != 'undefined' ? parent_classname : 'parent';
			
			var parsed_page = dublib.parseUri(window.location);
			var str_page_url = window.location.toString();
			$(selector).find('a').each(function(){
				var link = $(this);
				var href = link.attr('href');
				var parsed_href = dublib.parseUri(href);
				var href_to_check = href;
				if(parsed_href.domain == ''){
					if(href[0] == '/'){
						href_to_check = parsed_page.protocol+"://"+parsed_page.domain+href
					}else{
						href_to_check = parsed_page.protocol+"://"+parsed_page.domain+parsed_page.directoryPath+href
					}
				}
			
				if(href_to_check == window.location){
					link.addClass(classname);
				}else{
					matches = str_page_url.match(href_to_check)
					if(matches != null){
						link.addClass(parent_classname);
					}
				}
				
				
			})
		},
		findPosition: function (obj) {
			var curleft = curtop = 0;
			if (obj.offsetParent) {
				do {
					curleft += obj.offsetLeft;
					curtop += obj.offsetTop;
				} while (obj = obj.offsetParent);
				return {x:curleft,y:curtop};
			}
		},
		/* parseUri JS v0.1, by Steven Levithan (http://badassery.blogspot.com)
		Splits any well-formed URI into the following parts (all are optional):
		----------------------
		• source (since the exec() method returns backreference 0 [i.e., the entire match] as key 0, we might as well use it)
		• protocol (scheme)
		• authority (includes both the domain and port)
		    • domain (part of the authority; can be an IP address)
		    • port (part of the authority)
		• path (includes both the directory path and filename)
		    • directoryPath (part of the path; supports directories with periods, and without a trailing backslash)
		    • fileName (part of the path)
		• query (does not include the leading question mark)
		• anchor (fragment)
		*/
		parseUri: function (sourceUri) {
			var uriPartNames = ["source","protocol","authority","domain","port","path","directoryPath","fileName","query","anchor"];
			var uriParts = new RegExp("^(?:([^:/?#.]+):)?(?://)?(([^:/?#]*)(?::(\\d*))?)?((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[\\?#]|$)))*/?)?([^?#/]*))?(?:\\?([^#]*))?(?:#(.*))?").exec(sourceUri);
			var uri = {};

			for(var i = 0; i < 10; i++){
				uri[uriPartNames[i]] = (uriParts[i] ? uriParts[i] : "");
			}

			// Always end directoryPath with a trailing backslash if a path was present in the source URI
			// Note that a trailing backslash is NOT automatically inserted within or appended to the "path" key
			if(uri.directoryPath.length > 0){
				uri.directoryPath = uri.directoryPath.replace(/\/?$/, "/");
			}

			return uri;
		}
	}
}

// Initialize dublib
( function($) { 
  $( function() {
		init_dublib();
  });
} ) (jQuery);
