// mixtec, base js

// mixtec object
var mixtec = {};
// debug?
mixtec.debug = true;

// on ready
$(document).ready(function() {
	// apply bindings
	mixtec.bind();
});

// apply bindings
mixtec.bind = function(context) {
	// contact map
	$('#contact_map', context).each(function(index) {
		// don't reapply if already JS'd
		if ($(this).hasClass('js_applied')) return;
		// turn <li>'s into tool tips
		$(this).find('li').each(function(index) {
			// if no id, generate one
			if (!$(this).attr('id')) {
				var location_id = 'contact_map' + new Date().getTime() + '_' + index;
				$(this).attr('id', location_id);
			}
			// store content
			$(this).attr('tool_tip_content', $(this).html());
			// change <li> to push pin, change HTML and apply bindings
			$(this).addClass('tir push_pin tool_tip_trigger').html($(this).find('.title').eq(0).text()).click(function() { mixtec.toolTipToggle($(this).attr('id')); });
			// if selected <li>, select it after a tiny delay
			if ($(this).hasClass('selected')) setTimeout('mixtec.toolTipToggle("' + $(this).attr('id') + '");', 500);
		});
		// and use js_applied styles (and to parent for .class_name #id_name because IE wont do #id_name.class_name)
		$(this).addClass('js_applied').parent().eq(0).addClass('js_applied');
	});
	// form dependencies
	$('.form_dependency').each(function(index) {
		// get the dependency
		var $dependee = $(this);
		var pieces = $(this).attr('value').split(',');
		var input_id = pieces[0];
		var input_values = pieces[1].split('|');
		// apply bindings
		$('#' + input_id).change(function() {
			// if has value
			if ($(this).val()) {
				// in depency met, show form field
				if ($.inArray($(this).val(), input_values) > -1) $dependee.parents('.form_field').eq(0).slideDown(200);
				// else hide form field
				else $dependee.parents('.form_field').eq(0).slideUp(200);
			}
		});
	});
	// measurement system
	$('input[name="measurement_system"]').change(function() {
		// hide all units
		$('.unit').addClass('hide');
		// show these units
		$('.unit_' + $(this).attr('value')).removeClass('hide');
	});
	// photo gallery
	if ($('#gallery').length) {
		// load theme
		Galleria.loadTheme('/assets/templates/mixtec/js/galleria_themes/mixtec/galleria.mixtec.js');
		// init gallery
		$('#gallery').galleria();
	}
	// sliders
	$('.slider', context).each(function(index) {
		// don't reapply if already JS'ed
		if ($(this).hasClass('js_applied')) return;
		// if no id, generate one
		if (!$(this).attr('id')) {
			var scroller_id = 'scroller_' + new Date().getTime() + '_' + index;
			$(this).attr('id', scroller_id);
		}
		// write the slider nav HTML
		var slider_nav_html = '<ul id="' + $(this).attr('id') + '_nav" class="slider_nav">';
		for (var i=0; i < $(this).find('li').length; i++) {
			slider_nav_html += '<li' + (i == 0 ? ' class="selected"' : '') + '><a href="javascript: mixtec.sliderSelect(\'' + $(this).attr('id') + '\', ' + i + ', true);" class="tir">Show Pane ' + (i + 1) + '</a></li>';
		};
		slider_nav_html += '</ul>';
		// add in the slider nav HTML
		$(this).after(slider_nav_html);
		// add JS applied style
		$(this).addClass('js_applied');
		// if auto-slide
		if ($(this).hasClass('auto_slide')) {
			// start the timer
			var timer_id = setTimeout('mixtec.sliderAutoSlide(\'' + $(this).attr('id') + '\')', 7500);
			$(this).attr('timer_id', timer_id);
		}
	});
};

// debug message
mixtec.debugMessage = function(message) {
	// if debug, show it
	if (mixtec.debug && typeof(console) !== 'undefined' && console != null) console.log('[mixtec] ' + message);
};

// slider, auto slide it
mixtec.sliderAutoSlide = function(slider_id) {
	// the slider
	var $slider = $('#' + slider_id);
	// the currently selected index
	var index_selected = $slider.find('li').index($slider.find('li.selected'));
	// the number of panes
	var panes = $slider.find('li').length;
	// index to select
	var index_to_select = index_selected + 1;
	// if we need to loop back to the start
	if (index_to_select > (panes - 1)) index_to_select = 0;
	// select the pae
	mixtec.sliderSelect(slider_id, index_to_select);
	// and retrigger
	var timer_id = setTimeout('mixtec.sliderAutoSlide(\'' + slider_id + '\')', 7500);
	$slider.attr('timer_id', timer_id);
};

// slider, select a pane
mixtec.sliderSelect = function(slider_id, index_to_select, nav_clicked) {
	// debug
	mixtec.debugMessage('Show pane index ' + index_to_select + ' for slider ID #' + slider_id);
	// the slider
	var $slider = $('#' + slider_id);
	// the index of the currently selected pane
	var index_selected = $slider.find('li').index($slider.find('li.selected'));
	// if the index_to_select is already selected, do nothing
	if (index_to_select == index_selected) return;
	// if the index_to_select doesn't exist, do nothing
	if (index_to_select < 0) return;
	if (index_to_select > $slider.find('li').length - 1) return;
	// the slider nav
	var $slider_nav = $('#' + slider_id + '_nav');
	// direction to animate
	var animate_direction = index_selected > index_to_select ? 1 : -1;
	var pane_height = $slider.find('li').eq(0).outerHeight();
	// animate out the selected index
	$slider.find('li').eq(index_selected).animate({ top: animate_direction * pane_height }, 360, 'linear', function() { $(this).removeClass('selected'); });
	// animate in the index to select
	$slider.find('li').eq(index_to_select).css({ top: animate_direction * pane_height * -1 }).addClass('selected').animate({ top: 0 }, 360, 'linear');
	// update nav
	$slider_nav.find('li.selected').removeClass('selected');
	$slider_nav.find('li').eq(index_to_select).addClass('selected');
	// if nav was clicked, delay the auto slide if neccessary
	if (nav_clicked && $slider.hasClass('auto_slide')) {
		// kill any auto slider interval
		clearTimeout(parseInt($slider.attr('timer_id')));
		// and set new longer interval
		var timer_id = setTimeout('mixtec.sliderAutoSlide(\'' + slider_id + '\')', 15000);
		$slider.attr('timer_id', timer_id);
	}
};

// toggle a tool tip
mixtec.toolTipToggle = function(tool_tip_trigger_id) {
	// debug
	mixtec.debugMessage('Toogle tool tip for trigger #' + tool_tip_trigger_id);
	// the tool tip trigger
	var $tool_tip_trigger = $('#' + tool_tip_trigger_id);
	// if the tool tip is hidden
	if (!$tool_tip_trigger.hasClass('push_pin_selected')) {
		// unselect any other triggers
		$('.tool_tip_trigger').removeClass('push_pin_selected');
		// fade out any other tool tips
		$('.tool_tip').fadeOut(350, function() { $(this).remove(); });
		// tigger offset
		var offset = $tool_tip_trigger.offset();
		// add class
		$tool_tip_trigger.addClass('push_pin_selected');
		// generate tool tip HTML
		var tool_tip_html = '<div class="tool_tip">' + $tool_tip_trigger.attr('tool_tip_content') + '</div>';
		// add in and basic positioning
		var $tool_tip = $(tool_tip_html).appendTo('body');
		// hide
		$tool_tip.fadeOut(0);
		// left position
		var pos_left = offset.left + 36;
		while (pos_left + $tool_tip.outerWidth() + 30 > $(window).width()) pos_left--;
		// top position
		var pos_top = offset.top - $tool_tip.outerHeight() + 13;
		while (pos_top + $tool_tip.outerHeight() + 30 > $(window).height()) pos_top--;
		// position and fade in
		$tool_tip.css({ left: pos_left + 'px', top: pos_top + 'px' }).fadeIn(350);
	}
	// else its shown, hide it
	else {
		// remove class
		$('.tool_tip_trigger').removeClass('push_pin_selected');
		// fade out any tool tips
		$('.tool_tip').fadeOut(350, function() { $(this).remove(); });
	}
};
