﻿$(document).ready(function() {
	// Modal window
	
	$(".modal").fancybox({
		"titleShow" : false,
		"overlayColor" : "#00162e",
		"overlayOpacity" : "0.8",
		"type" : "iframe",
		"width" : 775,
		"height" : 375,
		"onStart" : function(){
			$("<h4></h4>")
				.text("Submit your information to experience the DMI difference")
				.appendTo("#fancybox-wrap");
		},
		"onClosed" : function(){
			$("#fancybox-wrap").find("h4").remove();
		}
	});
	
	// Home page feature list
	if ($('div.homeContentMain').length) {
		$.featureList(
			$("ul.tabs li"),
			$("ul.output li"), {
				start_item: 0
			});
		}
	
	// Sidenav accordion
	if ($('ul.sideNav').length) {
		$('li.expandable div a').click(function(e) {
			if ($(this).parent().hasClass('active')) {
				$(this).parent().removeClass('active');
				$(this).children('.arrow').removeClass('down');
				$(this).parent().next().slideUp();
				e.preventDefault();
			}
			else {
				var self = $(this),
					parent1 = self.parent(),
					parent3 = self.parent().parent().parent();
				
				if (parent3.find('.active')) {
					parent3.find('.arrow').removeClass('down');
					parent3.find('.active').next().slideUp();
					parent3.find('.active').removeClass('active');
				}
				parent1.addClass('active');
				self.children('.arrow').addClass('down');
				parent1.next().slideDown();
				e.preventDefault();
			}
		});
	}

	// Inner pages sidenav and main content height matching
	if (($('div.inner').length) && !($('div.tab_main').length) && !($('div.slider').length)){
		var contentHeight = $('div.contentBody').height() + 126;
		var sidenavHeight = $('ul.sideNav').height() + 12;

		if (contentHeight > sidenavHeight)
			$('ul.sideNav').css('min-height', contentHeight - 12).css('height', contentHeight - 12);
		else
			$('div.contentBody').height(sidenavHeight - 126);

		$('div.bucketsWrapper div.bucket').equalHeight();
	}
	
	
	// Inner pages sidenav and main content height matching, with tabs.
	if (($('div.inner').length) && ($('div.tab_main').length) && !($('div.slider').length)) {
				
		var maxHeight = 0;
		 
		//loop through each tab and set the max height.
		$('div.tab_main').each(function(){
        if (maxHeight < $(this).height()) {maxHeight = $(this).height()}});
	
		var contentHeight = maxHeight;
		var sidenavHeight = $('ul.sideNav').height() + 12;

		if (contentHeight > sidenavHeight)
		{
            $('div.contentBody').height(contentHeight + 50);
			$('ul.sideNav').css('min-height', contentHeight + 165).css('height', contentHeight +165);
		}
		else
			$('div.contentBody').height(sidenavHeight - 126);

		$('div.bucketsWrapper div.bucket').equalHeight();
	}
	
	// Inner pages sidenav and main content height matching, with sliders.
	if (($('div.inner').length) && !($('div.tab_main').length) && ($('div.slider').length)) {
		
		var maxHeight = 0;
		 
		//loop through each slider div, and set the max height.
		$('div.slider').each(function(){
        if (maxHeight < $(this).height()) {maxHeight = $(this).height()}});

	
		var contentHeight = maxHeight + 150;
		var sidenavHeight = $('ul.sideNav').height() + 12;

		if (contentHeight > sidenavHeight)
		{
            $('div.contentBody').height(contentHeight - 50);
			$('ul.sideNav').css('min-height', contentHeight + 65).css('height', contentHeight + 65);
		}
		else
			$('div.contentBody').height(sidenavHeight - 126);
			
			//document.write ($('ul.sideNav').height());

		$('div.bucketsWrapper div.bucket').equalHeight();
	}

	// Inner tabs template
	if ($('div.inner ul.tabs').length) {
		$('ul.tabs').tabs('div.panes > div');
	}

	// Inner pages scrollable template
	if ($('div.inner .scrollable').length) {
		$('div.tabs').hide();
		$('div.tab1').show();

		$('ul.items li a').live('click', function(e) {
			var target = $(this).attr('href').replace(window.location,'').substring(1);
			$('div.tabs').hide();
			$('div.' + target).show();
			e.preventDefault();
		});

		var ulItems = $('ul.items');
		var allow = true;

		$('a.left').click(function(e) {
			var theLast = $('ul.items li:last');
			if (allow == true) {
				allow = false;
				theLast.clone().prependTo(ulItems);
				ulItems.animate({ 'left': '-=149px' }, 0);
				ulItems.stop(true, true).animate({ 'left': '+=149px' }, 500, function() {
					theLast.remove();
					allow = true;
				});
			}
			e.preventDefault();
		});

		$('a.right').click(function(e) {
			var theFirst = $('ul.items li:first');
			if (allow == true) {
				allow = false;
				theFirst.clone().appendTo(ulItems);
				ulItems.stop(true, true).animate({ 'left': '-=149px' }, 500, function() {
					ulItems.animate({ 'left': '+=149' }, 0);
					theFirst.remove();
					allow = true;
				});
			}
			e.preventDefault();
		});
	}
});
