﻿/*

*

*  Taylor Rafferty javascript

*  Latest update:090703

*  By: Patrik Totero

*
 
*/

/*
/* ON DOM LOAD
-----------------------------------------------------------------------*/
$(document).ready(function() {
    initCarusel();
    restyleTable();
});

/* CARUSEL v1.1 - CREATE CARUSEL NAVIGATION ON DOM LOAD BASED ON THE CARUSEL ITEMS AND HOOK UP CLICK EVENT
-----------------------------------------------------------------------*/
var initCarusel = function() {

	// get carusel
	var carusel = $('#caruselwrapper');
	if (carusel.length == 0) return;

	// get all h1 and h2 in carusel item
	var itemHeadingsArray = carusel.find('.caruselitemheading');
	if (itemHeadingsArray.length == 0) return;

	// create UL for carusel menu and add it after #caruselwrapper div 
	var caruselUl = '<ul class="caruselmenu"></ul>';
	carusel.after(caruselUl);

	// create array for putting LIs in carusel menu
	var caruselLi = [];
	// populate the LIs and set innerHTML equal to carusel item heading. Then appent it to caruselUl
	$(itemHeadingsArray).each(function(i) {
		if (i == 0) {
			caruselLi[i] = '<li class="first selected"><a href="#caruselwrapper" class="caruselitem" name="' + i + '">' + $(itemHeadingsArray[i]).html() + '</a></li>'
		} else {
			caruselLi[i] = '<li><a href="#caruselwrapper" class="caruselitem" name="' + i + '">' + $(itemHeadingsArray[i]).html() + '</a></li>'
		}
		$('.caruselmenu').append(caruselLi[i]);
	});

	//select all the a tag with name equal to carusel item  
	$('a[class=caruselitem]').click(function(e) {
		//Cancel the link behavior  
		e.preventDefault();
		$(this).blur();

		var clicked = $(this);
		//get the href of clicked item
		var href = $(this).attr('href');
		//get the anchor id #caruselwrapper
		var id = $(this).attr('href').substring(href.indexOf('#'), href.indexOf('#') + 15)

		//get the name attribute containing the position in the navigation list
		var posIndex = $(this).attr('name');

		// the with of the slide
		var slideWidth = 890;

		// get all li elements in caruselMenu
		var menuItemsArray = $(this).parent().parent().children();

		// animate the slide by setting negative margin
		$($(id)).animate({
			marginLeft: posIndex * -slideWidth + 'px'
		}, 600, 'swing', function() {
			// remove class selected if set
			$(menuItemsArray).each(function(i) {
				$(menuItemsArray).removeClass('selected');
			});
			// add class selected on the clicked li element
			clicked.parent().addClass('selected');
		});

	});

	var caruselMenu = $("ul.caruselmenu");
		caruselMenu.css({ 'width': "100%" });
	var colWrap = caruselMenu.width()-12;

	var itemLength = $("ul.caruselmenu li").length;
	var colLength = Math.floor(colWrap / itemLength)-3;

	$("ul.caruselmenu li").css({ width: colLength });
}

/*  RESTYLE TABLES
----------------------------------------------------*/
var restyleTable = function() {
    //get table with classname "restyled"
    var domTable = $('.restyled');
    if (domTable.length == 0) return;

    //get all TR elements in table
    var tableRowArray = $(domTable).find('tr');

    //get table head
    var tableHead = $(domTable).find('th');
    //if TH doesn't exist, change the TD in the first TR to TH
    if (tableHead.length == 0) {
        var tableTD = $(tableRowArray[0]).find('td');
        $(tableTD).each(function(m) {
            $(tableTD[m]).replaceWith('<th>' + $(tableTD[m]).text() + '</th>');
        });
    }

    //loop through all rows and add class name to every secont TR
    $(tableRowArray).each(function(i) {
        if (i % 2 == 1) $(tableRowArray[i]).addClass('altrow');
    });
}
