/*
 jQuery Coda-Slider v2.0 - http://www.ndoherty.biz/coda-slider
 Copyright (c) 2009 Niall Doherty
 This plugin available for use in all personal or commercial projects under both MIT and GPL licenses.
*/

JQ(function(){
 // Preloader
    JQ(".coda-slider").children('.panel').hide().end().prepend('<p class="loading">Loading...<br /><img src="/Templates/Images/Siemens/ajax-loader.gif" alt="loading..." /></p>');
});

var sliderCount = 1;

JQ.fn.codaSlider = function(settings) {

 settings = JQ.extend({
 autoHeight: true,
 autoHeightEaseDuration: 1000,
 autoHeightEaseFunction: "easeInOutExpo",
 autoSlide: false,
 autoSlideInterval: 7000,
 autoSlideStopWhenClicked: true,
 crossLinking: true,
 dynamicArrows: true,
 dynamicArrowLeftText: "&#171; left",
 dynamicArrowRightText: "right &#187;",
 dynamicTabs: true,
 dynamicTabsAlign: "center",
 dynamicTabsPosition: "top",
 externalTriggerSelector: "a.xtrig",
 firstPanelToLoad: 1,
 panelTitleSelector: "h2.title",
 slideEaseDuration: 1000,
 slideEaseFunction: "easeInOutExpo"
 }, settings);

 return this.each(function(){

 var eventId = 0;
 var slider = JQ(this);

 var panelWidth = 802;
 var panelCount = JQ('.panel').size();
 var panelContainerWidth = panelWidth*panelCount;
 var navClicks = 0; // Used if autoSlideStopWhenClicked = true

 // Surround the collection of panel divs with a container div (wide enough for all panels to be lined up end-to-end)
 JQ('.panel').wrapAll('<div class="panel-container"></div>');
 // Specify the width of the container div (wide enough for all panels to be lined up end-to-end)
 JQ(".panel-container").css({ width: panelContainerWidth });

 // Specify the current panel.
 // If the loaded URL has a hash (cross-linking), we're going to use that hash to give the slider a specific starting position...
 if (settings.crossLinking && location.hash && parseInt(location.hash.slice(1)) <= panelCount) {
 var currentPanel = parseInt(location.hash.slice(1));
 var offset = - (panelWidth*(currentPanel - 1));
 JQ('.panel-container', slider).css({ marginLeft: offset });
 // If that's not the case, check to see if we're supposed to load a panel other than Panel 1 initially...
 } else if (settings.firstPanelToLoad != 1 && settings.firstPanelToLoad <= panelCount) {
 var currentPanel = settings.firstPanelToLoad;
 var offset = - (panelWidth*(currentPanel - 1));
 JQ('.panel-container', slider).css({ marginLeft: offset });
 // Otherwise, we'll just set the current panel to 1...
 } else {
 var currentPanel = 1;
 };


 // If we need a tabbed nav
 JQ('#coda-nav-' + sliderCount + ' a').each(function(z) {
 // What happens when a nav link is clicked
 JQ(this).bind("click", function() {
 clearTimeout(eventId);
 eventId = setTimeout(autoSlide,settings.autoSlideInterval);
 navClicks++;
 JQ(this).addClass('current').parents('ul').find('a').not(JQ(this)).removeClass('current');
 offset = - (panelWidth*z);
 alterPanelHeight(z);
 currentPanel = z + 1;
 JQ('.panel-container', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
 if (!settings.crossLinking) { return false }; // Don't change the URL hash unless cross-linking is specified
 });
 });


 // Specify which tab is initially set to "current". Depends on if the loaded URL had a hash or not (cross-linking).
 if (settings.crossLinking && location.hash && parseInt(location.hash.slice(1)) <= panelCount) {
 JQ("#coda-nav-" + sliderCount + " a:eq(" + (location.hash.slice(1) - 1) + ")").addClass("current");
 // If there's no cross-linking, check to see if we're supposed to load a panel other than Panel 1 initially...
 } else if (settings.firstPanelToLoad != 1 && settings.firstPanelToLoad <= panelCount) {
 JQ("#coda-nav-" + sliderCount + " a:eq(" + (settings.firstPanelToLoad - 1) + ")").addClass("current");
 // Otherwise we must be loading Panel 1, so make the first tab the current one.
 } else {
 JQ("#coda-nav-" + sliderCount + " a:eq(0)").addClass("current");
 };

 // Trigger autoSlide
 if (settings.autoSlide) {
 slider.ready(function() {
 eventId = setTimeout(autoSlide,settings.autoSlideInterval);
 });
 };

 function alterPanelHeight(x) {
 if (settings.autoHeight) {
 panelHeight = JQ('.panel:eq(' + x + ')', slider).height()
 slider.animate({ height: panelHeight }, settings.autoHeightEaseDuration, settings.autoHeightEaseFunction);
 };
 };

 function autoSlide() {
 if (navClicks == 0 || !settings.autoSlideStopWhenClicked) {
 if (currentPanel == panelCount) {
 var offset = 0;
 currentPanel = 1;
 } else {
 var offset = - (panelWidth*currentPanel);
 currentPanel += 1;
 };
 alterPanelHeight(currentPanel - 1);
 // Switch the current tab:
 JQ('.coda-nav').find('a').removeClass('current');
 JQ('.tab'+currentPanel).find('a').addClass('current');
 // Slide:
 JQ('.panel-container', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
 eventId = setTimeout(autoSlide,settings.autoSlideInterval);
 };
 };


 // Kill the preloader
 JQ('.panel').show().end().find("p.loading").remove();
 slider.removeClass("preload");

 sliderCount++;

 });
};
