/*********
 * rotator plugin
 * author: Kamil Bednarz
 * data-create: 08-10-2011
 * *******/
/*fx
 swing
 easeInQuad
 easeOutQuad
 easeInOutQuad
 easeInCubic
 easeOutCubic
 easeInOutCubic
 easeInQuart
 easeOutQuart
 easeInOutQuart
 easeInQuint
 easeOutQuint
 easeInOutQuint
 easeInSine
 easeOutSine
 easeInOutSine
 easeInExpo
 easeOutExpo
 easeInOutExpo
 easeInCirc
 easeOutCirc
 easeInOutCirc
 easeInElastic
 easeOutElastic
 easeInOutElastic
 easeInBack
 easeOutBack
 easeInOutBack
 easeInBounce
 easeOutBounce
 easeInOutBounce
 */
var interavl_ID = null;
$(document).ready(function() {
    var stoprotate = $('.rotate-shorttext');
    var active = 1;
    var fx = ''
    var pause = 9000;
    var speed_animate_rotator = 0;
    var speed_animate_list = 400;
    var listElementsWidth = 190;
    var widthslide = 940;
    var galleryli = $(".rotate-true .show-container ul li");
    var lengthli = $(galleryli).length;
    var smallli = $(".navigation-rotate ul li");
    var smallc = $(smallli).length;
    var galleryul = $(".rotate-true .show-container ul");
    var smallcul = $(".navigation-rotate ul");
    var imagemask = $('.image-mask,.media2');
    var lightness = $(".lightness");
    var lightnessspeed = 1500;
    var rotatewrapper = $("#rotate-wrapper");
    var textBannerFadeSpeed = 1500;
    
/*buttons action*/ 

    var prev = $(".prev");
    var next = $(".next");   
    function showall(jQuery){
            $(prev).show();
            $(next).show();
    }
     function NhPs(jQuery){
            $(next).hide();
            $(prev).show();
    }
     function NsPh(jQuery){
            $(next).hide();
            $(prev).show();
    }
/*******/   
/******init functions*******/     
    if(smallc > 5) {

        $('	<div class="nav_small prev">prev</div><div rel="0" class="nav_small next">next</div>').insertAfter('.show-container');
        $('.next').show();
    }

    $(lightness).css('top', '0');
    $(lightness).animate({
        opacity : 0
    }, lightnessspeed, function() {
        $(imagemask).fadeOut(textBannerFadeSpeed);
        $(lightness).css({
            'top' : '-700px',
            'opacity' : "1"
        });
        return false
    });

    $(galleryli).each(function(i) {
        var widthli_elements = $(this).width();
        $(this).data("idx", i + 1);
        $(this).data("rotate", i + 1);
        $(this).attr("num", i + 1);

    });
    $(galleryul).css('width', lengthli * widthslide);

    $(smallli).each(function(i) {
        $(this).data("idx", i + 1);
        $(this).attr("current", i + 1);
    });

    $(smallcul).css('width', smallc * listElementsWidth);
    $(stoprotate).click(function() {
        window.clearInterval(interavl_ID);
    });
    
/*******rotation on tabs*********/
    $(smallli).click(function() {
        window.clearInterval(interavl_ID);

        var lithis = $(this).data("idx") + 1;

        var slidewidth = $(this).data("idx") - 1;
        $(smallli).children('div').removeClass('bg');
        $(this).children('div').addClass('bg');
        $('div.show-container ul li').css('visibility','hidden');
        $('div.show-container ul li').eq(slidewidth).css('visibility','visible');
        $(imagemask).show();
        $(".lightness").css('top', '0px');
        $(galleryli+'[num='+(slidewidth + 1)+'] .lightness').animate({
            opacity : 0
        }, lightnessspeed, function() {
            $(galleryli+'[num='+(slidewidth + 1)+']').find(imagemask).fadeOut(textBannerFadeSpeed);
            $(".lightness").css({
                'top' : '-700px',
                'opacity' : "1"
            });
            return false
        });
        $(galleryul).animate({
            left : "-" + slidewidth * widthslide
        }, speed_animate_rotator, fx, function() {
        });
        $(next).attr('rel', lithis - 1);
        $(galleryli).data("rotate", slidewidth);
        if(lithis <= 4) {
            NsPh(jQuery);
            $(smallcul).animate({
                left : 0
            }, speed_animate_list, function() {
            });
            $(next).attr('rel', '0');
        }

        if(lithis >= lengthli - 1 & lengthli >= 5) {
            NhPs(jQuery)
            $(smallcul).animate({
                left : -smallc * listElementsWidth + (5 * listElementsWidth)
            }, speed_animate_list, function() {
            });
            $(next).attr('rel', smallc - 5);
        } else if(lithis < lengthli - 1 & lithis > 4 & lengthli >= 5) {
            showall(jQuery);
            $(smallcul).animate({
                left : -(lithis * listElementsWidth - 4 * listElementsWidth)
            }, speed_animate_list, function() {
            });
            $(next).attr('rel', lithis - 4);
        } else if(lithis >= lengthli - 1 & lengthli <= 5) {
            NhPs(jQuery)
            $(smallcul).animate({
                left : 0
            }, speed_animate_list, function() {
            });
            $(next).attr('rel', smallc - 5);
        } else if(lithis < lengthli - 1 & lithis > 4 & lengthli <= 5) {
            showall(jQuery);
            $(smallcul).animate({
                left : 0
            }, speed_animate_list, function() {
            });
            $(next).attr('rel', lithis - 4);
        }

        return false
    });
    var init = lengthli;
    var currentAnim  = 0;
    
/*******rotation automatic********/

    function rotateOff() {
     
        var currentme = parseInt($(galleryli).data("rotate"));
        currentAnim = currentme;
        var nextme = (currentme + 1) % init;
        if(nextme >= 0) {
            $(galleryli).data('rotate', nextme)
        }
       $(imagemask).show();
        $(".lightness").css('top','0px');
        $(galleryli+'[num='+(currentme+1)+'] .lightness').animate({
            opacity : 0
        }, lightnessspeed, function() {
            $(galleryli+'[num='+(currentme+1)+']').find(imagemask).fadeOut(textBannerFadeSpeed);
             $(".lightness").css({'top':'-700px','opacity':"1"});
            return false
        });
        $(galleryul).animate({
            left : "-" + currentme * widthslide
        }, speed_animate_rotator, fx, function() {
        });
        $(smallli).children('div').removeClass('bg');

        $(galleryli + "[current=" + nextme + "]").children('div').addClass('bg');
        if(nextme >= 1 & nextme <= 3) {
            NsPh(jQuery);
            $(smallcul).animate({
                left : 0
            }, speed_animate_list, function() {
            });
        }

        if(nextme >= lengthli - 2 & lengthli >= 5) {
            NhPs(jQuery)
            $(smallcul).animate({
                left : -smallc * listElementsWidth + (5 * listElementsWidth)
            }, speed_animate_list, function() {
            });
        } else if(nextme < lengthli - 2 & nextme > 3) {
            showall(jQuery);
            $(smallcul).animate({
                left : -(nextme * listElementsWidth - 3 * listElementsWidth)
            }, speed_animate_list, function() {
            });
        } else if(nextme <= 0 & lengthli >= 5) {
            NhPs(jQuery)
            $(smallcul).animate({
                left : -smallc * listElementsWidth + (5 * listElementsWidth)
            }, speed_animate_list, function() {
            });
            $(galleryli + "[current=" + lengthli + "]").children('div').addClass('bg');
        } else if(nextme >= lengthli - 2 & lengthli <= 5) {
            NhPs(jQuery)
            $(smallcul).animate({
                left : 0
            }, speed_animate_list, function() {
            });
        } else if(nextme <= 0 & lengthli <= 5) {
            NhPs(jQuery)
            $(smallcul).animate({
                left : 0
            }, speed_animate_list, function() {
            });
            $(galleryli + "[current=" + lengthli + "]").children('div').addClass('bg');
        }
        
        $('div.show-container ul li').css('visibility','hidden');
        $('div.show-container ul li').eq(currentme).css('visibility','visible');

    }

    
    if(rotatewrapper.hasClass("rotate-true")){
        interavl_ID = window.setInterval(rotateOff, pause);
    }
/**************/


/**************prev/next navigator***************/
    $(smallli + "[current=" + active + "]").children('div').addClass('bg');
    $(next).click(function() {
        window.clearInterval(interavl_ID);
        var start = $(this).attr('rel');
        var current = parseInt(start) + 1
        $(this).attr('rel', current)
        var currentnext = $(this).attr("rel");
        if(currentnext <= 0) {
            NsPh(jQuery);
        }
        if(currentnext >= smallc - 5) {
            NhPs(jQuery)
            $(smallcul).animate({
                left : "-=190"
            }, speed_animate_list, function() {
            });
        } else if(currentnext < smallc - 5 & currentnext > 0) {
            $(smallcul).animate({
                left : "-=190"
            }, speed_animate_list, function() {
            });
            showall(jQuery);
        }
    });
    $(prev).click(function() {
        window.clearInterval(interavl_ID);
        var currentdata = $(next).attr("rel") - 1;
        $(next).attr('rel', currentdata);

        if(currentdata <= 0) {
            NhPs(jQuery)
            $(smallcul).animate({
                left : "+=190"
            }, speed_animate_list, function() {
            });
        }
        if(currentdata >= smallc - 5) {
            NhPs(jQuery)

        } else if(currentdata < smallc - 5 & currentdata > 0) {
            $(smallcul).animate({
                left : "+=190"
            }, speed_animate_list, function() {
                var left = $(smallcul).css('left');
            });
            showall(jQuery);
        }

    });
    $('div.show-container').scroll(function(event){
        event.preventDefault();
        
         $('div.show-container').scrollLeft(0);
        return false;
    });
    
   
});
/**************************
 * rotator end
 */
