//Market intelligence name space
var mi = {};
//MI Utils name space
mi.utils = {};

mi.utils.news_loader = {
	limit : 10,
	offset : 0,
	busy : false,

	setup : function() {
		$("#news-loader a").click(function() {
			mi.utils.news_loader.load();
			return false;
		});
	},

	load : function() {
		if (mi.utils.news_loader.busy) {
			return false;
		}

		mi.utils.news_loader.busy = true;
		mi.utils.news_loader.offset = mi.utils.news_loader.offset + mi.utils.news_loader.limit;
		var action = '/' + current_url;

		$.post(action, {offset:mi.utils.news_loader.offset, limit:mi.utils.news_loader.limit}, function(data) {
			$("#news-snippet-container").append(data);

			$(".news-page-col-right.slideIn, .news-page-col-left.slideIn").slideDown('slow', function() {
				$(this).removeClass('slideIn');
				mi.utils.news_loader.busy = false;
			});
		});
	}
};

//MI Slider
mi.utils.slider = {

	animating : false, //Is the dropdown animating?
	height_closed : false, //The initial height as defined in css when closed
	height_open : false, //The height when open

	subject : false, //The element we are controlling
	control : false, //The element to control the above

	//Compute heights and bind the click event
	setup : function(subject, control)
	{
		//Assign local vars
		this.subject = $(subject);
		this.control = $(control);

		//Initially not animating
		this.animating = false;

		//Store the closed height for later - remember it has to be closed initially incase of no js
		this.height_closed = this.subject.height();

		//Bind the control bar click to handle animations
		this.control.bind("click", this.handle_click);
	},

	handle_click : function(e)
	{
		//Prevent page jump
		e.preventDefault();

		//We cant get this height on dom ready as it is too incosistent (must be images loading)
		if (mi.utils.slider.height_open == false)
		{
			mi.utils.slider.subject.css("height", "auto");
			mi.utils.slider.height_open = mi.utils.slider.subject.height();
			mi.utils.slider.subject.css("height", mi.utils.slider.height_closed);
		}

		//Stop them piling up animations
		if (mi.utils.slider.animating == true)
			return;

		//Flag were animating...
		mi.utils.slider.animating = true;
		//...and work out which way were going based on current height
		animate_to = (mi.utils.slider.subject.height() == mi.utils.slider.height_closed) ? mi.utils.slider.height_open : mi.utils.slider.height_closed;

		//Perform the animation and set a callback for when its done
		mi.utils.slider.subject.animate({height: animate_to}, 666, function()
		{
			//All done lets allow them to toggle again
			mi.utils.slider.animating = false;
		});
	}

};

mi.utils.toggler = {

	animating : false,

	top_open : false,
	orig_title : false,

	top_closed : false,
	alt_class : false,
	alt_title : false,

	subject : false,
	control : false,

	setup : function(subject, control, alt_title, alt_class, alt_top)
	{
		//Initially not animating
		this.animating = false;

		//Assign local vars
		this.subject = $(subject);
		this.control = $(control);

		this.alt_class = alt_class;
		this.alt_title = alt_title;
		this.orig_title = this.control.html();

		this.top_open = this.subject.css("top");
		this.top_closed = alt_top;

		//Bind the control bar click to handle animations
		this.control.bind("click", this.handle_click);
	},

	handle_click : function(e)
	{
		//Prevent page jump
		e.preventDefault();

		//Stop them piling up animations
		if (mi.utils.toggler.animating == true)
			return;

		//Flag were animating...
		mi.utils.toggler.animating = true;

		//...and work out which way were going based on current height
		if (mi.utils.toggler.subject.css("top") == mi.utils.toggler.top_closed)
			mi.utils.toggler.toggle_open();
		else
			mi.utils.toggler.toggle_close();

	},

	toggle_open : function(e)
	{
		//Perform the animation and set a callback for when its done
		mi.utils.toggler.subject.animate({top: mi.utils.toggler.top_open}, 666, function()
		{
			mi.utils.toggler.control.html(mi.utils.toggler.orig_title);
			mi.utils.toggler.control.removeClass(mi.utils.toggler.alt_class);
			//All done lets allow them to toggle again
			mi.utils.toggler.animating = false;
		});
	},

	toggle_close : function(e)
	{
		//Perform the animation and set a callback for when its done
		mi.utils.toggler.subject.animate({top: mi.utils.toggler.top_closed}, 666, function()
		{
			mi.utils.toggler.control.html(mi.utils.toggler.alt_title);
			mi.utils.toggler.control.addClass(mi.utils.toggler.alt_class);
			//All done lets allow them to toggle again
			mi.utils.toggler.animating = false;
		});
	}


};


mi.utils.hoverer = {

	animating : false,
	subject_height : false,
	subject_class : false,
	control_class : false,

	setup : function(subject_class, control_class)
	{
		//Assign local vars
		this.subject_class = subject_class;
		this.control_class = control_class;

		//Store open heights then close em
		$(this.subject_class).each(function()
		{
			$(this).css("display", "block");
			open_height = mi.utils.hoverer.get_height($(this));
			closed_height = -open_height;
			$(this).css("top", closed_height + "px");
		});

		//Bind the control bar click to handle animations
		$(this.control_class).hover(this.hover_in, this.hover_out);
	},

	get_height: function(e)
	{
		height = 	parseInt(e.css("height").replace("px", "")) +
					parseInt(e.css("paddingTop").replace("px", "")) +
					parseInt(e.css("paddingBottom").replace("px", ""));
		return height;
	},

	hover_in : function()
	{
		subject = $(this).find(mi.utils.hoverer.subject_class).eq(0);
		subject.stop();
		subject.animate({ top: "0px" }, 666);
	},

	hover_out : function()
	{
		subject = $(this).find(mi.utils.hoverer.subject_class).eq(0);
		subject.stop();
		open_height = mi.utils.hoverer.get_height(subject);
		closed_top = -open_height;
		subject.animate({ top: closed_top + "px" }, 666);
	}

};

mi.utils.map_slider = {

	animating : false,
	right_closed : false,

	subject : false, //The element we are controlling
	control : false, //The element to control the above

	//Compute heights and bind the click event
	setup : function(subject, control)
	{
		//Assign local vars
		this.subject = $(subject);
		this.control = $(control);

		//Initially not animating
		this.animating = false;

		//Work out the open and closed heights - remember it has to be closed initially incase of no js -or not (client ammend)
		this.right_closed = "-450px"; //this.subject.css("right");

		//Bind the control bar click to handle animations
		this.control.bind("click", this.handle_click);
	},

	handle_click : function(e)
	{
		//Prevent page jump
		e.preventDefault();

		//Stop them piling up animations
		if (mi.utils.map_slider.animating == true)
			return;

		//Flag were animating...
		mi.utils.slider.animating = true;
		//...and work out which way were going based on current height
		animate_to = (mi.utils.map_slider.subject.css("right") == mi.utils.map_slider.right_closed) ? 0 : mi.utils.map_slider.right_closed;

		//Perform the animation and set a callback for when its done
		mi.utils.map_slider.subject.animate({right: animate_to}, 666, function()
		{
			//All done lets allow them to toggle again
			mi.utils.map_slider.animating = false;
		});
	}

};


mi.utils.prog_slider = {

	subject_a : false,
	subject_b : false,
	prefix : false,

	//Compute heights and bind the click event
	setup : function(subject_a, subject_b, prefix)
	{
		//Assign local vars
		this.subject_a = $(subject_a);
		this.subject_b = $(subject_b);
		this.prefix = prefix;

		$('select' + subject_a + ', select' + subject_b).selectToUISlider(
		{
			labels: 0,
			sliderOptions: { change: mi.utils.prog_slider.handle_change }
		}).hide();

	},


	handle_change : function(e, ui)
	{
		from = mi.utils.prog_slider.subject_a.val();
		to = mi.utils.prog_slider.subject_b.val();

		outs = new Array();
		ins = new Array();

		found_start = false;
		mi.utils.prog_slider.subject_a.find("option").each(function()
		{
			if ($(this).val() == from)
				found_start = true;

			if (found_start == true)
				ins[ins.length] = "#" + mi.utils.prog_slider.prefix + $(this).val();
			else
				outs[outs.length] = "#" + mi.utils.prog_slider.prefix + $(this).val();

			if ($(this).val() == to)
				found_start = false;
		});

		if (outs.length > 0)
			$(outs.join(',')).slideUp(666);

		if (ins.length > 0)
			$(ins.join(',')).slideDown(666);
	}

};

//Not really general so stuffs hardcoded
mi.utils.prog_buttons = {

	animating : false,

	setup : function()
	{
		$("#prog-btn-all").bind("click", this.click_show_all);
		$("#prog-btn-sessions").bind("click", this.click_hide_non_sessions);
		$("#prog-btn-speakers").bind("click", this.click_show_speakers);
	},

	click_show_all : function(e)
	{
		e.preventDefault();

		if (mi.utils.prog_buttons.animating)
			return;
		mi.utils.prog_buttons.animating = true;

		$("#prog-btn-sessions").removeClass("btn_enabled");
		$("#prog-btn-speakers").removeClass("btn_enabled");
		$("#prog-btn-all").addClass("btn_enabled");

		$("#programme-speakers").fadeOut(666, function()
		{
			if ($("#programme").css("display") == "none")
				mi.utils.prog_buttons.show_programme();

			mi.utils.prog_buttons.show_sessions();
			mi.utils.prog_buttons.show_non_sessions();
		});
	},

	click_hide_non_sessions : function(e)
	{
		e.preventDefault();

		if (mi.utils.prog_buttons.animating)
			return;
		mi.utils.prog_buttons.animating = true;

		$("#prog-btn-sessions").addClass("btn_enabled");
		$("#prog-btn-speakers").removeClass("btn_enabled");
		$("#prog-btn-all").removeClass("btn_enabled");

		$("#programme-speakers").fadeOut(666, function()
		{
			if ($("#programme").css("display") == "none")
				mi.utils.prog_buttons.show_programme();

			mi.utils.prog_buttons.show_sessions();
			mi.utils.prog_buttons.hide_non_sessions();
		});

	},

	click_show_speakers : function(e)
	{
		if (e)
			e.preventDefault();

		if (mi.utils.prog_buttons.animating)
			return;

		mi.utils.prog_buttons.animating = true;

		$("#prog-btn-sessions").removeClass("btn_enabled");
		$("#prog-btn-speakers").addClass("btn_enabled");
		$("#prog-btn-all").removeClass("btn_enabled");

		$("#programme").fadeOut(666, function()
		{
			mi.utils.prog_buttons.show_speakers();
		});
	},

	show_programme : function() { $("#programme").fadeIn(666); },
	hide_programme : function() { $("#programme").fadeOut(666); },
	show_sessions : function() { $(".programme-session").slideDown(666); },
	hide_sessions : function() { $(".programme-session").slideUp(666); },
	show_non_sessions : function() { $(".programme-not-session").slideDown(666, function() { mi.utils.prog_buttons.animating = false; } ); },
	hide_non_sessions : function() { $(".programme-not-session").slideUp(666, function() { mi.utils.prog_buttons.animating = false; } ); },
	show_speakers : function() { $("#programme-speakers").fadeIn(666, function() { mi.utils.prog_buttons.animating = false; } ); },
	hide_speakers : function() { $("#programme-speakers").fadeOut(666, function() { mi.utils.prog_buttons.animating = false; } ); }
};

mi.sponsors =
{

	images_width : 0,
	append_first : true,
	animate_first_to : false,
	animate_second_to : 0,
	base_speed : 30000,
	base_ratio : false,
	current_speed : false,

	init : function()
	{
		this.current_speed = this.base_speed;
		$("#sponsors").bind("run_slides", mi.sponsors.run_slides);
		this.start_slides();
	},

	bind_hover : function()
	{
		$("#sponsors-logos-outer").bind("mouseenter", function()
		{
			$("#sponsors-logos-one").stop();
			$("#sponsors-logos-two").stop();
		});
		$("#sponsors-logos-outer").bind("mouseleave", function()
		{
			$("#sponsors").trigger('run_slides');
		});

	},

	start_slides : function()
	{
		$('#sponsors-logos-one a').each(function(idx, val)
		{
			mi.sponsors.images_width = mi.sponsors.images_width + $(val).outerWidth(true);
		});

		if (mi.sponsors.images_width <= $("#sponsors-logos-outer").innerWidth())
			return true;

		this.bind_hover();

		$("#sponsors-logos-one").clone().attr("id", "sponsors-logos-two").css("left", mi.sponsors.images_width).insertAfter("#sponsors-logos-one");

		outer_container_width = this.images_width * 2;
		$('#sponsors-logos').width(outer_container_width + "px");
		$('#sponsors-logos-one').width(this.images_width + "px");
		$('#sponsors-logos-two').width(this.images_width + "px");
		$('#sponsors-logos-two').css('left', this.images_width + 'px');

		this.animate_first_to = -this.images_width;

		this.base_ratio = this.base_speed / this.images_width;

		$("#sponsors").trigger('run_slides');

		return true;
	},


	run_slides : function()
	{
		running_auto = -2;
		if ($('#sponsors-logos-one').css("left") == mi.sponsors.animate_second_to + "px")
			running_auto++;
		if ($('#sponsors-logos-two').css("left") == mi.sponsors.animate_first_to + "px")
			running_auto++;
		if ($('#sponsors-logos-one').css("left") == mi.sponsors.animate_first_to + "px")
			running_auto++;
		if ($('#sponsors-logos-two').css("left") == mi.sponsors.animate_second_to + "px")
			running_auto++;

		if (running_auto != 0)
		{
			if (mi.sponsors.append_first === true)
				mi.sponsors.append_first = false;
			else
				mi.sponsors.append_first = true;
		}

		if (mi.sponsors.append_first === true)
		{
			mi.sponsors.append_first = false;
			$subj_a = $('#sponsors-logos-one');
			$subj_b = $('#sponsors-logos-two');
		}
		else
		{
			mi.sponsors.append_first = true;
			$subj_a = $('#sponsors-logos-two');
			$subj_b = $('#sponsors-logos-one');
		}

		if (running_auto == 0)
			$subj_a.css("left", mi.sponsors.images_width);

		mi.sponsors.compute_speed();

		$subj_b.animate( { left: mi.sponsors.animate_first_to }, mi.sponsors.current_speed, 'linear');

		$subj_a.animate( { left: mi.sponsors.animate_second_to }, mi.sponsors.current_speed, 'linear', function()
		{
			$("#sponsors").trigger('run_slides');
		});

		return true;
	},

	compute_speed : function()
	{
		a_left = $("#sponsors-logos-one").css("left");
		b_left = $("#sponsors-logos-two").css("left");
		a_left = a_left.replace("px", "");
		b_left = b_left.replace("px", "");

		if (a_left < b_left)
			distance = b_left - mi.sponsors.animate_second_to;
		else
			distance = a_left - mi.sponsors.animate_second_to;

		speed = mi.sponsors.base_ratio * distance;

		mi.sponsors.current_speed = speed;
	}
}

//Dom ready
$(function()
{
	//Setup slider for use with sponsor logos
	//mi.utils.slider.setup("#sponsors-logos", "#sponsors-control");
	mi.utils.toggler.setup("#home-main-image-cover", "#home-cover-toggle", "Maximise", "home-cover-toggle-closed", "340px");
	mi.utils.hoverer.setup(".straight-sidebar-dropdown", ".straight-sidebar-content");
	mi.utils.map_slider.setup("#gmap-box-map", "#gmap-box-bottom");
	mi.utils.news_loader.setup();

	$(".gallery-box-inner a").fancybox(
	{
		'zoomOpacity' : true,
		'overlayShow' : false,
		'zoomSpeedIn' : 666,
		'zoomSpeedOut' : 666
	});

	$("a.speaker-profile").fancybox({ 'hideOnContentClick': true });

	var aurl = document.location.toString();
	if (aurl.match('#'))
	{
		req = aurl.split('#')[1];
		if (req == "speakers")
		{
			mi.utils.prog_buttons.click_show_speakers();
		}
	}

	$(".open_external").attr('target', '_blank');

	$(".mi-content-box-inner .anchor-button a").bind("click", function(e)
	{
		if ($(this).attr("href").match('#'));
		{
			req = $(this).attr("href").split('#')[1];
			if (req == "speakers")
				mi.utils.prog_buttons.click_show_speakers();
		}
	});

	$(".input-toggle-field").focus(function()
	{
		def_field = $(this).attr('id') + '-default';
		def_val = $("#" + def_field).val();
		if ($(this).val() == def_val)
			$(this).val('');
	});

	$(".input-toggle-field").blur(function()
	{
		if ($(this).val() == "")
		{
			def_field = $(this).attr('id') + '-default';
			def_val = $("#" + def_field).val();
			$(this).val(def_val);
		}
	});


	$("div.box-trigger").bind("click", function(e)
	{
		e.preventDefault();
		href = $(this).find("a.hide").eq(0).attr("href");
		window.open(href, "_blank");
		return false;
	});

	//new sponsors stuff
	$(window).load(function() {	mi.sponsors.init(); });
});