var md = {};

// md.click_label() function - all labels using this function must have a common class
// and all inputs must have a common class - ex. md.click_label('text-input-label','field');
md.click_label = function (match_class_label, match_class_input) {
	$('label.' + match_class_label).each(function () {
		$(this).hide();
		$('input#' + $(this).attr("for")).val($(this).text());
	});
	$('input.' + match_class_input).focus(function () {
		$(this).val() == $('label[for="' + $(this).attr('id') + '"]').text() ? $(this).val('') : '';
	});
	$('input.' + match_class_input).blur(function () {
		$(this).val() == '' ? $(this).val($('label[for="' + $(this).attr('id') + '"]').text()) : '';
	});
}

// md.active_toc() function - this is only for cm3 articles
// function needs parameter for div that wraps navigation - generally this will be 'as_toc'
md.active_toc = function (parent_selector) {
	if($(parent_selector + ' a') != null){
		$(parent_selector + ' a').each(function () {
			if($(this).text() == $('.as_title:first').text())
			{
				//alert('test');
				$(this).closest('li').addClass('active');
				$(this).click(function () {
					return false;
				});
			}
		});
	}
}
// md.cm_article_rotation() function - this will extract the content from all cm3 articles in a section
// 1st paremeter is the cm3 section id and 2nd is the div where the content will be appended - (include '.' or '#' to indicate id or class)
//3rd parameter can be used to append a close button (true or false)
md.cm_article_rotation = function (section_id, append_to, close_button) {
	$.get("/cm3/sections/show_all_articles/" + section_id, function(data){
		var data_wrapper = '<div class="data_wrapper">' + data + '</div>';
		buildRotation(data_wrapper);
	});
	
	function buildRotation(data_wrapper)
	{
		var content_items = new Array();
		$(data_wrapper).find('div.article_list > div').each(function () {
			content_items.push({
				title: '<h2>' + $(this).find('.as_title').text() + '</h2>',
				main_image: $(this).find('.as_image').text(),
				content: $(this).find('.as_text').html()
			});
		});
		$(append_to).append('<ul class="rotation-nav"></ul><div class="content-item" style="display: none"></div>');
		for(i = 0; i < content_items.length; i++)
		{
			$(append_to).find('ul.rotation-nav').append('<li class="item_' + i + '"><a href="rotating-item_' + i  + '">' + $(content_items)[i].title + '</a></li>');
		}
		$('ul.rotation-nav li a').live('click', function () {
			$('ul.rotation-nav li a').removeClass('active');
			$(this).addClass('active');
			var active_index = $(this).attr('href').replace(/^(.+)(_)(.+)$/,'$3');
			$(append_to).find('div.content-item').html($(content_items)[active_index].title + $(content_items)[active_index].main_image + $(content_items)[active_index].content);
			close_button == true ? $(append_to).find('div.content-item').append('<a href="#" class="close">close x</a>') : '';
			$(append_to).find('div.content-item').show();
			return false;
		});
		
		$(append_to).find('div.content-item a.close').live('click', function () {
			$('ul.rotation-nav li a').removeClass('active');
			$(append_to).find('div.content-item').hide();
			return false;
		});
	}
}

// md.cm_picture_rotation() function - this will extract the content from a cm3 article in a specific section
// 1st paremeter is the cm3 section id and 2nd is the div where the content will be appended - (include '.' or '#' to indicate id or class)
// 3rd paremeter is a selector that can be used upon click to stop the rotation (use false for param if you wish not to use)
// 4th paremeter is a selector that can be used upon click to restart the rotation (use false for param if you wish not to use)
md.cm_picture_rotation = function (article_id, append_to, stop_trigger, restart_trigger) {
	var current_item = 0;
	var next_item = 0;
	var timeout = 5000;
	
	$.get("/cm3/articles/show/" + article_id, function(data){
		var data_wrapper = '<div class="data_wrapper">' + data + '</div>';
		buildRotation(data_wrapper);
	});
	
	function buildRotation(data_wrapper)
	{
		var content_items = new Array();
		var autoRotate;
		$(data_wrapper).find('div.as_text img').each(function () {
			content_items.push(this);
		});
		
		setTimeout(function() { 
			$(append_to).find('img').fadeOut("slow");
			$(content_items[next_item]).hide();
			$(append_to).append(content_items[next_item]);
			$(content_items[next_item]).fadeIn(1500, function () {
				current_item = next_item;
			});
			autoRotate = setInterval(function ()
			{
				current_item < content_items.length - 1 ? next_item++ : next_item = 0;
				$(append_to).find('img').fadeOut("slow");
				$(content_items[next_item]).hide();
				$(append_to).append(content_items[next_item]);
				$(content_items[next_item]).fadeIn(1500, function () {
					current_item = next_item;
				});
			}, timeout);
		}, 3000);
		

		
		$(stop_trigger).live('click', function () {
			clearInterval(autoRotate);
			return false;
		});
		
		$(restart_trigger).live('click', function () {
			autoRotate = setInterval(function ()
			{
				current_item < content_items.length - 1 ? next_item++ : next_item = 0;
				$(append_to).find('img').fadeOut("slow");
				$(content_items[next_item]).hide();
				$(append_to).append(content_items[next_item]);
				$(content_items[next_item]).fadeIn(1500, function () {
					current_item = next_item;
				});
			}, timeout);
			return false;
		});
		
	}
}