/*-----------------------------------------------------------------------------


@author:		César Ebrat
@contact:		cesar.ebrat@whads.com
@organization:	Whads/Accent SL
@since:			June 2010
-----------------------------------------------------------------------------*/

var SLIDESHOW_TIMEOUT = 8000;
var max;
var slidetimeout;
var i;
var MINIATURA_SIZE = 120;
var clicked = false;
var stop = false;


function showPanel (panel) {
    jQuery(panel).fadeIn("slow");
    showarros();
}

function setCurrent(panel) {    
    if(jQuery(".SlideShowBlockView .panel:visible").length>0){
        jQuery(".SlideShowBlockView .panel:visible").fadeOut("slow", function () {
            showPanel(panel);
        });
    }else{
        showPanel(panel);
    }
    
    var videoi = 0;
    jQuery(".SlideShowBlockView .panel:visible .filmstrip > li").each(function(){
        //Col·loquem els elements en filera posicionats de forma absoluta
        jQuery(this).css("position","absolute");
        jQuery(this).css("top","0");
        jQuery(this).css("left",MINIATURA_SIZE * videoi);
        //Incrementem l'index
        videoi++;
    });  

}

function initSlider (z) {    
    if(z==max) z = 0;
    var panel = jQuery(".SlideShowBlockView .panel").get(z);
    setCurrent(jQuery(panel));
    i = z;
    z++; 
    //slidetimeout = setTimeout("initSlider(" + z + ")", SLIDESHOW_TIMEOUT);
}

function stopSlider() {
    clearTimeout(slidetimeout);
}

function next(){
    i++;
    if(i>=max) i = 0;
    var panel = jQuery(".SlideShowBlockView .panel").get(i);
    setCurrent(jQuery(panel));
}

function previous(){
    i--;
    if(i<0) i = max-1;
    var panel = jQuery(".SlideShowBlockView .panel").get(i);
    setCurrent(jQuery(panel));
}
    
function showarros(){
    
    var nvideos = jQuery(".SlideShowBlockView .panel:visible .filmstrip-video-container ul li").length;
    if ( nvideos == 0 ) {
        jQuery(".SlideShowBlockView .panel:visible .filmstrip-container").css("background", "none");
    }
    if ( nvideos <=3 ){
        jQuery(".SlideShowBlockView .next").hide();
        jQuery(".SlideShowBlockView .previous").hide();
        jQuery(".SlideShowBlockView .panel:visible .filmstrip-video-container").css("margin-left", "25px");
    }else{
        var tope = MINIATURA_SIZE*(nvideos - 4);
        var left = $(".SlideShowBlockView .panel:visible .filmstrip-video-container ul").css("left");
        if (!left) {
            left == "0px";
        }
        left = left.replace("px", "");
        nleft = parseInt(left,10);
               
        if( nleft >= 0){
            jQuery(".SlideShowBlockView .previous").hide();
        }else {
            jQuery(".SlideShowBlockView .previous").show();
        }
        
        if ( nleft <= -tope ){
            jQuery(".SlideShowBlockView .next").hide();
        }else{
            jQuery(".SlideShowBlockView .next").show();
        }
    

    }
    

}

function reinit(){
    if (stop == "True") {
        stop = "False";
        i++;
        if(i > max) i = 0;
        if (slidetimeout) stopSlider();
        setTimeout("initSlider(" + i + ")", SLIDESHOW_TIMEOUT);;
    }
}
    

jQuery(document).ready(function () {
    
    i = 0;

    max = jQuery(".SlideShowBlockView .panel").length;
    
    jQuery(".flipmstrip:empty").parent().parent().addClass("buit");

    jQuery("#panel-next").css({
        top: (jQuery(".SlideShowBlockView .block_content").height()/2) - 50
    }); 
    
    jQuery("#panel-previous").css({
        top: (jQuery(".SlideShowBlockView .block_content").height()/2) - 50
    });
 
    jQuery("#panel-next")
        .click(function () {
            if(slidetimeout) stopSlider();
            next();
        })
        .mouseover(function () {
            jQuery(this)
            .animate({opacity: 1}, 300);
        })
        .mouseout(function () {
            jQuery(this)
            .animate({opacity: 0.1}, 300);
        })

    jQuery("#panel-previous")
        .click(function () {
            if(slidetimeout) stopSlider();
            previous();
        })
        .mouseover(function () {
            jQuery(this)
            .animate({opacity: 1}, 300);
        })
        .mouseout(function () {
            jQuery(this)
            .animate({opacity: 0.1}, 300);
        })
        
    jQuery(".SlideShowBlockView .next")
        .click(function(){
            if (slidetimeout) stopSlider();
            
            if (!clicked) {
                clicked = true;
                
                $('.SlideShowBlockView .panel:visible .filmstrip').animate({
                    left: '-='+MINIATURA_SIZE
                }, 750, function() {
                    showarros();
                    clicked = false;
                });
            }
                
            return(false);
        })
        .mouseover(function () {
            jQuery(this)
            .animate({opacity: 1}, 300);
        })
        .mouseout(function () {
            jQuery(this)
            .animate({opacity: 0.2}, 300);
        })

    jQuery(".SlideShowBlockView .previous")
        .click(function(){
            if (slidetimeout) stopSlider();
            
            if (!clicked) {
                clicked = true;            
                $('.SlideShowBlockView .panel:visible .filmstrip').animate({
                    left: '+='+MINIATURA_SIZE
                }, 750, function() {
                    showarros();
                    clicked = false;
                });
            }
            
            return(false);
        })
        .mouseover(function () {
            jQuery(this)
            .animate({opacity: 1}, 300);
        })
        .mouseout(function () {
            jQuery(this)
            .animate({opacity: 0.2}, 300);
        })
    
    jQuery(".SlideShowBlockView .filmstrip li img")
        .mouseover(function () {
            jQuery(this)
                .stop()
                .animate({opacity: 0.6}, 500);
            jQuery(this).next(".title")               
                .show();
        })
        .mouseout(function () {
            jQuery(this).next(".title")
                .hide(); 
            if(jQuery(this).hasClass("current")){
                jQuery(this)
                    .stop()
                    .animate({top: 0}, 500);
            }else{
                jQuery(this)
                    .stop()
                    .animate({opacity: 1, top: 0}, 500);
            }            
        });
            
    jQuery(".SlideShowBlockView .panel .filmstrip-container")
        .mouseover(function () {
            if (slidetimeout) stopSlider();
            stop = "False";
        })
        .mouseout(function () {
            stop = "True";
            setTimeout( "reinit()" ,3000);
        });

    jQuery(".SlideShowBlockView .panel .filmstrip-container *")
        .mouseover(function () {
            stop = "False";
        });

    if(max > 1) initSlider(i);
    if(max == 1){ 
        jQuery(".SlideShowBlockView .panel").fadeIn("slow");
        jQuery(".SlideShowBlockView .filmstrip img").animate({
            opacity: 1
            }, 500, function() {
                showarros();
            }).addClass("current");
    }

    var torest = parseInt(jQuery(".SlideShowBlockView .panel .panel-overlay").css('paddingRight'))*2;

    jQuery(".SlideShowBlockView .panel .panel-overlay").css({
        width: jQuery(".SlideShowBlockView .block_content").width() - torest
    });

    jQuery(".video_link,.vimeo_link").click(function () {
        if(slidetimeout) stopSlider();
        var height = jQuery(".SlideShowBlockView .block_content").height();
        var width = jQuery(".SlideShowBlockView .block_content").width();
        jQuery(".SlideShowBlockView .panel,.SlideShowBlockView .filmstrip,#panel-next,#panel-previous").hide();
        
        //Cms video
        if(jQuery(this).hasClass("video_link")) {

            var img_url = jQuery(this).parents(".panel").find(".panel-image").attr("src");

            var flashvars = {
              item: this.id,
              description: "",
              playlistfile: "/playlist_xml/?id=" + jQuery(this).parent().attr("id"),
              controlbar: "over",
              //type: "video",
              //title: "",
              //image: img_url,              
              skin: "/wearewater_resources/flash/stylish.swf",
              autostart: "true",
              frontcolor: 'cccccc',
              lightcolor: '66CCFF',
              backcolor: '111111',
              screencolor: '000000',
              dock: 'true',
              plugins: 'flow.swf, vimeo.swf, sharing-1',
              "flow.onidle":"hide",
              "flow.onplaying":"hide",
              "flow.onpaused":"hide",
              "flow.focallength":760,
              "flow.coverwidth":250,
              "flow.textstyle":"div{color:#ffffff; textAlign:center; fontFamily:Arial,Verdana,sans-serif;} h1{fontSize:13; leading:5;} h2{fontSize:11;}",
              "flow.coverdepth":100,
              "flow.coveroffset":105,
              "flow.yposition":-10,
              //"flow.controlbaricon":true,
              "flow.tweentime":"0.8",
              "flow.opacitydecrease":"0.2"
            };

            var params = {
              allowfullscreen: true,
              allowscriptaccess: 'allways',
              allownetworking: 'all',
              wmode: "transparent"
            };

            var attributes = {
              id: "myDynamicContent",
              name: "myDynamicContent"
            };


            swfobject.embedSWF("/wearewater_resources/flash/player.swf","video_player",String(width),String(height),"10.0.0",false, flashvars, params, attributes);

        }            
        
        //Vimeo video
        if(jQuery(this).hasClass("vimeo_link")) {

            var flashvars = {
                clip_id: this.id,
                server: "vimeo.com",
                show_title: 1,
                show_byline: 1,
                show_portrait: 0,
                color: "00adef",
                fullscreen: 1
            }

            var params = {
                allowfullscreen: true,
                allowscriptaccess: "always",
                wmode: "transparent"
            }

            var attributes = {
              id: "myDynamicContent",
              name: "myDynamicContent"
            }

            swfobject.embedSWF("http://vimeo.com/moogaloop.swf","video_player",String(width),String(height),"9.0.0","expressInstall.swf", flashvars, params, attributes);
        }

        jQuery(".video-container").append("<a href='javascript:;' class='close-button'><img src='/wearewater_resources/images/close_video_button.gif' alt='' /></a>");

        jQuery(".close-button").click(function () {
            var div = document.createElement("div");
            div.id = "video_player";
            jQuery(".video-container")
                .html('')
                .append(div);
            jQuery(".SlideShowBlockView .filmstrip, #panel-next, #panel-previous").show();
            if(max > 1) initSlider(i);
            if(max == 1) jQuery(".SlideShowBlockView .panel").show();
        });

    });
        
    showarros();
    
});

