var ie6 = null;

/*@cc_on
  @if (@_jscript_version < 5.7) ie6 = true;
  @elif (@_jscript_version == 5.7)
   if (!window.XMLHttpRequest) ie6 = true;
  @end
@*/

/* Copyright (c) 2007 Ganeshji Marwaha */
(function($) {

$.fn.jCarouselLite = function(o) {
    o = $.extend({
        btnPrev: null,
        btnNext: null,
        btnGo: null,
        mouseWheel: false,
        auto: null,
        speed: 400,
        easing: null,
        vertical: false,
        circular: true,
        visible: 1,
        start: 0,
        scroll: 1,
        beforeStart: null,
        afterEnd: null,
        buttons:null
    }, o || {});

    var version = null;

    return this.each(function() {
        var running = false, animCss=o.vertical?"top":"left", sizeCss=o.vertical?"height":"width";
        var target = $(this), stage = $(".stage", target), nodes = $(".node", stage), tl = nodes.size(), v = o.visible;
        var autoSlide = o.auto != null;
        var interval = null;

        if(o.circular) {
          stage.prepend(nodes.slice(tl-v-1+1).clone()).append(nodes.slice(0,v).clone());
          o.start += v;
        }

        var node = $(".node", stage), itemLength = node.size(), curr = o.start;
        node.css({float: o.vertical ? "none" : "left"});
        stage.css({position: "relative", "z-index": "1"});

        var nodeSize = o.vertical ? height(node) : width(node); // full node size (incl margin)-Used for animation
        var stageSize = nodeSize * itemLength;                  // size of full stage (total length, not just for the visible items)

        stage.css(sizeCss, stageSize+"px").css(animCss, -(curr*nodeSize));
        node.css({width: node.width(), height: node.height()});

        if (o.buttons)
          o.btnPrev =  $("<span />").css({background:"url("+o.buttons[0]+")", zIndex:10, position:'absolute', width:o.buttons[1]+"px", height:o.buttons[2]+"px", left:target.offset().left + o.buttons[3] +"px", top:target.offset().top + o.buttons[4] +"px", cursor:"pointer", display:"inline"}).attr({title:"Previous Slide "}).appendTo("body");
  
        if (o.buttons)
          o.btnNext = $("<span />").css({background:"url("+o.buttons[0]+") 0 -"+ o.buttons[2] +"px no-repeat", zIndex:10, width:o.buttons[1]+"px", height:o.buttons[2]+"px" , position:'absolute', left:target.offset().left + target.width() + o.buttons[5] +"px", top:target.offset().top + o.buttons[6] +"px", cursor:"pointer"}).attr({title:"Next Slide "}).appendTo("body");

        if (o.buttons)
          $(window).bind("resize", function() {
            o.btnPrev.css({left:target.offset().left + o.buttons[3] +'px'});
            o.btnNext.css({left:target.offset().left + target.width() + o.buttons[5] +'px'});
          });

        // disable auto sliding while mouse over stage
        stage.hover(function() { // onMouseOver
              if(autoSlide) stopAutoSlide(interval);
            },
          function() {           // onMouseOut
              if(autoSlide) interval = startAutoSlide();
          });
        // toggle auto sliding when clicking stage 
        stage.click(function() {
          o.auto ? autoSlide = null : autoSlide = true;
          });

        if(o.btnPrev)
            $(o.btnPrev).click(function() {
                var result = go(curr+o.scroll);
                if (autoSlide) interval = resetAutoslide(interval);
                return result;
            });

        if(o.btnNext)
            $(o.btnNext).click(function() {
                var result = go(curr-o.scroll);
                if (autoSlide) interval = resetAutoslide(interval);
                return result;
            });

        if(o.btnGo)
            $.each(o.btnGo, function(i, val) {
                $(val).click(function() {
                    return go(o.circular ? o.visible+i : i);
                });
            });

        if(o.mouseWheel && target.mousewheel)
            target.mousewheel(function(e, d) {
                return d>0 ? go(curr-o.scroll) : go(curr+o.scroll);
            });

        if(autoSlide) interval = startAutoSlide();

        function vis() {
            return node.slice(curr).slice(0,v);
        }

        function startAutoSlide() {
          return setInterval(function() { go(curr - o.scroll); }, o.auto + o.speed);
        }
        
        function stopAutoSlide(interval) {
          clearInterval(interval);
        }
        
        function resetAutoslide(interval) {
          if (interval) {
            clearInterval(interval);
            return startAutoSlide();
          }
          return null;
        }

        function go(to) {
            if(!running) {
                if(o.beforeStart) o.beforeStart.call(this, vis());

                if(o.circular) {            // If circular we are in first or last, then goto the other end
                    if(to<=o.start-v-1) {           // If first, then goto last
                        stage.css(animCss, -((itemLength-(v*2))*nodeSize)+"px");
                        // If "scroll" > 1, then the "to" might not be equal to the condition; it can be lesser depending on the number of elements.
                        curr = to==o.start-v-1 ? itemLength-(v*2)-1 : itemLength-(v*2)-o.scroll;
                    } else if(to>=itemLength-v+1) { // If last, then goto first
                        stage.css(animCss, -( (v) * nodeSize ) + "px" );
                        // If "scroll" > 1, then the "to" might not be equal to the condition; it can be greater depending on the number of elements.
                        curr = to==itemLength-v+1 ? v+1 : v+o.scroll;
                    } else curr = to;
                } else {                    // If non-circular and to points to first or last, we just return.
                    if(to<0 || to>itemLength-v) return;
                    else curr = to;
                }                           // If neither overrides it, the curr will still be "to" and we can proceed.

                running = true;

                stage.animate(
                    animCss == "left" ? { left: -(curr*nodeSize) } : { top: -(curr*nodeSize) } , o.speed, o.easing,
                    function() {
                        if(o.afterEnd) o.afterEnd.call(this, vis());
                        running = false;
                    }
                );
                // Disable buttons when the carousel reaches the last/first, and enable when not
                if(!o.circular) {
                    $(o.btnPrev + "," + o.btnNext).removeClass("disabled");
                    $((curr-o.scroll<0 && o.btnPrev)
                        || (curr+o.scroll > itemLength-v && o.btnNext)
                        || []
                     ).addClass("disabled");
                }
            }
            return false;
        };
    });
};

function css(el, prop) {
    return parseInt($.css(el[0], prop)) || 0;
};
function width(el) {
    return  el[0].offsetWidth + css(el, 'marginLeft') + css(el, 'marginRight');
};
function height(el) {
    return el[0].offsetHeight + css(el, 'marginTop') + css(el, 'marginBottom');
};
})(jQuery);

/* Smooth Navigational Menu - By Dynamic Drive DHTML code library: http://www.dynamicdrive.com/ */
var ddsmoothmenu = {
  transition: {overtime:200, outtime:80}, // duration of slide in/out animation, in milliseconds

  buildmenu:function($, settings) {
    var mainMenu = $("#"+settings.mainMenuId+">ul")
    mainMenu.parent().get(0).className = settings.classname

    // calculate the size of the submenus from the size of the main menu
    mainMenu.find("ul.submenu>li").each(function() {
      var li = $(this);
      if (li.parent().parent().parent().attr("class")=="menu") {
        li.css({width:li.parent().parent().css("width")});
        adjustLinkSize(li);
      } else {
         li.css({width:li.parent().parent().css("width")});
         adjustLinkSize(li);
      }
    });
    
    var entries = mainMenu.find("ul").parent()
    entries.hover (
      function(e) {
        $(this).children('a:eq(0)').addClass('selected')
      },
      function(e) {
        $(this).children('a:eq(0)').removeClass('selected')
      }
    )
    entries.each(function(i) {
      var entry = $(this).css({zIndex: 100-i}) // reference current LI header
      var subMenu = $(this).find('ul:eq(0)').css({display:'block'})

      this._dimensions = {w:this.offsetWidth, h:this.offsetHeight, subulw:subMenu.outerWidth(), subulh:subMenu.outerHeight()}
      this.istopheader = entry.parents("ul").length==1? true : false // is top level header?
      subMenu.css({top:this.istopheader && settings.orientation!='v'? this._dimensions.h+"px" : 0, minWidth:$(this).innerWidth()})
      entry.hover(
        function(e) {
          var $targetul = $(this).children("ul:eq(0)")
          this._offsets = {left:$(this).offset().left, top:$(this).offset().top}
          var menuleft = this.istopheader && settings.orientation!='v'? 0 : this._dimensions.w
          menuleft = (this._offsets.left+menuleft+this._dimensions.subulw>$(window).width())? (this.istopheader && settings.orientation!='v'? -this._dimensions.subulw+this._dimensions.w : -this._dimensions.w) : menuleft //calculate this sub menu's offsets from its parent
          if ($targetul.queue().length <= 1) { // if 1 or less queued animations
            $targetul.css({left:menuleft+"px"}).animate({height:'show',opacity:'show'}, ddsmoothmenu.transition.overtime);

          }
        },
        function(e) {
          var $targetul = $(this).children("ul:eq(0)")
          $targetul.animate({height:'hide', opacity:'hide'}, ddsmoothmenu.transition.outtime);
        }
      )
    })

    mainMenu.find("ul").css({display:'none', visibility:'visible'})
    

    function adjustLinkSize(rLink) {
      rLink.children("a").each(function(i) {
          var insets = parseFloat($(this).css("padding-left")) +
                       parseFloat($(this).css("padding-right") +
                       parseFloat(rLink.css("border-left-width")) +
                       parseFloat(rLink.css("border-right-width")));
          $(this).css({width:(parseFloat(rLink.css("width")) - insets) + "px"});
        });
    };
  },

  init:function(settings) {
    var target = $("#"+settings.target)
    var height = target.height();

    // there is no event for font size changes, as a workaround we therefore
    // install a custom handler that polls for component height changes
    setInterval(function() {
        if (target.height() != height) {
          ddsmoothmenu.buildmenu($, settings);
          height = target.height();
        }
      }, 1000);
    
    $(document).ready(function($){
      ddsmoothmenu.buildmenu($, settings);
    })
  }
}

$(document).ready(function() {
/*
  if (!ie6)
    $(".hall").jCarouselLite({
        auto: 7000
//        buttons: ["/images/highlights-buttons.png", 42, 48, -42, 130, 0, 130]
    });
*/

  ddsmoothmenu.init({
    mainMenuId:  "smoothmenu",
    orientation: "h",
    classname:   "menu",
    target   :   "header"
  });

  var tabindex = 1;

  // style form buttons, if JavaScript is disabled, the platform specific look is used
  $('input[type="submit"]').each(function() {
    var form = $(this).parent();
    while (form.get(0).tagName != 'FORM') form = form.parent(); // get the enclosing <form> tag

    // control tab order - important for the submit button!
    form.find(":input").each(function(i, val) {
      var field = $(this);
      if ((field.attr("type") != "hidden") && (field.attr("type") != "submit")) {
        field.attr("tabindex", tabindex);
        tabindex = tabindex + 1;
      }
    });

    var field = $(this);
    var text = field.text() || field.val();
    var button = $('<a>').insertAfter(this)
                         .addClass(this.className)
                         .addClass("btn")
                         .attr("tabindex", tabindex)
                         .text('')
                         .prepend('<i></i>')
                         .append($('<span>')
                         .text(text)
                         .append('<i></i><span></span>'));
    button.click(function(e) {
        // we want to have the value of the submit button included, as server
        // side form processing might rely on this information. Unfortunately,
        // IE does not allow to alter name and type attributes, we therefore
        // need to create a new field with the submit values
        var hiddenField = $('<input type="hidden" '
                          + 'name="' + field.attr("name") + '" '
                          + 'value="' + field.attr("value") +'">');
        hiddenField.insertBefore(button);

        form.submit();
      });

    field.remove();
  });

  if (ie6) return;

  var sidebarOffset       = $("#sidebar").offset().top;
  var sidebarTopAttribute = $("#sidebar").css("top");
  var sidebarDistance     = $("#sidebar").offset().left - ($("#text")
                                         .offset().left + $("#text").width());

  $(window).bind("scroll", function(e) {
    var sidebarTop     = $("#sidebar").offset().top;
    var sidebarHeight  = $("#sidebar").height();
    var sidebarLeft    = $("#sidebar").offset().left;
    var viewportHeight = window.innerHeigh || (document.documentElement && document.documentElement.clientHeight) ||  document.body.clientHeight;
    var pageOffset     = window.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || document.body.scrollTop;;
    var footerTop      = ($("#boxes") && $("#boxes").offset().top) || $("#footer").offset().top;
    
    if (pageOffset + sidebarHeight > footerTop) {
      $("#sidebar").css("position", "fixed");
      $("#sidebar").css("left", sidebarLeft);
      $("#sidebar").css("top", footerTop - (pageOffset + sidebarHeight));
    } else if (pageOffset > sidebarOffset) {
      if (sidebarHeight > viewportHeight) {
        $("#sidebar").css("position", "fixed");
        $("#sidebar").css("left", sidebarLeft);
        $("#sidebar").css("top", viewportHeight - sidebarHeight);
      } else {
        $("#sidebar").css("position", "fixed");
        $("#sidebar").css("left", sidebarLeft);
        $("#sidebar").css("top", "0");
      }
    } else {
      $("#sidebar").css("position", "relative");
      $("#sidebar").css("left", "0");
      $("#sidebar").css("top", sidebarTopAttribute);
    }
  });

  $(window).bind("resize", function(e) {
    if ($("#sidebar").css("position") == "fixed") {
      $("#sidebar").css("left", $("#text").offset().left + $("#text").width() + sidebarDistance);
    }
  });
});

