/***************************/
//@Author: Adrian "yEnS" Mato Gondelle & Ivan Guardado Castro
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

$(document).ready(function(){
	$('.menu a[@href$="/blog/"]').attr({target: "_blank" });
	$(".holderabc").jScrollPane( { reinitialiseOnImageLoad: true }); 
	if ($(".holder").length > 0 ) {
		$(window).resize(function(){
   			$('.holder').jScrollPaneRemove();
   			$('.holder').jScrollPane();
		});
	}

	$(".post-middle-holderabc").jScrollPane( { showArrows:true, reinitialiseOnImageLoad: true });
	if ($(".post-middle-holder").length > 0 ) {
		$(window).resize(function(){
   			$('.post-middle-holder').jScrollPaneRemove();
   			$('.post-middle-holder').jScrollPane();
		});
	}

	$(".post-right-holder").jScrollPane( { showArrows:true, reinitialiseOnImageLoad: true });
	if ($(".post-right-holder").length > 0 ) {
		$(window).resize(function(){
   			$('.post-right-holder').jScrollPaneRemove();
   			$('.post-right-holder').jScrollPane();
		});
	}

  	$(".pagecontentsections img").hover(function() {
    		$(this).attr("src", $(this).attr("src").split(".").join("-hover."));
  	}, function() {
    		$(this).attr("src", $(this).attr("src").split("-hover.").join("."));
  	});

  	$(".hover").hover(function() {
    		$(this).attr("src", $(this).attr("src").split(".").join("-hover."));
  	}, function() {
    		$(this).attr("src", $(this).attr("src").split("-hover.").join("."));
  	});

	$(".menu1 > li").click(function(e){
		switch(e.target.id){
			case "about":
				//change status & style menu
				$("#about").addClass("active");
				$("#blog").removeClass("active");
				$("#email").removeClass("active");
				$("#gallery").removeClass("active");
				$("#invest").removeClass("active");
				$("#clients").removeClass("active");
				//display selected division, hide others
				$("div.about").fadeIn();
				$("div.blog").css("display", "none");
				$("div.email").css("display", "none");
				$("div.gallery").css("display", "none");
				$("div.invest").css("display", "none");
				$("div.clients").css("display", "none");
			break;
			case "blog":
				//change status & style menu
				$("#blog").addClass("active");
				$("#about").removeClass("active");
				$("#email").removeClass("active");
				$("#gallery").removeClass("active");
				$("#invest").removeClass("active");
				$("#clients").removeClass("active");
				//display selected division, hide others
				$("div.blog").fadeIn();
				$("div.about").css("display", "none");
				$("div.email").css("display", "none");
				$("div.gallery").css("display", "none");
				$("div.invest").css("display", "none");
				$("div.clients").css("display", "none");
			break;
			case "email":
				//change status & style menu
				$("#email").addClass("active");
				$("#blog").removeClass("active");
				$("#about").removeClass("active");
				$("#gallery").removeClass("active");
				$("#invest").removeClass("active");
				$("#clients").removeClass("active");
				//display selected division, hide others
				$("div.email").fadeIn();
				$("div.blog").css("display", "none");
				$("div.about").css("display", "none");
				$("div.gallery").css("display", "none");
				$("div.invest").css("display", "none");
				$("div.clients").css("display", "none");
			break;
			case "gallery":
				//change status & style menu
				$("#gallery").addClass("active");
				$("#blog").removeClass("active");
				$("#email").removeClass("active");
				$("#about").removeClass("active");
				$("#invest").removeClass("active");
				$("#clients").removeClass("active");
				//display selected division, hide others
				$("div.gallery").fadeIn();
				$("div.blog").css("display", "none");
				$("div.email").css("display", "none");
				$("div.about").css("display", "none");
				$("div.invest").css("display", "none");
				$("div.clients").css("display", "none");
			break;
			case "invest":
				//change status & style menu
				$("#invest").addClass("active");
				$("#blog").removeClass("active");
				$("#email").removeClass("active");
				$("#gallery").removeClass("active");
				$("#about").removeClass("active");
				$("#clients").removeClass("active");
				//display selected division, hide others
				$("div.invest").fadeIn();
				$("div.blog").css("display", "none");
				$("div.email").css("display", "none");
				$("div.gallery").css("display", "none");
				$("div.about").css("display", "none");
				$("div.clients").css("display", "none");
			break;
			case "clients":
				//change status & style menu
				$("#clients").addClass("active");
				$("#blog").removeClass("active");
				$("#email").removeClass("active");
				$("#gallery").removeClass("active");
				$("#invest").removeClass("active");
				$("#about").removeClass("active");
				//display selected division, hide others
				$("div.clients").fadeIn();
				$("div.blog").css("display", "none");
				$("div.email").css("display", "none");
				$("div.gallery").css("display", "none");
				$("div.invest").css("display", "none");
				$("div.about").css("display", "none");
			break;
		}
		//alert(e.target.id);
		return false;
	});

  lightbox();

     //select all the a tag with name equal to modal  
     $('a[name=modal]').click(function(e) {  
         //Cancel the link behavior  
         e.preventDefault();  
         //Get the A tag  
         var id = $(this).attr('href');  
       
         //Get the screen height and width  
         var maskHeight = $(document).height();  
         var maskWidth = $(window).width();  
       
         //Set height and width to mask to fill up the whole screen  
         $('#mask').css({'width':maskWidth,'height':maskHeight});  
           
         //transition effect       
         $('#mask').fadeIn(1000);      
         $('#mask').fadeTo("slow",0.8);    
       
         //Get the window height and width  
         var winH = $(window).height();  
         var winW = $(window).width();  
                 
         //Set the popup window to center  
         $(id).css('top',  winH/2-$(id).height()/2);  
         $(id).css('left', winW/2-$(id).width()/2);  
       
         //transition effect  
         $(id).fadeIn(2000);   
       
     });  
       
     //if close button is clicked  
     $('.window .close').click(function (e) {  
         //Cancel the link behavior  
         e.preventDefault();  
         $('#mask, .window').hide();  
     });       
       
     //if mask is clicked  
     $('#mask').click(function () {  
         $(this).hide();  
         $('.window').hide();  
     });           
});

function lightbox() {
  var links = $('a[rel^=lightbox]');
  var newborns = $('a[rel^=lightboxnewborn]');
  var babies = $('a[rel^=lightboxbabies]');
  var kids = $('a[rel^=lightboxkids]');
  var family = $('a[rel^=lightboxfamily]');
  var overlay = $(jQuery('<div id="overlay" style="display: none"></div>'));
  var container = $(jQuery('<div id="lightbox" style="display: none"></div>'));
  var close = $(jQuery('<a href="#close" class="close">&times; Close</a>'));
  var target = $(jQuery('<div class="target"></div>'));
  var prev = $(jQuery('<a href="#prev" class="prev">&laquo; Previous</a>'));
  var next = $(jQuery('<a href="#next" class="next">Next &raquo;</a>'));

  $('body').append(overlay).append(container);
  container.append(close).append(target).append(prev).append(next);
  container.show().css({'top': Math.round((($(window).height() > window.innerHeight ? window.innerHeight : $(window).height()) - container.outerHeight()) / 2) + 'px', 'left': Math.round(($(window).width() - container.outerWidth()) / 2) + 'px', 'margin-top': 0, 'margin-left': 0}).hide();
  close.click(function(c) {
    c.preventDefault();
    overlay.add(container).fadeOut('normal');
    $(document).unbind('keydown', keyHandler);
    links.filter('.selected').removeClass('selected');
  });

  prev.add(next).click(function(c) {
    c.preventDefault();
    var gallery = links.filter('.selected').attr('lb-gallery');
    if (gallery == "newborn")
	locallinks = newborns;
    else if (gallery == "babies")
	locallinks = babies;
    else if (gallery == "kids")
	locallinks = kids;
    else if (gallery == "family")
	locallinks = family;

    var current = parseInt(locallinks.filter('.selected').attr('lb-position'),10);
    var to = $(this).is('.prev') ? locallinks.eq(current - 1) : locallinks.eq(current + 1);

    /*
    if(!to.size()) {
      to = $(this).is('.prev') ? newborns.eq(newborns.size() - 1) : newborns.eq(0);
    }
    */

    if(to.size()) {
      to.click();
    }
  });

  newborns.each(function(index) {
    var link = $(this);
    link.click(function(c) {
      c.preventDefault();
      open(link.attr('href'));
      newborns.filter('.selected').removeClass('selected');
      link.addClass('selected');
      var pos = parseInt(link.attr('lb-position'),10);
      if (pos == 0) {
         $(".prev").css("display", "none");
	 $(".next").css("display", "block");
      }
      else if (pos == newborns.size() - 1) {
	 $(".next").css("display", "none");
         $(".prev").css("display", "block");
      }
      else {
         $(".prev").css("display", "block");
	 $(".next").css("display", "block");
      }
    });
    link.attr({'lb-position': index});
    link.attr({'lb-gallery': "newborn"});
  });

  babies.each(function(index) {
    var link = $(this);
    link.click(function(c) {
      c.preventDefault();
      open(link.attr('href'));
      babies.filter('.selected').removeClass('selected');
      link.addClass('selected');
      var pos = parseInt(link.attr('lb-position'),10);
      if (pos == 0) {
         $(".prev").css("display", "none");
	 $(".next").css("display", "block");
      }
      else if (pos == babies.size() - 1) {
	 $(".next").css("display", "none");
         $(".prev").css("display", "block");
      }
      else {
         $(".prev").css("display", "block");
	 $(".next").css("display", "block");
      }
    });
    link.attr({'lb-position': index});
    link.attr({'lb-gallery': "babies"});
  });

  kids.each(function(index) {
    var link = $(this);
    link.click(function(c) {
      c.preventDefault();
      open(link.attr('href'));
      kids.filter('.selected').removeClass('selected');
      link.addClass('selected');
      var pos = parseInt(link.attr('lb-position'),10);
      if (pos == 0) {
         $(".prev").css("display", "none");
	 $(".next").css("display", "block");
      }
      else if (pos == kids.size() - 1) {
	 $(".next").css("display", "none");
         $(".prev").css("display", "block");
      }
      else {
         $(".prev").css("display", "block");
	 $(".next").css("display", "block");
      }
    });
    link.attr({'lb-position': index});
    link.attr({'lb-gallery': "kids"});
  });

  family.each(function(index) {
    var link = $(this);
    link.click(function(c) {
      c.preventDefault();
      open(link.attr('href'));
      family.filter('.selected').removeClass('selected');
      link.addClass('selected');
      var pos = parseInt(link.attr('lb-position'),10);
      if (pos == 0) {
         $(".prev").css("display", "none");
	 $(".next").css("display", "block");
      }
      else if (pos == family.size() - 1) {
	 $(".next").css("display", "none");
         $(".prev").css("display", "block");
      }
      else {
         $(".prev").css("display", "block");
	 $(".next").css("display", "block");
      }
    });
    link.attr({'lb-position': index});
    link.attr({'lb-gallery': "family"});
  });
  
  var open = function(url) {
    if(container.is(':visible')) {
      target.children().fadeOut('normal', function() {
        target.children().remove();
        loadImage(url);
      });
    } else {
      target.children().remove();
      overlay.add(container).fadeIn('normal',function(){
        loadImage(url);
    	$(document).bind('keydown', keyHandler);
      });
    }
  }

  var keyHandler = function(c) {
     switch(c.keyCode) {
        case 81: // q
        case 88: // x
        case 27: // esc
           c.preventDefault();
           close.click();
           break;
        case 37: // left
           c.preventDefault();
           prev.click();
           break;
        case 39: // right
           c.preventDefault();
           next.click();
           break;
     }
  }
  
  var loadImage = function(url) {
    if(container.is('.loading')) { return; }
    container.addClass('loading');
    var img = new Image();
    img.onload = function() {
      img.style.display = 'none';
      
      var maxWidth = ($(window).width() - parseInt(container.css('padding-left'),10) - parseInt(container.css('padding-right'), 10)) - 100;
      var maxHeight = (($(window).height() > window.innerHeight ? window.innerHeight : $(window).height()) - parseInt(container.css('padding-top'),10) - parseInt(container.css('padding-bottom'), 10)) - 100;
      
      if(img.width > maxWidth || img.height > maxHeight) { // One of these is larger than the window
        var ratio = img.width / img.height;
        if(img.height >= maxHeight) {
          img.height = maxHeight;
          img.width = maxHeight * ratio;
        } else {
          img.width = maxWidth;
          img.height = maxWidth * ratio;
        }
      }
      
      container.animate({'width': img.width,'height': img.height, 'top': Math.round((($(window).height() > window.innerHeight ? window.innerHeight : $(window).height()) - img.height - parseInt(container.css('padding-top'),10) - parseInt(container.css('padding-bottom'),10)) / 2) + 'px', 'left': Math.round(($(window).width() - img.width - parseInt(container.css('padding-left'),10) - parseInt(container.css('padding-right'),10)) / 2) + 'px'},'normal', function(){
        target.append(img);
        $(img).fadeIn('normal', function() {
          container.removeClass('loading');
        });
      })
    }
    img.src = url;
  }
}

