/**
  * @category
  * @package	ICONtrol
  * @author		Bilal Cinarli
  * @copyright	2005 - 2010 ICON Perception Management Co.
  * @license
  * @version
  * @filesource
  * @link		http://www.iconpm.com
  * @see
  * @since
  **/
(function($){
    $.fn.iconuiFocus = function(options){
        return this.each(function(){
           var $el = $(this);
           var $label = $("label[for=" + $el.attr('id') + "]");

           if($el.val() != ''){
                $label.hide();
           }

           $el.focus(function(){
                $label.hide();
           });

           $el.blur(function(){
                if($el.val() == ''){
                    $label.show();
                }
           });
        });
    }
})(jQuery);

(function($){
    $.fn.iconuiAccordion = function(options){
        var opts = $.extend({}, $.fn.iconuiAccordion.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().find(content);

                if(submenu.is(':visible') && close == true){
                    submenu.slideUp();
                    $(this).removeClass('hover');
                }

                else{
                    if(closeOther == true){
                        $menu.find(content).slideUp();
                        $menu.find('.hover').removeClass('hover');
                    }
                    submenu.slideDown();
                    $(this).addClass('hover');
                }
            });
        });
    },

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

// makes all related area clickable according to associated target url
(function($){
    $.fn.iconuiClickable = function(options){
        var opts = $.extend({}, $.fn.iconuiClickable.defaults, options);

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

			var $linkContainer = $container.find(o.linkContainer);
			var target = o.target;
			var changeCursor = o.changeCursor;
            var location;

            if($linkContainer.length > 0)
            {
                if(changeCursor == true)
                {
                    $container.css('cursor', 'pointer');
                }

                if(target == 'default')
                {
                    target = $linkContainer.attr('target');
                }

                location = $linkContainer.attr('href');

                $container.click(function(){
                    if(target == '_blank')
                    {
                        window.open(location);
                    }
                    else{
                        window.location = location;
                    }
                });
            }
        });
    },

   	$.fn.iconuiClickable.defaults = {
		linkContainer: '.target',
        target: 'default', // when area clicked opens the window behaviour default|_self|_blank
        changeCursor: true // set the rollover cursor to pointer
	};
})(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 transition = o.transition;
            var transitionSpeed = o.transitionSpeed;

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

            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;
            });

            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');            
                $navigation.parent().find("span.currentnumber").html(nextIndex + 1);

                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();
                        });
                    }
                }
            }
        });
    },

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