var $$ = {
	hash: {},
  load: function() {
		$(document).ready(function() {
			$$.init();
		});
		$(window).load(function () {
		});
	},

	init: function(){
		$("img.size-medium, img.attachment-thumbnail, img.size-large, img.size-thumbnail").parent("a").attr("rel","prettyPhoto[gallery]").prettyPhoto({theme:'facebook'});

		$(".init").each(function(){
			classList = this.className.split(' ');
			for (var key in classList) {
				var val = classList [key];
				if(val != 'init' && $$.i[val] != undefined) $$.i[val](this);
			}
		});
		this.initForms();

	},


	initForms: function(){
		// Default form values
		var submitForm = function(event){
			$("input.default", event.data.form).each(function(){
				if(this.value == this.title)	this.value='';
			});
			return true;
		}

		$("input.default").each(function(){
			//console.log(this.className);
			this.title = this.value;
			var el = this;
			$(el).blur(function () {
				if(this.value == '') {
					$(this).removeClass('active');
					this.value = el.title;
				}
	    }).focus(function () {
				if(this.value == el.title) {
					this.value = '';
				}
				$(this).addClass('active');
			});
			if(this.value == '') this.value = el.title;
			if(this.value != el.title)$(el).addClass('active');

			// remove default values before submission
			var form = $($(el).parents("form"));
			form.unbind("submit", submitForm).bind("submit", {form: form}, submitForm);
		});
	},

	// Lazy Script Loading

	onScriptLoad: function(url) {
		var script = this.hash[url];
		script.loaded = true;
		for (var i = 0, len = script.callbacks.length; i < len; i++){
			var callback = script.callbacks[i];
			if (typeof callback != 'undefined') callback();
		}
	},

	require: function(url, callback, type){

		//Check if script has already been added to the loader
		if (this.hash[url] != undefined){
			if (this.hash[url].loaded){ //File loaded
				//Run callback straight away
				if (typeof callback != 'undefined') callback();
			}
			else {//Still loading
				//Add callback to list for running later
				this.hash[url].callbacks.push(callback);
			}
			//Script already requested so exit here
			return;
		}

		//Create tracker for this script to monitor status and build a list of callbacks
		this.hash[url] = {loaded: false, callbacks: [callback]};

		//Add script element to DOM and add onload handlers for callbacks
		var script = document.createElement("script")
		script.type = "text/javascript";

		if (script.readyState){ //IE
			script.onreadystatechange = function(){
				if (script.readyState == "loaded" ||
				script.readyState == "complete"){
					script.onreadystatechange = null;
					$$.onScriptLoad(url);
				}
			};
		}
		else {//Other browsers
			script.onload = function(event){
				$$.onScriptLoad(url);
			};
		}
		var path = $('script[src*="global.js"]').attr('src').replace("global.js", "");
		script.src = path + url.replace(/\./, '/') + '.js?v=3';
    (document.head||document.getElementsByTagName('head')[0]).appendChild(script);
	},
	
	i: {
		'index-links': function(el){ // Init banner
			//var ul=$(el);
			
		}
	}
};

$$.load();

var kkeys = [], konami = "38,38,40,40,37,39,37,39";
$(document).keydown(function(e) {
  kkeys.push( e.keyCode );
  if ( kkeys.toString().indexOf( konami ) >= 0 ){
    $(document).unbind('keydown',arguments.callee);
    $.getScript('http://www.cornify.com/js/cornify.js',function(){
      cornify_add();
      $(document).keydown(cornify_add);
    });          
  }
});

