// Define main namespace
var MCFC = {};

// Define init object that 
// handles on load events
MCFC.init = function() {
	
	// First make sure we can debug using console
	if(!window.console){ 
		window.console = { };
		methods = ['trace', 'log', 'info', 'debug', 'warn', 'error'];
		for (var key in methods){
			window.console[methods[key]] = function(msg){};
		};
	};
		
	// private variables
	var _dom_ready_callbacks = [];
	var _dom_ready = false;
	
	// on dom ready fires when jQuery document.ready fires.
	var on_dom_ready = function () {
		console.log('DOM ready');
		_dom_ready = true;
		obj.dispatch_ready(_dom_ready_callbacks);
	};	
	
	// This is the object what will represent MCFC.init	
	var obj = {
		
		add_on_dom_ready : function (fn) {
			if(_dom_ready) return fn();
			_dom_ready_callbacks.push(fn);
		},
		
		// utility method that fires the callbacks.
		dispatch_ready : function (callbacks) {
			callbacks.reverse();
			while(callbacks.length){
				callbacks.pop()();
			}
		}
	};
	
	$(document).ready(on_dom_ready);
	
	return obj;

}();



/*
	Manages URL hashes
*/
MCFC.hash = function() {
	
	var current_hash = "";
	var change_callbacks = [];
	var hash_change = function () {
		if(current_hash.toString() != pub.get().toString()) {
			for (var i=0; i < change_callbacks.length; i++) {
				change_callbacks[i](pub.get());
			};
			current_hash = pub.get();
		}
	};
	
	setInterval(hash_change, 150);
	
	var pub = {
		get : function(index){
			var url = document.location.hash;
			if(url.length === 0){
				return false;
			}
			var values = url.split("#/")[1];
			if(!values){
				return url;
			}
			var value = index !== undefined ? values.split("/")[index] : values && values.split("/") || false; 
			return value || false;
		},
		
		// string or array
		set : function(hash){
			if(hash !== undefined){
				hash = (typeof hash == 'object' && hash.length !== undefined) ? hash.join('/') : hash;
				hash = "#/" + hash;
			}else{
				hash = "#";
			}
			document.location.hash = hash;
			current_hash = this.get();
		},
		
		replace : function (index, value) {
			var all = this.get() || [];
			all[index]=value;
			this.set(all);
		},
		
		on_change : function (fn) {
			change_callbacks.push(fn);
		}
	};
	current_hash = pub.get();
		
	return pub;	
}();


MCFC.swf = function() {
	
	var default_flashvars = {
		debug : 0
	};
	var default_attributes = {};
	var default_params = {
		allowScriptAccess : 'sameDomain',
		allowFullScreen:"true",
		menu : false,
		quality : 'best',
		salign : 'tl',
		wmode : 'window'
	};
	
	return {
		embed : function (url, element_id, width, height, flashvars, params, attributes) {
			var flashvars = 	$.extend({}, default_flashvars, flashvars);
			var attributes = 	$.extend({}, default_attributes, attributes);
			var params = 		$.extend({}, default_params, params);
			if(!attributes.id) { attributes.id = element_id; }
			swfobject.embedSWF(url, element_id, width, height, "9.0.28", "http://www.mcfc.co.uk/flash/expressInstall.swf", flashvars, params, attributes);
			setTimeout(function(){MCFC.swf.focus(element_id);}, 500);
			return document.getElementById(attributes.id);
		},

		remove : function (element_id) {
			swfobject.removeSWF(element_id);
		},
		
		focus : function (element_id) {
			$('#'+element_id).focus();
		}
	};
	
}();


/* Behaviour for global search form */
MCFC.search = function () {
	var init = function () {
		var search_fields = [
			{
				element : '#searchterm',
				idle : 'Search'
			}
		];

		for (var i=0; i < search_fields.length; i++) {
			bindEventsForSearchElement($(search_fields[i].element), search_fields[i].idle);
			$(search_fields[i].element).blur();
		};
	};

	var bindEventsForSearchElement = function (el, idle) {
		el.blur(function(){
			if(!el.val()){
				el.val(idle);
				el.addClass('blur');
			}
		});
		el.focus(function(){
			if(el.val() == idle){
				el.val('');
				el.removeClass('blur');
			}
		});
	};
	
	MCFC.init.add_on_dom_ready(init);
	
}();


/* setup all jquery UI tabs */
MCFC.jquery_ui_tabs = function () {
	MCFC.init.add_on_dom_ready(function(){
		$(".jquery-ui-tabs").tabs();
	});
}();

/* simple one click share services */
MCFC.share = function () {
    var hover_timer;
    var init = function () {
        $('body').prepend('<div id="share_container" style="width:1px;height:1px;display:none;"></div>');    
        $('#mini-tab-add-this').mouseover(function (){
            clearTimeout(hover_timer);
            $('ul.sharing-list').show();
        });
        $('ul.sharing-list').mouseout(function () {
            hover_timer = setTimeout(function() {
                $('ul.sharing-list').hide();
            }, 500);
        });
    };
    
    MCFC.init.add_on_dom_ready(init);
    
	var popup = function (url) {
		window.open(url, 'share','scrollbars=yes,toolbar=0,status=0,width=750,height=500');
		return false;
	};	
	var services = {
		facebook : function (url, title) {
			return popup('http://www.facebook.com/sharer.php?u='+url+'&t='+title);
		},
		digg : function (url, title) {
			return popup('http://digg.com/submit?url='+url+'&amp;title='+title+'&amp;media=news&amp;topic=soccer');
		},		
		delicious : function (url, title) {
			return popup('http://delicious.com/save?v=5&amp;noui&amp;jump=close&amp;url='+url+'&amp;title='+title);
		},
		twitter : function (url, title) {
		    var msg = title + '%20@%20' + url;
		    return popup('http://twitter.com/home?status='+msg);
		},
		more : function (url, title) {
		    return addthis_open($('#share_container').get(0),'more', url, title);
		}
	};	
	return function(service, url, title) {
		var url = encodeURIComponent(url || location.href);
		var title = encodeURIComponent(title || document.title);
		return services[service](url, title);
	};
}();

var addthis_pub="pokemcfc"; // addThis.com user. Ovverride this in the DOM if we need to change settings for different templates.
var addthis_options = 'email, facebook, twitter, delicious, digg, more';

/* takes a json object and returns a query string*/
MCFC.serialize = function (obj, url) {
	var s = url && url+'?'||'';
	for(k in obj) {
		s += '&'+k+'='+obj[k];
	}
	return s;
};


MCFC.decorators = {
	scope : function (fn, obj) {
		var wrapper = function() {
			return fn.apply(obj);
		};
		return wrapper;
	}
};


MCFC.language_selection = function () {
    var init = function () {
        if (MCFC.language_selection.show && $.jqm) {
            $('#language_selection').jqm({modal:false});
            $('#language_selection').jqmShow(); 
        };
    };
    MCFC.init.add_on_dom_ready(init);
    return {};
}();









