/**
  * @category
  * @package
  * @author		Bilal Cinarli
  * @copyright	2005 - 2010 ICON Perception Management Co.
  * @license
  * @version
  * @filesource
  * @link		http://www.iconpm.com
  * @see
  * @since
  **/

(function($){
    $.fn.iconAccordionMenu = function(options){
        var opts = $.extend({}, $.fn.iconAccordionMenu.defaults, options);

        return this.each(function(){
            var $menu = $(this);
			var o = opts;

			var header = o.header;
			var close = o.close;
            var closeOther = o.closeOther;
            var active = o.active;
            var content = o.content;

            $menu.find(content).hide();
            $menu.find(active + ' ' + content).show();

            $menu.find(header).click(function(){
                var submenu = $(this).parent().parent().find(content);

                if(submenu.is(':visible') && close == true){
                    submenu.slideUp();
                    $menu.find('.current').removeClass('current');
                }

                else{
                    if(closeOther == true){
                        $menu.find('.current').removeClass('current');
                    }
                    submenu.slideDown();
                    $(this).parent().addClass('current');
                }

                return false;
            });
        });
    },

   	$.fn.iconAccordionMenu.defaults = {
		close: true,
        closeOther: true,
        header: 'span.category',
        content: 'ul',
        active: '.current'
	};
})(jQuery);


/**
  * sample
  * <span title="Tooltip description" class="tooltip">Hover</span>
  * $(".tooltip").ntToolTip();
  * $(".tooltip-right").ntToolTip({ placement: 'right' });
  **/

(function($){
    $.fn.ntToolTip = function(options) {
        var opts = $.extend({}, $.fn.ntToolTip.defaults, options);

        return this.each(function() {
            var $element = $(this);
            var o = opts;
            var placement = o.placement;
            var desc, ttHeight, moveHeight;

            var tooltipBox = '<div class="ntToolTip">\
                        <div class="top-row">\
                            <div class="left corner"><\/div>\
                            <div class="row"><\/div>\
                            <div class="right corner">\
                        <\/div>\
                        <div style="clear:both"><\/div>\
                        <div class="ntToolTip-content">text<\/div>\
                        <div class="bottom-row">\
                            <div class="left corner"><\/div>\
                            <div class="row"><\/div>\
                            <div class="right corner">\
                        <\/div>\
                        <div style="clear:both"><\/div>\
                        <div class="arrow ' + placement + '"><\/div>\
                        <\/div>';

            $element.hover(
                function(){
                    var offset = $element.offset();
                    var left = offset.left;
                    var top = offset.top;

                    desc = $(this).attr('title');
                    $(this).attr('title', '');
                    $('body').append(tooltipBox);
                    $(".ntToolTip-content").html('<span>' + desc + '<\/span>');
                    var ttWidth =  $(".ntToolTip-content span").width();

                    $(".ntToolTip-content, .ntToolTip .row").css('width', ttWidth);

                    ttHeight = $(".ntToolTip").height();
                    var arrow = $(".ntToolTip .arrow").height();
                    var offsetTop = top - ttHeight - arrow - 2;
                    moveHeight = '+=' + (arrow + 1);

                    if(placement == 'right'){
                        var overAllToolTipWidth = $(".ntToolTip").width();
                        var objectWidth = $(this).width();

                        left = left - (overAllToolTipWidth - objectWidth);
                    }

                    $(".ntToolTip").css({left : left, top: offsetTop, opacity: 0}).animate({ opacity: 1, top : moveHeight });
                },
                function(){
                    $(".ntToolTip").remove();
                    $(this).attr('title', desc);
                }
            );
        });
    },
    $.fn.ntToolTip().defaults = {
		placement: 'left' // left|right
	};
})(jQuery);

(function($){
    $.fn.iconuiMediaRotator = function(options){
        var opts = $.extend({}, $.fn.iconuiMediaRotator.defaults, options);

        return this.each(function(){
            var $container = $(this);
			var o = opts;

            var $pages = $container.find(o.pages);
            var $navigation = $container.find(o.navigation);
            var $pageNavigations = $navigation.find('a');
            var currentClass = o.currentClass;
            var currentClassName = currentClass.substr(1);
            var nextButton = o.nextButton;
            var previousButton = o.previousButton;
            var playPauseButton = o.playPauseButton;
            var transition = o.transition;
            var transitionSpeed = o.transitionSpeed;

            var $nextPreButtons = $container.find(nextButton + ', ' + previousButton);
            var $playPauseButton = $container.find(playPauseButton);

            var autoSlide = o.autoSlide;
            var slideShowDelay = o.slideShowDelay;

            var thePage, timer, currentSlide, nextSlide, $e, currentIndex, nextIndex, animating = false;

            $pages.siblings(o.pages).hide().first().show().addClass(currentClassName);
            $pageNavigations.first().addClass(currentClassName).parent().addClass(currentClassName).parent().hide();
            currentSlide = $pages.filter(currentClass);

            var navLength = $pageNavigations.length;

            if(navLength > 1){
                $navigation.show();
                if(autoSlide == true){
                    startSlideShow();

                    $pages.hover(
                        function(){
                            stopSlideShow();
                        },
                        function(){
                            startSlideShow();
                        }
                    );
                }
            }

            // give tab-key navigation support
            $pageNavigations.click(function(){
               this.focus();
               return false;
            });

            $pageNavigations.focus(function(){
                $e = $(this);
                if(!$e.hasClass(currentClassName)){
                    animatePage($e);
                }
                resetSlideShow();
            });

            $nextPreButtons.click(function(){
                if($(this).is(nextButton)){
                    navigateNext('next');

                }
                else{
                    navigateNext('prev');
                }
                resetSlideShow();
                return false;
            });

            if($playPauseButton.length == 1){
                $playPauseButton.click(function(){
                    if($(this).hasClass('pause')){
                        stopSlideShow();
                        $(this).removeClass('pause');
                    }
                    else{
                        startSlideShow();
                        $(this).addClass('pause');
                    }
                });
            }

            function navigateNext(direction){
                currentIndex = $pageNavigations.filter(currentClass).parent().index();
                if(direction == 'prev'){
                    nextIndex = currentIndex - 1;
                    if(nextIndex < 0){
                        nextIndex = navLength - 1;
                    }
                }
                else{
                    nextIndex = currentIndex + 1;
                    if(nextIndex >= navLength){
                        nextIndex = 0;
                    }
                }

                $e = $navigation.find('li:eq(' + nextIndex + ') a');

                animatePage($e);
            }

            function slideshow(){
                navigateNext('next');
            }

            function stopSlideShow(){
                clearInterval(timer);
            }

            function startSlideShow(){
                timer = setInterval(slideshow, slideShowDelay);
            }

            function resetSlideShow(){
                if(autoSlide == true){
                    stopSlideShow();
                    startSlideShow();
                }
            }

            // this function do the all job for showing next slide with or without transition effect
            function animatePage($el){
                if(animating == false){
                    animating = true;
                    nextSlide = $($el.attr('href'));
                    currentSlide = $pages.filter(currentClass);

                    currentSlide.removeClass(currentClassName);
                    nextSlide.addClass(currentClassName);
                    $pageNavigations.removeClass(currentClassName).parent().removeClass(currentClassName);
                    $el.addClass(currentClassName).parent().addClass(currentClassName);

                    if(transition == 'fade'){
                        currentSlide.fadeOut(transitionSpeed);
                        nextSlide.fadeIn(transitionSpeed, function(){
                            animating = false;
                        });
                    }

                    else if(transition == 'vslide'){
                        currentSlide.slideUp(transitionSpeed);
                        nextSlide.slideDown(transitionSpeed, function(){
                            animating = false;
                        });
                    }

                    else if(transition == 'hslide'){
                        var c = $pages.index(currentSlide);
                        var n = $pages.index(nextSlide);

                        var containerW = currentSlide.parent().width();

                        var cM, nM;

                        // slide from right to left
                        if(n > c){
                            cM = -containerW;
                            nM = containerW;
                        }

                        // slide from left to right
                        else{
                           cM = containerW;
                           nM = -containerW;
                        }

                        currentSlide.animate({ left: cM }, transitionSpeed, 'linear');
                        nextSlide.css({ left: nM }).show().animate({ left: 0 }, transitionSpeed, 'linear', function(){
                            currentSlide.hide().css({ left: 0 });
                            animating = false;
                        });
                    }

                    else{
                        currentSlide.hide();
                        nextSlide.show().queue(function(){
                            animating = false;
                            $(this).dequeue();
                        });
                    }

                    if($playPauseButton.length == 1){
                        $playPauseButton.addClass('pause');
                    }
                }
            }
        });
    },

   	$.fn.iconuiMediaRotator.defaults = {
		pages: '.slide',
        navigation: '.slide-nav',
        currentClass: '.current',
        nextButton: '.next',
        previousButton: '.prev',
        playPauseButton: '.play-pause',
        transition: 'none', // fade|hslide|vslide|none
        transitionSpeed: 600, // slow|fast|1000|5000 etc.
        autoSlide: true,
        slideShowDelay: 6000
	};
})(jQuery);

(function($){
    $.fn.iconuiKwick = function(options){
        var opts = $.extend({}, $.fn.iconuiKwick.defaults, options);

        return this.each(function(){
            var o = opts;
            var $el = $(this);

            var $items = $el.find('li');
            o.initialWidth = opts.initialWidth;
            o.closedWidth = opts.closedWidth;
            o.openedWidth = opts.openedWidth;
            o.speed = opts.speed;

           var i = 0;
           var z = 99;
           $items.each(function(){
                $(this).css('left', i*o.initialWidth);
                $(this).css('z-index', z);
                i++;
                z--;
           });
           
           $items.each(function(){
                if($(this).hasClass('current')){
             		var index = $(this).index();
               		var newLeft = index * o.closedWidth;
                    $(this).animate({ width: o.openedWidth, left: newLeft }, o.speed, function(){
                        $(this).addClass('current');
                        $(this).find(".kwick-content").fadeIn(400)
                    })
                    var $siblings = $(this).siblings();
                    $siblings.removeClass('current').each(function(){
                    	var siblingIndex = $(this).index();
                    	var newSiblingLeft = siblingIndex * o.closedWidth;
                    	if(siblingIndex > index){
                    		newSiblingLeft += o.openedWidth - o.closedWidth;
                    	}
                    	$(this).animate({ width: o.closedWidth, left: newSiblingLeft }, o.speed);
                    });
               }
           });

           $items.click(function(){
           		var index = $(this).index();
           		var newLeft = index * o.closedWidth;
                $(this).animate({ width: o.openedWidth, left: newLeft }, o.speed, function(){
                    $(this).addClass('current');
                    $(".kwick-content").fadeOut(400);
                    $(this).find(".kwick-content").fadeIn(400)
                })
                var $siblings = $(this).siblings();
                $siblings.removeClass('current').each(function(){
                	var siblingIndex = $(this).index();
                	var newSiblingLeft = siblingIndex * o.closedWidth;
                	if(siblingIndex > index){
                		newSiblingLeft += o.openedWidth - o.closedWidth;
                	}
                	$(this).animate({ width: o.closedWidth, left: newSiblingLeft }, o.speed);
                });
           });
        });
    },

    $.fn.iconuiKwick.defaults = {
        initialWidth: 240,
        closedWidth: 30,
        openedWidth: 870,
        speed: 1000 // fast, slow, normal, 1000, 2000 etc.
    }
})(jQuery);
