﻿/* slider with index */
$.fn.slideShow = function(o) {
	o = o || {};
	return this.each(function() {
		var onShowCallback = o.onShow;
		var onHideCallback = o.onHide;
		var slideShow = $(this);
		var p = slideShow.parent();
		var index = $(o.index || "div.slideShowIndex",p); //slideShow.next
		var slideShowTimer = null;
		var current = null;
		var current_but = null;
		var current_index = 0;
		var counter = 0;
		var time = o.time || 10000;
		var IsMouseOver = false; //true if mouse is over the entire box
		var IsSwitching = false; //true if in process of switching from one slide to the next
		var liSet = slideShow.children("li");
		var winIsLoaded = false; //when the window has finished loading, the variable is set to true
		var shouldGoToNext = false; //notification that the timer wants to show the next item but it waits for the page to load
		if (liSet.length > 0)
			$(liSet[0]).show();
		if (liSet.length < 2) {
			liSet.show();
			if (onShowCallback)
				onShowCallback(liSet, current_index);
			return;
		}
		function setTimer() {
			shouldGoToNext = true;
			if (winIsLoaded)
				doSlideShow();
			//return setTimeout(fn, t_interval);
			//return null;
		}
		function hide() {
			if (onHideCallback)
				onHideCallback(current, current_index);
			if (current) current.hide();
			if (current_but) current_but.removeClass("selected");
		}
		function next(nextL, nextB, nextI) {
			current = nextL;
			current_but = nextB;
			current_index = nextI;
		}
		function show() {
			if (slideShowTimer)
				clearTimeout(slideShowTimer);
			current_but.addClass("selected");
			current.show();
			if (onShowCallback)
				onShowCallback(current, current_index);
			//don't set the timer if mouse is over the big div
			if (IsMouseOver)
				slideShowTimer = null;
			else
				slideShowTimer = setTimeout(setTimer, time);
		}
		function switchToNext(nextL, nextB, nextI) {
			if (IsSwitching) return; //don't do a thing if already in the process of switching
			IsSwitching = true; //is in process of switching to another slide
			var isIE = false;
			if ($.browser.msie || (navigator && navigator.appName && navigator.appName.browserName == "Microsoft Internet Explorer")) {
				isIE = true;
			}
			try {
				if (isIE) {
					hide();
					next(nextL, nextB, nextI);
					show();
					IsSwitching = false; //switch has ended
				} else if (current) {
					current.fadeOut("slow", function() {
						hide();
						next(nextL, nextB, nextI);
						current.fadeIn("slow", function() {
							//fix for IE 7 when choosing to use fade
							//if ($.browser.msie) this.style.removeAttribute('filter');
							show();
							IsSwitching = false; //switch has ended
						});
					});
				} else {
					next(nextL, nextB, nextI);
					current.fadeIn("slow", function() {
						//fix for IE 7 when choosing to use fade
						//if ($.browser.msie) this.style.removeAttribute('filter');
						show();
						IsSwitching = false; //switch has ended
					});
				}
			} catch (e) {
				IsSwitching = false; //switch has ended
				alert("Error while switching to next slide, message: " + e.message);
			}
		}
		function onMouseEnter() {
			IsMouseOver = true;
			//on mouse enter over the slideshow div stop the timer for the slideshow
			if (slideShowTimer) {
				clearTimeout(slideShowTimer);
				slideShowTimer = null;
			}
		}
		function onMouseLeave() {
			if (slideShowTimer)
				clearTimeout(slideShowTimer);
			slideShowTimer = setTimeout(setTimer, time);
			IsMouseOver = false;
			//$("div.mouseOutInfo").text(++mouseOutCount);
			//alert("mouse out!");
		}
		var bIsFirst = true;
		liSet.each(function() {
			var li = $(this);
			if (bIsFirst) {
				bIsFirst = false;
				li.show();
			} else
				li.hide();
			var but = document.createElement("div");
			index.get(0).appendChild(but);
			but = $(but);
			but.html("" + (counter + 1));
			var cIndex = counter;
			but.click(function(e) {
				if (current_index != cIndex)
				//if (current && current != li)
					switchToNext(li, $(e.target), cIndex);
			}).mouseenter(onMouseEnter).mouseleave(onMouseLeave);
			counter++;
		});
		var mouseOutCount = 0;
		slideShow.mouseenter(onMouseEnter).mouseleave(onMouseLeave);
		//switch between the visible items once each 5 seconds
		function doSlideShow() {
			var buttons = index.children("div");
			var i, next_i = 0;
			for (i = 0; i < buttons.length; i++)
				if ($(buttons[i]).hasClass("selected")) {
				next_i = ((i + 1) % buttons.length);
				break;
			}
			switchToNext($(liSet[next_i]), $(buttons[next_i]), next_i);
		}
		$.windowLoaded(function() {
			//before starting the slideshow, going to the second item and so on,
			//first make sure that the page is fully loaded
			winIsLoaded = true;
			if (shouldGoToNext) doSlideShow();
		});
		doSlideShow();
		//slideShowTimer = setTimeout(doSlideShow, 100);
	});
}

function isBool(s) { return (/^\s*(true)|(yes)|1|(random)|(randomStart)\s*$/i).test(s); }
/* slider with paging */
$.fn.horizSlider = function (o) {
    o = o || {};
    return this.each(function () {
        var slider = $(this);
        var paging = slider.next(o.paging || "div.horizSliderPaging");
        var butPrev = paging.find(o.prev || "div.horizSliderPagePrev");
        var butNext = paging.find(o.next || "div.horizSliderPageNext");
        var clsHorizSliderPagePrevActive = (o.clsPrevA || "horizSliderPagePrevActive");
        var clsHorizSliderPageNextActive = (o.clsNextA || "horizSliderPageNextActive");
        var onShowCallback = o.onShow || function () { };
        var onHideCallback = o.onHide || function () { };
        var speed = o.speed || slider.attr("speed") || 1500;
        var randomStart = o.randomStart || false;
        var attrRandomStart = slider.attr("randomStart");
        if (attrRandomStart)
            randomStart = isBool(attrRandomStart);
        //transition can be slide or fade
        var transition = o.transition || slider.attr("transition") || "slide";

        var liSet = slider.children("li");
        var liCount = liSet.length;
        var currentIndex = (randomStart ? Math.floor(Math.random() * liCount) : 0); //random start position //0;
        onShowCallback($(liSet[currentIndex]).show(), currentIndex);
        if (liCount <= 1) {
            paging.hide();
            return;
        }
        function skipTo(nextIndex) {
            var nextIndex = nextIndex % liCount;
            if (nextIndex == currentIndex) return;
            var direction = (nextIndex > currentIndex ? 1 : -1);
            var cLi = $(liSet[currentIndex]);
            var nLi = $(liSet[nextIndex]);
            var liWidth = cLi.width();
            if (transition != "fade") {
                nLi.css("left", (direction * liWidth) + "px");
                nLi.show();
            }
            onShowCallback(nLi, nextIndex);
            liSet.stop(false, true);
            var prevCurrentIndex = currentIndex;
            currentIndex = nextIndex;
            if (currentIndex == 0)
                butPrev.removeClass(clsHorizSliderPagePrevActive);
            else
                butPrev.addClass(clsHorizSliderPagePrevActive);
            if (currentIndex == liCount - 1)
                butNext.removeClass(clsHorizSliderPageNextActive);
            else
                butNext.addClass(clsHorizSliderPageNextActive);
            if (transition == "fade") {
                cLi.fadeOut(speed/2, function () {
                    onHideCallback(cLi, prevCurrentIndex);
                    nLi.fadeIn(speed/2);  
                })
                if ( isIE() ) {
                    cLi.hide();
                    nLi.show();  
                };
            } else {//default transition is slide
                cLi.animate({ left: ((-1) * direction * liWidth) + "px" }, speed, "swing", function () {
                    onHideCallback(cLi, prevCurrentIndex);
                });
                nLi.animate({ left: "0px" }, speed, "swing");
            }
        }
        function moveNext(step) {
            if (!step) step = 1;
            var nextIndex = (currentIndex + step) % liCount;
            skipTo(nextIndex);
        }
        butNext.click(function () { if (butNext.hasClass(clsHorizSliderPageNextActive)) moveNext() });
        butPrev.click(function () { if (butPrev.hasClass(clsHorizSliderPagePrevActive)) moveNext(-1) });
        if (currentIndex > 0)
            butPrev.addClass(clsHorizSliderPagePrevActive);
        if (currentIndex < liCount - 1)
            butNext.addClass(clsHorizSliderPageNextActive);
        slider.data("horizSlider", { skipTo: skipTo });
    });
};

/* make tabs */
$.fn.makeTabs1 = function(o){
	o = o || {};
  return this.each(function() {
    var root = $(this);
    var TabsClass = o.tabs;
    var onShowCallback = o.onShow;
    var TabContentsClass = o.contents;
    var currentClass = o.current;
    var contentClass = o.content;
    var tabClass = o.tab;
    var index= o.index;
    var curIndex = o.curIndex;
    if(!o.index) index = 0;
    if(!o.curIndex) curIndex = 0;
    var contents = root.find(TabContentsClass+">li");
    function show(noFadeIn) {
      var curTabLi = $(contents.get(curIndex));
      var curTabContent = $(curTabLi.find(contentClass));
      var curTab = $(tabs.get(curIndex));
      if ( noFadeIn )
        curTabLi.show();
      else
        curTabLi.fadeIn('slow');
      curTab.addClass(currentClass);
      if ( onShowCallback )
        onShowCallback(curTab, curTabContent);
    }
    contents.each(function(){
      var lIndex = index++;
      var Tab = $(this).find(tabClass);
      Tab.click(function(){
        if (lIndex < contents.size() && lIndex != curIndex) {
          $(contents.get(curIndex)).fadeOut(function(){
            $(tabs.get(curIndex)).removeClass(currentClass);
            curIndex = lIndex;
            show();
          });
        }
      })
      Tab.detach();
      $('<li></li>').appendTo(root.find(TabsClass)).append(Tab);
      $(this).hide();
    })
    var tabs = root.find(TabsClass+">li "+tabClass);
    show(true);
	var root = $(this);
	var TabsClass = o.tabs;
	var onShowCallback = o.onShow;
	var TabContentsClass = o.contents;
	var currentClass = o.current;
	var contentClass = o.content;
	var tabClass = o.tab;
	var index= o.index;
    var curIndex = o.curIndex;
    if(!o.index) index = 0;
    if(!o.curIndex) curIndex = 0;
	var contents = root.find(TabContentsClass+">li");
	function show(noFadeIn) {
	  var curTabLi = $(contents.get(curIndex));
	  var curTabContent = $(curTabLi.find(contentClass));
	  var curTab = $(tabs.get(curIndex));
	  if ( noFadeIn )
		curTabLi.show();
	  else
		curTabLi.fadeIn('slow');
	  curTab.addClass(currentClass);
	  if ( onShowCallback )
		onShowCallback(curTab, curTabContent);
	}
	contents.each(function(){
	  var lIndex = index++;
	  var Tab = $(this).find(tabClass);
	  Tab.click(function(){
		if (lIndex < contents.size() && lIndex != curIndex) {
		  $(contents.get(curIndex)).fadeOut(function(){
			$(tabs.get(curIndex)).removeClass(currentClass);
			curIndex = lIndex;
			show();
		  });
		}
	  })
	  Tab.detach();
	  $('<li></li>').appendTo(root.find(TabsClass)).append(Tab);
	  $(this).hide();
	})
	var tabs = root.find(TabsClass+">li "+tabClass);
	show(true);
  })
}

$.urlParam = function(name) {
	var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
	if (!results) { return 0; }
	return results[1] || 0;
}

$.fn.makeTabs2 = function(o){
	o = o || {};
	if ( !o.tabs || !o.contents )
	return {};
  return this.each(function() {
	var root = $(this);
	var liTabs = root.find(o.tabs);
	var onShowCallback = o.onShow;
	var liTabContents = root.find(o.contents);
	var index=0;
	var curIndex = o.activeIndex||0;
	function show(noFadeIn) {
	  var curTabContent = $(liTabContents[curIndex]);
	  var curTab = $(liTabs[curIndex]);
	  if ( noFadeIn )
		curTabContent.show(function(){
		  if ( onShowCallback ) onShowCallback(curTab, curTabContent);
		});
	  else
		if ($.browser.msie) {
			curTabContent.show();
		}
		curTabContent.fadeIn('slow', function(){
		  if ( onShowCallback ) onShowCallback(curTab, curTabContent);
		});
	  curTab.addClass("current");
	}
	liTabs.each(function(){
	  var liIndex = index++;
	  var liTab = $(this);
	  if ( liIndex!=curIndex ) {
		//hide the rest but the first
		$(liTabContents[liIndex]).hide();
	  }
	  liTab.click(function(){
		if (liIndex < liTabContents.size() && liIndex != curIndex) {
		  if ($.browser.msie) {
			  $(liTabContents[curIndex]).hide();
		  }
		  $(liTabContents[curIndex]).fadeOut(function(){
			$(liTabs[curIndex]).removeClass("current");
			curIndex = liIndex;
			show();
		  });
		}
	  })
	})
	show(true);
  });
}



