$(document).ready(function(){
    $("#photos").css("display","block");
    $("#photos").galleryView({
        panel_width: 300,
        panel_height: 200,
        overlay_height: 50,
        overlay_position: 'bottom',
        overlay_color: '#ffffff',
        overlay_text_color: '#026db3',
        caption_text_color 	: '#353736',
        overlay_opacity: 0.5,
        transition_speed: 1000,
        transition_interval: 5000,
        nav_theme: 'dark',
        border: 'none',
        pause_on_hover: true
    });
    $("#photos").each(function(){
        var truncate = function(text, len) {
            if (text.length > len) {
                text = text.substring(0, len);
                text = text.substring(0, text.lastIndexOf(" "));
                while (text.charAt(text.length-1) == ',' || text.charAt(text.length-1) == '.') {
                    text = text.substring(0, text.length-1);
                }
                text = text + " ...";
            }
            return text;
        }
        $(".panel .panel-overlay", this).each(function(){            
            $(this).css("overflow", "hidden");
            $("h4 a", this).html(truncate($("h4 a", this).html(), 46));
            $(".summary", this).html(truncate($(".summary", this).html(), 100));
        });
    });    
    // begin js for "Image news"   
    
    $(".home-img-slide").each(function(){
        var total = $(".items", this).size();
        var iterator = 0;
        var timmer = false;
        var height = 0;
        var t = this;
        $(".items", this).each(function(i){
            if ($(this).height() > height) {
                height = $(this).height();
            }
            if(i > 1) {
                $(".options ul li:eq(0)", $(t).parent()).after("<li class=\"blank active\">&nbsp;</li>");
            } else {
                $(".options ul li:eq(0)", $(t).parent()).after("<li class=\"blank\">&nbsp;</li>");
            }
        });
         $(this).css('position', 'relative').css('height', height + 'px');
        $(".items", this).css('position', 'absolute').css('top', 0).css('left', 0);
        $(".options ul li:first", $(this).parent()).click(function(){
            if (!timmer) {
                var tt = this;
                timmer = true;
                $(this).css("cursor", "wait");
                // current items will fade out
                $(".items", t).eq(iterator).fadeOut(500, function(){
                    // repeat items
                    if (iterator == 0){
                        iterator = total;
                    }
                    // previous items
                    iterator--;
                    // slide nav items
                    $(".options ul li", $(t).parent()).removeClass("active");
                    $(".options ul li", $(t).parent()).eq(iterator+1).addClass("active");                
                    // previous items will fade out
                    $(".items", t).eq(iterator).fadeIn(1000, function(){
                        timmer = false;
                        $(tt).css("cursor", "pointer");
                    });
                });
            }
        }).css("cursor", "pointer");
        $(".options ul li:last", $(this).parent()).click(function(){
            if (!timmer) {
                var tt = this;
                timmer = true;
                $(this).css("cursor", "wait");
                // current items will fade out
                $(".items", t).eq(iterator).fadeOut(500, function(){
                    // next items
                    iterator++;
                    // repeat items
                    if (iterator == total){
                        iterator = 0;
                    }
                    // slide nav items
                    $(".options ul li", $(t).parent()).removeClass("active");
                    $(".options ul li", $(t).parent()).eq(iterator+1).addClass("active");                    
                    // next items will fade out
                    $(".items", t).eq(iterator).fadeIn(1000, function(){
                        timmer = false;
                        $(tt).css("cursor", "pointer");
                    });
                });
            }
        }).css("cursor", "pointer");
    });
    // end js for "Image news"
    
    // Home categories
    $(".home-categories").each(function(){
        var total = $(".home-category", this).size();
        var iterator = 0;
        var timmer = false;
        var height = 0;
        var t = this;
        $(".home-category", this).each(function(i){
            if ($(this).height() > height) {
                height = $(this).height();
            }
        });
        $(this).css('position', 'relative').css('height', height + 'px');
        $(".home-category .box", this).css('height', height + 'px');
        $(".home-category", this).css('position', 'absolute').css('top', 0).css('left', 0);
        $(".options ul li:first", $(this).parent()).click(function(){
            if (!timmer) {
                var tt = this;
                timmer = true;
                $(this).css("cursor", "wait");
                // current items will fade out
                $(".home-category", t).eq(iterator).fadeOut(500, function(){
                    // repeat items
                    if (iterator == 0){
                        iterator = total;
                    }
                    // previous items
                    iterator--;
                    
                    // previous items will fade out
                    $(".home-category", t).eq(iterator).fadeIn(1000, function(){
                        timmer = false;
                        $(tt).css("cursor", "pointer");
                    });
                });
            }
        }).css("cursor", "pointer");
        $(".options ul li:last", $(this).parent()).click(function(){
            if (!timmer) {
                var tt = this;
                timmer = true;
                $(this).css("cursor", "wait");
                // current items will fade out
                $(".home-category", t).eq(iterator).fadeOut(500, function(){
                    // next items
                    iterator++;
                    // repeat items
                    if (iterator == total){
                        iterator = 0;
                    }
                    // next items will fade out
                    $(".home-category", t).eq(iterator).fadeIn(1000, function(){
                        timmer = false;
                        $(tt).css("cursor", "pointer");
                    });
                });
            }
        }).css("cursor", "pointer");
    });
    // Home photo album
    $(".cute-img .imgs ul li img").each(function(i){
        var imgs = new Array();
        imgs[i] = new Image();
        imgs[i].src = $(this).attr("src").replace("size=65x45", "size=200x300");
        $(this).click(function(){
            var imgX = $(this).parent().offset().left;
            var imgY = $(this).parent().offset().top;
            
            var viewX = $(".cute-img .img").offset().left + parseInt($(".cute-img .img").css("padding-left"),10);
            var viewY = $(".cute-img .img").offset().top + parseInt($(".cute-img .img").css("padding-top"),10);;
            
            var gotoX = viewX - imgX;
            var gotoY = viewY - imgY;

            var width = $(".cute-img .img").width();
            var height = $(".cute-img .img").height();

            $(this)
            .clone()
            .prependTo(".cute-img .imgs ul li:eq("+i+")")
            .css({'position' : 'absolute'})
            .animate({opacity: 0.4}, 100 )
            .animate({
                opacity: 0.8,
                marginLeft: gotoX,
                marginTop: gotoY,
                width: width,
                height: height}, 1200, function() {
                    $(this).remove();                    
                    $(".cute-img .img a").html("<img src=\"" + imgs[i].src + "\" />").fadeTo("slow", 1);
                }
            );       
        }).css("cursor", "pointer");
    });
});


