/*!
 * ICONui, jQuery Based user interface library
 * http://iconpm.net/
 * http://jquery.com/
 *
 * Copyright 2010, ICON, Bilal Cinarli @ ICON Perception Management Co.
 * All rights reserved
 * Distributed only commercially and bind to unique website address.
 * Reproduction, redistribution or inclusion of the code in downloadable items
 * such as templates or themes (free or commercial), in whole or in part, is strictly forbidden.
 *
 *
 * Version 1.0
 *
 */

(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.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 showCount = o.showCount;

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

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

                    if(showCount == true && countText.length == 1){
                        countText.text(nextIndex + 1);
                    }
                }
            }
        });
    },

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

(function($){

    var selectedIndex = 0, selectedOpts = {}, selectedArray = [],
        imgRegExp = /\.(jpg|gif|png|bmp|jpeg)(.*)?$/i,
        img, cH, cW, dH, wH, scrollTop = 0;

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

        return this.each(function(){

        });
    };

   	$.fn.iconuiModal.defaults = {
		href: ""
	};

    $.iconuiModal = function(obj){

        var opts = typeof arguments[1] !== 'undefined' ? arguments[1] : {};

		selectedArray	= [];
		selectedIndex	= opts.index || 0;

		if ($.isArray(obj)) {
			for (var i = 0, j = obj.length; i < j; i++) {
				if (typeof obj[i] == 'object') {
					$(obj[i]).data('iconuiModal', $.extend({}, opts, obj[i]));
				} else {
					obj[i] = $({}).data('iconuiModal', $.extend({content : obj[i]}, opts));
				}
			}

			selectedArray = jQuery.merge(selectedArray, obj);

		} else {
			if (typeof obj == 'object') {
				$(obj).data('iconuiModal', $.extend({}, opts, obj));
			} else {
				obj = $({}).data('iconuiModal', $.extend({content : obj}, opts));
			}

			selectedArray.push(obj);
		}

		if (selectedIndex > selectedArray.length || selectedIndex < 0) {
			selectedIndex = 0;
		}


        iconuiModal_start();
    }

    $.iconuiModal.close = function(){
        return iconuiModal_close();
    }

    iconuiModal_start = function(){
        var obj	= selectedArray[ selectedIndex ],
            url,
            overlay;

        selectedOpts = $.extend({}, $.fn.iconuiModal.defaults, (typeof $(obj).data('iconuiModal') == 'undefined' ? selectedOpts : $(obj).data('iconuiModal')));

        url = selectedOpts.href;

        dH = $(document).height();
        wH = $(window).height();
        scrollTop = $(window).scrollTop();

        overlay = '<div id="iconuiModal"><div id="iconuiModal-overlay"><\/div><div id="iconuiModal-container"><div id="iconuiModal-content"><\/div><a id="iconuiModal-close">Kapat<\/a><\/div><\/div>';
        $("body").append(overlay);

        $("#iconuiModal-overlay").css({ opacity: 0.75, height: dH });

        iconuiModal_center();

        $("body").css('overflow-y', 'hidden');

        if(url.match(imgRegExp))
        {
            img = $('<img style="vertical-align: bottom;" />').attr('src', url);
            $("#iconuiModal").fadeIn('slow', function(){
                $("#iconuiModal-content").addClass('loading').append(img).queue(function(){
                    $(this).removeClass('loading');
                    $(this).dequeue();

                    iconuiModal_resize();
                });
            });
        }
        else{
            $("#iconuiModal").fadeIn('slow', function(){
                $("#iconuiModal-content").addClass('loading').load(url, '', function(data){
                    $(this).removeClass('loading');
                });
            });
        }

        $("#iconuiModal-close, #iconuiModal-overlay").live('click', function(){
            $("#iconuiModal").fadeOut('fast', function(){
                $(this).remove();
                $("body").css('overflow-y', 'auto');
            });
        });
    }

    iconuiModal_center = function(){
        scrollTop = $(window).scrollTop();

        $("#iconuiModal-container").css({ marginTop: scrollTop })
    }

    iconuiModal_resize = function(){
        if(img != null)
        {
            cW = img.width();
            cH = img.height();

            wH = $(window).height();
            var newTop = (wH / 2) + scrollTop;

            $("#iconuiModal-container").animate({ width: cW, height: cH, top: newTop, left: '50%', marginTop: '-' + (cH/2), marginLeft: '-' + (cW/2) });
            $("#iconuiModal-overlay").height($(document).height());
        }
    }

    $(window).resize(function(){
        scrollTop = $(window).scrollTop();
        $("#iconuiModal-container").css({ width: '100px', height: '100px' });
        iconuiModal_resize();
    })
})(jQuery);

