$(document).ready(function(){
	initGaleria();
	$('.btnVerReel').click(function(){viewReel($(this).attr('href'));return false;});
	setInterval('loop()',100);
});





var ul_galImgs;
var left;
var totImgs;
var countClicks = 0;
var direccion;
var lastPos = 100;
var status_galImgs = 'stopped';
var galListImgs = new Array();


function initGaleria(){
	ul_galImgs  = $(".galeria-imgs ul");
	left        = 0;
	if(ul_galImgs.length==0) return false;

	$(".galeria-imgs li img").each(function(){ galListImgs.push( new Array($(this).attr('longdesc'),$(this).attr('alt')));  });
	totImgs = galListImgs.length;

    $(".galeria-imgs").append('<p class="imgDesc"></p>');
    $(".galeria-imgs").append('<div class="imgAction"></div>');
    $(".galeria-imgs").append('<div class="prev"></div>');
    $(".galeria-imgs").append('<div class="next"></div>');
	$(".galeria-imgs").append("<p class='counter'><span>1</span> / "+totImgs+"</p>");
	$(".galeria-imgs").append("<div class='lupa'></div>");

	var html = "<li class='relleno left'></li>"+ul_galImgs.html()+"<li class='relleno right'></li>";
	ul_galImgs.html(html);
    counterUpdate();

    //  Aplico acciones
    $(".galeria-imgs .imgAction").click(viewImg);
	$(".galeria-imgs .lupa").click(viewImg);
	$(".galeria-imgs .prev").click(viewPrev);
	$(".galeria-imgs .next").click(viewNext);


    setInterval('loop()',100);
}

function viewImg(){
    var html = '';
    html += '<img src="'+galListImgs[countClicks][0]+'" alt="'+galListImgs[countClicks][1]+'"  />';
    addPopUp('galeria',html);
}
function viewReel(id){
    var html = '';
    html += htmlPlayer(id);
    addPopUp('reel',html);
}
function viewNext(){
	if(countClicks<totImgs-1) {
		direccion = "next";
		left -=275;
		countClicks++;
        status_galImgs='play';
		counterUpdate();
        return true;
	}else return false;
}
function viewPrev(){
	if(0<countClicks){
		direccion = "prev";
		left +=275;
		countClicks--;
        status_galImgs='play';
		counterUpdate();
        return true;
	}else return false;
}
function counterUpdate(){
    tit = galListImgs[countClicks][1];
	$(".galeria-imgs .counter span").text(countClicks+1);
    $(".galeria-imgs p.imgDesc").text(tit);

    // Saca cursor cuando estoy en los extremos
    var btnPrev = $(".galeria-imgs .prev");
    var btnNext = $(".galeria-imgs .next");

    if(countClicks==0)      btnPrev.css('cursor','auto');
    else if(countClicks==1) btnPrev.css('cursor','pointer');

    if(countClicks==(totImgs-1))      btnNext.css('cursor','auto');
    else if(countClicks==(totImgs-2)) btnNext.css('cursor','pointer');

}

function moverGaleria(){
    if(status_galImgs=='stopped') return false;

	var pos = parseInt(ul_galImgs.css("margin-left"));
	if(direccion=="next")
        pos = Math.floor((left+pos*2)/3);
	else
        pos = parseInt((left+pos*2)/3);

    if(pos==lastPos){
        status_galImgs='stopped';
        return false;
    }
    lastPos = pos;

	ul_galImgs.css("margin-left",pos+"px");
}


function loop(){
    moverGaleria();
    resizeContainer();
    posisionaPopUp();
}



var documentHeight = 0;
var popUpWindow;


function addPopUp(type,html){
    documentHeight = $(document).height();
    $('body').append('<div id="fnd-popUp"></div>');
    $('div#fnd-popUp').height(documentHeight);
    $('div#fnd-popUp').click(removePopUp);

    addWindow(type,html);
}

function removePopUp(){
    $('div#fnd-popUp').remove();
    popUpWindow.remove();
    $(document).unbind('scroll');
}

function addWindow(type,html){
    $('body').append('<div id="popUpWindow"></div>');
    popUpWindow = $('div#popUpWindow');
    if(type=='galeria'){
        if(1<totImgs)
           popUpWindow.html('<div class="prev"></div><div class="next"></div><span class="close"></span><div class="auxContainer"><div class="container">'+html+'</div></div><p class="imgDesc">'+galListImgs[countClicks][1]+'</p>');
        else
           popUpWindow.html('<span class="close"></span><div class="auxContainer"><div class="container">'+html+'</div></div><p class="imgDesc">'+galListImgs[countClicks][1]+'</p>');
    }else
        popUpWindow.html('<span class="close"></span><div class="container">'+html+'</div>');

    $('.close',popUpWindow).click(removePopUp);

    ajustaWindow(type);
}

function ajustaWindow(type){
    container = $('.container',popUpWindow);
    if(type=='reel')
        container.addClass('reel');
    var left = parseInt(($(document).width()-$('#popUpWindow').width())/2);
    popUpWindow.css('left',left+'px');

    if(type=='galeria') ajustaWindowGaleria();
	else ajustaWindowReel();
}

function ajustaWindowGaleria(){
    btnPrev   = $('.prev',popUpWindow);
    btnNext   = $('.next',popUpWindow);

    btnPrev.click(function(){if(viewPrev())changeImage();});
    btnNext.click(function(){if(viewNext())changeImage();});

    $('img',container).load(function(){showImage($(this)) });
}

function ajustaWindowReel(){
	showReel();
}

var status_container      = 'stopped';
var containerHeightLast   = 0;
var containerHeightFinal  = 0;
var container ;
var btnNext;
var btnPrev;

function resizeContainer(){
    if(status_container=='stopped') return false;

	var actualH = container.height();
    actualH = parseInt((containerHeightFinal+actualH)/2);

    if(actualH==containerHeightLast){
        status_container='stopped';
        $('img',container).fadeIn('normal');
        if(load_img){
            $('img',popUpWindow).attr('src',galListImgs[countClicks][0]);
            $('.imgDesc',popUpWindow).html(galListImgs[countClicks][1]);
            load_img = false;
	    	btnNext.show();
       		btnPrev.show();
        }
        return false;
    }

    containerHeightLast = actualH;
	container.height(actualH);

          if(btnNext!=undefined ){
		btnNext.height(actualH);
   		btnPrev.height(actualH);
   	  }
	
}

var status_popUp = 'stopped';
var popUpYLast   = 0;
var popUpYFinal  = 0;

function posisionaPopUp(){
    if(status_popUp=='stopped') return false;
    var YFinal = $(document).scrollTop()+popUpYFinal;
	var posY = parseInt(popUpWindow.css('top'));
    posY     = parseInt((YFinal+posY)/2);
    if(posY==popUpYLast){
        status_popUp='stopped';
        status_container = 'play';
        return false;
    }
    popUpYLast = posY;
	popUpWindow.css('top',posY+'px');
}

function autoPosicionar(){
    status_popUp = 'play';
}

function changeImage(){
	btnNext.hide();
   		btnPrev.hide();

     if(countClicks==0)      btnPrev.css('width','0');
     else if(countClicks==1) btnPrev.css('width','33px');

     if(countClicks==(totImgs-1))      btnNext.css('width','0');
     else if(countClicks==(totImgs-2)) btnNext.css('width','33px');

     containerHeightFinal = 0;
     status_container     = 'play';
     load_img             = true;
}
var load_img = false;

function showImage(img){
   containerHeightFinal = img.height();
   img.hide();
   $(document).bind('scroll',autoPosicionar);
   var altoPopUp = popUpWindow.height()+containerHeightFinal;
   popUpYFinal   = parseInt(($(window).height()-altoPopUp)/2);
   status_popUp  = 'play';
}


function showReel(){
   containerHeightFinal = 350;
   $(document).bind('scroll',autoPosicionar);
   var altoPopUp = popUpWindow.height()+containerHeightFinal-80;
   popUpYFinal   = parseInt(($(window).height()-altoPopUp)/2);
   status_popUp  = 'play';
}

function htmlPlayer(URL_MOVIE){
    var htmlReel = '';
    htmlReel += '<object data="/swf/player.swf?movie='+URL_MOVIE+'&amp;w=440&amp;h=330" type="application/x-shockwave-flash" width="440" height="330">';
    htmlReel += '<param name="allowScriptAccess" value="sameDomain" />';
    htmlReel += '<param name="movie" value="/swf/player.swf?movie='+URL_MOVIE+'&amp;w=440&amp;h=330" />';
    htmlReel += '<param name="quality" value="best" />';
    htmlReel += '<param name="scale" value="noscale" />';
    htmlReel += '<param name="wmode" value="transparent" />';
    htmlReel += '<param name="menu" value="false" />';
    htmlReel += '<param name="bgcolor" value="#FFFFFF" />';
    htmlReel += '<param name="pluginurl" value= "http://www.macromedia.com/go/getflashplayer" />Este sitio requiere <a href="http://www.macromedia.com/go/getflashplayer">Flash player</a>';
    htmlReel += '</object>';
    return htmlReel;
}
