/***************************/
//@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!					
/***************************/

var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

function jsddm_open() {  
	jsddm_canceltimer();
	jsddm_close();
	ddmenuitem = $(this).find('ul').css('visibility', 'visible');
}

function jsddm_close() {  
	if(ddmenuitem) 
		ddmenuitem.css('visibility', 'hidden');
}

function jsddm_timer() {  
	closetimer = window.setTimeout(jsddm_close, timeout);
}

function jsddm_canceltimer() {
	if(closetimer) {
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

document.onclick = jsddm_close;

$(document).ready(function(){

	$('.blogmenu > li').bind('mouseover', jsddm_open);
   	$('.blogmenu > li').bind('mouseout',  jsddm_timer);

	$('.menu a[@href$="/classes/"]').attr({href: "http://www.focusbymjw.com/" });
	$(".menu li").click(function(){
	  window.location=$(this).find("a").attr("href"); return false;
	});

	$(".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();
  vidbox();

     //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;
  }
}

function vidbox() {
  var vidlinks = $('a[rel^=vidbox]');
  var vids = $('a[rel^=vidbox1]');
  var overlayvid = $(jQuery('<div id="overlayvid" style="display: none"></div>'));
  var containervid = $(jQuery('<div id="lightboxvid" style="display: none"></div>'));
  var closevid = $(jQuery('<a href="#close" class="closevid">&times; Close</a>'));
  var targetvid = $(jQuery('<div class="targetvid"></div>'));
  var prevvid = $(jQuery('<a href="#prev" class="prevvid">&laquo; Previous</a>'));
  var nextvid = $(jQuery('<a href="#next" class="nextvid">Next &raquo;</a>'));

  $('body').append(overlayvid).append(containervid);
  containervid.append(closevid).append(targetvid); //.append(prevvid).append(nextvid);
  containervid.show().css({'top': Math.round((($(window).height() > window.innerHeight ? window.innerHeight : $(window).height()) - containervid.outerHeight()) / 2) + 'px', 'left': Math.round(($(window).width() - containervid.outerWidth()) / 2) + 'px', 'margin-top': 0, 'margin-left': 0}).hide();
  closevid.click(function(c) {
    c.preventDefault();
    overlayvid.add(containervid).fadeOut('normal');
    $(document).unbind('keydown', vidkeyHandler);
    vidlinks.filter('.selected').removeClass('selected');
  });

  prevvid.add(nextvid).click(function(c) {
    c.preventDefault();
    var gallery = vidlinks.filter('.selected').attr('lb-gallery');
    if (gallery == "vids")
	locallinks = vids;

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

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

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

  var vidopen = function(url) {
    if(containervid.is(':visible')) {
      targetvid.children().fadeOut('normal', function() {
        targetvid.children().remove();
        loadVid(url);
      });
    } else {
      targetvid.children().remove();
      overlayvid.add(containervid).fadeIn('normal',function(){
        loadVid(url);
    	$(document).bind('keydown', vidkeyHandler);
      });
    }
  }

  var vidkeyHandler = function(c) {
     switch(c.keyCode) {
        case 81: // q
        case 88: // x
        case 27: // esc
           c.preventDefault();
           closevid.click();
           break;
        case 37: // left
           c.preventDefault();
           prevvid.click();
           break;
        case 39: // right
           c.preventDefault();
           nextvid.click();
           break;
     }
  }
  
  var loadVid = function(url) {
    if(containervid.is('.loading')) { return; }
    containervid.addClass('loading');
    var img = new Image();
    var imgwidth = 854;
    var imgheight = 480;
    var vidobj1 = $(jQuery('<iframe id="vp1YcJYE" title="Video Player" width="854" height="474" frameborder="0" src="http://embed.animoto.com/play.html?w=swf/vp1&e=1309044885&f=YcJYEgXCCwYWV0Zq615ymg&d=388&m=p&r=240p+480p&volume=100&start_res=480p&i=m&ct=MJW%20Photography&cu=http://www.mjwphotography.com&options=autostart" allowfullscreen></iframe>'));

    var vidobj;

    vidobj = vidobj1;
      
    containervid.animate({'width': imgwidth,'height': imgheight, 'top': Math.round((($(window).height() > window.innerHeight ? window.innerHeight : $(window).height()) - imgheight - parseInt(containervid.css('padding-top'),10) - parseInt(containervid.css('padding-bottom'),10)) / 2) + 'px', 'left': Math.round(($(window).width() - imgwidth - parseInt(containervid.css('padding-left'),10) - parseInt(containervid.css('padding-right'),10)) / 2) + 'px'},'normal', function(){
        targetvid.append(vidobj);
        $(vidobj).fadeIn('normal', function() {
          containervid.removeClass('loading');
        });
      })
    }
}


