/* Author: Gerard Nesta
   Date: 22/11/2011
*/

$(document).ready(function(){ 
console.log("common sentences");
	$("#firstpane p.menu_head").click(function(){
		$(this).css("text-decoration","underline").next("div.menu_body").slideToggle(300).siblings("div.menu_body").slideUp("slow");
		$(this).css("text-decoration","underline").siblings("p.menu_head").css("text-decoration","none");
	}); 
	mCustomScrollbars();
	console.log("common sentences done");
});

function mCustomScrollbars(){
	$("#galeria").mCustomScrollbar("horizontal",500,"easeOutCirc",1,"fixed","yes","yes",20); 
}

/* function to fix the -10000 pixel limit of jquery.animate */
$.fx.prototype.cur = function(){
    if ( this.elem[this.prop] != null && (!this.elem.style || this.elem.style[this.prop] == null) ) {
      return this.elem[ this.prop ];
    }
    var r = parseFloat( jQuery.css( this.elem, this.prop ) );
    return typeof r == 'undefined' ? 0 : r;
}

if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

path_imgs = "";
actual = 0;
j= 0;
g= 0;
f = 0;
anterior = 0;
function getRandomArray(min,max){
    var A= [];
    while(max>= min) A.push(max--)    
    A.sort(function(){return .5- Math.random()});  
    return A;
}

function scrollerScrollToX(refid,valTo){

if (valTo>0)
{
var $dragger=$("#"+refid+" .dragger");
var $customScrollBox=$("#"+refid+" .customScrollBox");
var $customScrollBox_container=$("#"+refid+" .customScrollBox .container");
var $dragger_container=$("#"+refid+" .dragger_container");

container_width=$customScrollBox.width(); content_width=$customScrollBox_container.width(); drag_width=$dragger_container.width();
dragger_width=$dragger.width();
if (valTo>(content_width-container_width)) valTo=content_width-container_width;

var scrollAmount=(content_width-container_width)/(drag_width-dragger_width);

var btnsScrollTo=parseInt(valTo/scrollAmount);

var scrollSpeed=Math.abs($dragger.position().left-btnsScrollTo)*(100/150);
$dragger.stop().animate({left: btnsScrollTo}, scrollSpeed,"linear");
$customScrollBox_container.stop().animate({left: "-="+valTo}, 300, "easeOutCirc");
}}

function galeriaficha(path,imgs){
		imgs_creadas = getRandomArray(1,imgs);
		actual = imgs_creadas[0];
		console.log(imgs_creadas);
		path_imgs = path;
		int = self.setInterval("siguiente()",5000);
		for(i=0;i<imgs;i++){
			imatge = new Image();
			imatge.src = path+"/thumb"+imgs_creadas[i]+"b.jpg";
			imatge.style.width = "50";
			imatge.style.height = "50";
			imatge.alt = "miniatura de un proyecto realizado por vertice, click para engrandecer";
			imatge.setAttribute("class","thumb_img");
			imatge.setAttribute("id", "image-"+imgs_creadas[i]);
			$("#preload").append("<img src='"+path+"/thumb"+imgs_creadas[i]+".jpg' width='1' height='1'>");
			document.getElementById("miniatures").appendChild(imatge);
			$("#image-"+imgs_creadas[i]).mouseover(function () {
				numero = $(this).attr("id");
				numero = numero.split("-");
				$(this).attr("src", function() { 
					 return path+"/thumb" + numero[1] + ".jpg";});
				});
			$("#image-"+imgs_creadas[i]).bind('mouseleave',function () {
				numero = $(this).attr("id");
				numero = numero.split("-");
				$(this).attr("src", function() { 
					return path+"/thumb" + numero[1] + "b.jpg";});
				});
			$("#image-"+imgs_creadas[i]).click(function () {
				window.clearInterval(int);
				$("#image-"+actual).bind('mouseleave',function () {
				numero = $(this).attr("id");
				numero = numero.split("-");
				$(this).attr("src", function() { 
					return path+"/thumb" + numero[1] + "b.jpg";});
				});
				$("#image-"+actual).attr("src",path_imgs+"/thumb"+actual+"b.jpg");
				numero = $(this).attr("id");
				numero = numero.split("-");
				imatgemain = document.getElementById("photohome");
				$("#imagen_gal img").fadeOut('fast', function() {
				$("#imagen_gal img").remove();		
				var _url = path+"/imagen"+numero[1]+".jpg";
				_im = $("<img>");
				_im.hide();
				_im.bind("load",function(){ 
				$(this).fadeIn(); 
				});
				$("#imagen_gal").append(_im);
				_im.attr("src",_url);	
				_im.attr("alt", "imagen de proyecto");
				_im.attr("id", "photohome");
				$("#image-"+numero[1]).unbind("mouseleave");
				$("#image-"+numero[1]).attr("src",path_imgs+"/thumb"+numero[1]+".jpg");	
				actual = parseInt(numero[1]);
				});
			});
				}
	
}

function siguiente(){
		window.clearInterval(int);
		if($("#miniatures img")[actual]){
			console.log("Previ: "+actual);
			imatgemain = document.getElementById("photohome");
		$("#image-"+actual).prop("src", function() { 
				numerob = $(this).attr("id");
				numerob = numerob.split("-");
				return path_imgs+"/thumb" + numerob[1] + ".jpg";
			});
			g= f-1
		anterior = imgs_creadas[g];
		$("#image-"+anterior).prop("src", function() { 
				numeroc = $(this).attr("id");
				numeroc = numeroc.split("-");
				 return path_imgs+"/thumb" + numeroc[1] + "b.jpg";
			});
		$("#imagen_gal img").fadeOut('fast', function() {
				$("#imagen_gal img").remove();	
				var _url = path_imgs+"/imagen"+actual+".jpg";
				console.log(_url);
				_im = $("<img>");
				_im.hide();
				_im.bind("load",function(){ 
				$(this).fadeIn(); 	
				int = self.setInterval("siguiente()",5000);
				});
				$("#imagen_gal").append(_im);
				_im.attr("src",_url);	
				_im.attr("alt", "imagen de proyecto");
				_im.attr("id", "photohome");
		f++;
		actual = imgs_creadas[f];
		console.log("Despres: "+actual);
		});
		if (g>=6){
				scrollerScrollToX("galeria",50);	
		}
		} else {
			actual = imgs_creadas[0];
		}
}

function galeriaficha2(path,imgs){
		if(imgs.length > 8){
			$(".horWrapper").css("float","none");
		}
		actual = 0; 
		path_imgs = path;
		for(i=0;i<imgs.length;i++){
			imatge = new Image();
			imatge.src = path+"/thumb_"+imgs[i]+"b.jpg";
			imatge.style.width = "50";
			imatge.style.height = "50";
			imatge.alt = "miniatura de un proyecto realizado por vertice, click para engrandecer";
			imatge.setAttribute("class","thumb_img");
			imatge.setAttribute("id", "image_"+imgs[i]);
			$("#preload").append("<img src='"+path+"/thumb_"+imgs[i]+".jpg' width='1' height='1'>");
			document.getElementById("miniatures").appendChild(imatge);
			$("#image_"+imgs[i]).mouseover(function () {
				numero = $(this).attr("id");
				numero = numero.substring(6);
				$(this).attr("src", function() { 
					 return path+"/thumb_" + numero + ".jpg";});
				});
			$("#image_"+imgs[i]).bind('mouseleave',function () {
				numero = $(this).attr("id");
				numero = numero.substring(6);
				$(this).attr("src", function() { 
					return path+"/thumb_" + numero + "b.jpg";});
				});
			$("#image_"+imgs[i]).click(function () {
				// window.clearInterval(int);
				$("#image_"+actual).bind('mouseleave',function () {
				numero = $(this).attr("id");
				numero = numero.substring(6);
				$(this).attr("src", function() { 
					return path+"/thumb_" + numero + "b.jpg";});
				});
				$("#image_"+imgs[actual]).attr("src",path_imgs+"/thumb_"+imgs[actual]+"b.jpg");
				numero = $(this).attr("id");
				numero = numero.substring(6);
				imatgemain = document.getElementById("photohome");
				$("#imagen_gal img").fadeOut('fast', function() {
				$("#imagen_gal img").remove();		
				var _url = path+"/"+numero+".jpg";
				_im = $("<img>");
				_im.hide();
				_im.bind("load",function(){ 
				$(this).fadeIn(); 
				});
				$("#imagen_gal").append(_im);
				_im.attr("src",_url);	
				_im.attr("alt", "imagen de proyecto");
				_im.attr("id", "photohome");
				$("#image_"+numero).unbind("mouseleave");
				$("#image_"+numero).attr("src",path_imgs+"/thumb_"+numero+".jpg");	
				actual = imgs.indexOf(numero);
				});
			});		
				
	}	
	console.log("start mcustom")
	$("#galeria").mCustomScrollbar("horizontal",500,"easeOutCirc",1,"fixed","yes","yes",20);
	console.log("finish mcustom");
}

