// Globals.
var PageMappings = new Array();
	PageMappings['li_home'] 		= 1;
	PageMappings['li_about'] 		= 2;
	PageMappings['li_foodservice'] 		= 3;
	PageMappings['li_cash_carry'] 		= 4;
	PageMappings['li_careers'] 		= 5;
	PageMappings['li_territory'] 		= 6;
	PageMappings['li_contact_us'] 		= 7;
var PageTitleTextPrefix 			= "Ettline Foods | ";
var PageTitles = new Array();
	PageTitles['li_home'] 			= "Home";
	PageTitles['li_about'] 			= "About";
	PageTitles['li_foodservice'] 		= "FoodService Links";
	PageTitles['li_cash_carry'] 		= "Cash & Carry";
	PageTitles['li_careers'] 		= "Careers";
	PageTitles['li_territory'] 		= "Territory";
	PageTitles['li_contact_us'] 		= "Contact Us";
var siteURL = "http://cms.ettline.com";

function mycarousel_itemVisibleInCallback(carousel, item, i, state, evt)
{
    // The index() method calculates the index from a
    // given index who is out of the actual item range.
    var idx = carousel.index(i, mycarousel_itemList.length);
    carousel.add(i, mycarousel_getItemHTML(mycarousel_itemList[idx - 1]));
	carousel.startAuto();
};

function mycarousel_itemVisibleOutCallback(carousel, item, i, state, evt)
{
    carousel.remove(i);
};

function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

/**
 * Item html creation helper.
 */
function mycarousel_getItemHTML(item)
{
    return '<a href="' + item.aURL + '" target="_blank" title="Click here to visit this company."><img src="' + item.iURL + '" alt="' + item.alt + '" /></a>';
};

/*
  *	Changes the current page.
  */
function ChangePage(pageName)
{
	// Get the page ID from the mappings.
	pageId = PageMappings[pageName];
	
	// Hide the iframe.
	$("iframe#body_content").css("display", "none");
	
	// Set the iframe URL.
	$("iframe#body_content").attr("src", siteURL + "/Utility/AjaxGetPageById.php?id=" + pageId);
	
	// Reshow it.
	$("#body_content").fadeIn("slow");

	// Update the tab.
	ChangeTab( pageName );
}

function ChangeTab(theID)
{
	for ( var tabID in PageMappings ) 
	{
		// Toggle the tab and change the viewport.
		if (tabID == theID ) {
			$("li#" + theID).children("a").attr("class", "active");
			try {
				document.title = PageTitleTextPrefix + PageTitles[tabID];
			}catch(e){}
		}else{
			$("li#" + tabID).children("a").attr("class", "inactive");
		}
	}
}

jQuery(document).ready(function() {
    jQuery('#subfoot_l_ul').jcarousel({
        wrap: 'circular',
		scroll: 1,
		animation: 2500,
		auto: 5,
        itemVisibleInCallback: {onBeforeAnimation: mycarousel_itemVisibleInCallback},
        itemVisibleOutCallback: {onAfterAnimation: mycarousel_itemVisibleOutCallback},
		initCallback: mycarousel_initCallback
    });
	
	ChangePage('li_home');
	
	// Form submission hack for IE.
	$('input.formInput').keydown(function(e){
        if (e.keyCode == 13) {
            $(this).parents('form').submit();
            return false;
        }
    });
});
