/* ==========================================================
 * ouiPic
 * date: 18.07.2011
 * author: Sokolov Andrey
 * web: http://www.navigatex.ru or http://www.vk.com/sokolovan
 * email: oui-plagins@ya.ru
 * Free to use under the MIT license.
 * ========================================================== */
 
(function($) {$.fn.ouiPic = function(set){

 var set = $.extend({
  top: 'center',
  width: 80,
  height: 60,
  fullScreen: false,
  fullScreenBut: true,
  closeButText: "close",
  fullButText: "full screen"
 }, set);
 
 set.fullScreen ? set.fullScreenBut = true : "";
 
 var 
 prClass	= "ouipic",
 w 			= $(window),
 d 			= $(document),
 b 			= $("body"),
 h 			= w.height()-(set.height+20),
 min, src, move, x, y, type;
 

 
 $(this).addClass(prClass+"-checked");
 b.append("<div class=\"ouiPicOverlay\" /><div class=\"ouiPicImagesBlock\"><div class=\"ouiPicFullImg\" /><div class=\"ouiPicClose\" /><div class=\"ouiPicFullScreen\" /><div class=\"ouiPicPrewImg\"><ul class=\"ouiPicUl\" /></div></div>"); 
 
 var
 ob 			= $("."+prClass+"-checked"),
 img_overlay	= $(".ouiPicOverlay"),
 img_block 		= $(".ouiPicImagesBlock"),
 img_fulldiv	= $(".ouiPicFullImg"),
 img_prev 		= $(".ouiPicPrewImg"),
 img_ul			= $(".ouiPicUl"),
 img_close		= $(".ouiPicClose"),
 img_full		= $(".ouiPicFullScreen");
 
 img_ul.empty();
 ob.each(function(){
  min = $(this).attr("data-min");
  img_ul.append("<li><a style=\"background: url("+(!min ? $(this).attr("href") : min)+") no-repeat center "+set.top+"; width: "+set.width+"px; height: "+set.height+"px\" href=\""+$(this).attr("href")+"\"></a></li>");
 });
 
 img_ul.width(ob.size()*(set.width+10));
 
 img_block.height(w.height());
 
 img_close
  .html("<div>"+set.closeButText+"</div>")
  .css("opacity","0.6")
  .children().css("opacity", 0);
  
 img_full
  .html("<div>"+set.fullButText+"</div>")
  .children().css("opacity", 0);
 
 /* handlers */
 w.resize(function(){
  img_block.height(w.height());
  picFullToggle($("img", img_fulldiv).width(), $("img", img_fulldiv).height(), set.fullScreen);
 });
 
 d.delegate("."+prClass+"-checked", "mousedown", function(){
  picShow($(this));
  return false;
 });
 
 img_fulldiv.delegate("img", "mousedown", function(e){
  if(!set.fullScreen){showNextImg();}
  else{
   move=true;
   y = e.pageY-parseFloat(img_fulldiv.css("margin-top").replace("px", ""));
   x = e.pageX-parseFloat(img_fulldiv.css("margin-left").replace("px", ""));
  }
 });
 
 d.mousemove(function(e){
  if(set.fullScreen && move && type){
   ((img_fulldiv.height()+(e.pageY-y))>=h) ? img_fulldiv.css({"margin-top": (e.pageY-y<0 ? e.pageY-y : "")}) : "";
   ((img_fulldiv.width()+(e.pageX-x))>=w.width()) ? img_fulldiv.css({"margin-left": (e.pageX-x<0 ? e.pageX-x : "")}) : "";
  }
  return false;
 }).mouseup(function(){
  move = false;
 });
 
 img_close
 .live("mousedown", function(){
  picClose();
 })
 .live("mouseenter", function(){
  $(this).stop().animate({opacity: 1},200).children().animate({opacity: 1},200);
 })
 .live("mouseleave", function(){
  $(this).stop().animate({opacity: 0.6},200).children().animate({opacity: 0},200);
 });
 
 img_full
 .live("mousedown", function(){
  set.fullScreen = ($(this).is(".ouiPicFullScreenAct") ? false : true);
  $(this).toggleClass("ouiPicFullScreenAct");
  $("a[href='"+($("img", img_fulldiv).attr("src"))+"']", img_ul).mousedown();
 })
 .live("mouseenter", function(){
  $(this).stop().animate({opacity: 1},200).children().animate({opacity: 1},200);
  return false;
 })
 .live("mouseleave", function(){
  $(this).stop().animate({opacity: 0.6},200).children().animate({opacity: (set.fullScreen ? 0.6 : 0)},200);;
  return false;
 });
 
 img_ul
 .delegate("a", "mousedown", function(){
  getImg($(this).attr("href"));
 })
 .live("click", function(e){
  e.preventDefault();
 });
 
 d.keydown(function(e){
  if(e.keyCode==27){   picClose();  }
  if(e.keyCode==37 || e.keyCode==40){   showNextImg("left");  }
  if(e.keyCode==39 || e.keyCode==38){   showNextImg();  }
 });

 /* functions */
 function showFullBut(){
  img_full.children().css({opacity: (set.fullScreen ? 1 : 0)});
  (type && set.fullScreenBut) || (!type && set.fullScreen) ? img_full.show().css("opacity","0.6") : img_full.hide();
 }
 
 function picShow(a){
  img_overlay.show().height(d.height());
  img_block.show();
  img_fulldiv.empty();
  getImg(a.attr("href"));
  toggleClass(a.attr("href"));
 }
 
 function picClose(){
  img_overlay.hide();
  img_block.hide();
 }
 
 function getImg(src){
  var img = new Image;
  img.src = src;
  if(img.width && img.height){
   getImgSrc(src, img);  
  }
  else{
   img_fulldiv.empty();
   img.onload = function(){
    getImgSrc(src, img);
   }  
  }
  /*
  if(img.width && img.height){

  }
  else {
   setTimeout(function(){getImg(src);}, 50);
  }*/
 }
function getImgSrc(src, img){ 
    img_fulldiv.html("<img ondragstart=\"return false;\" src=\""+src+"\" />").children().css("opacity", 0).animate({opacity: 1}, 100);
    picFullToggle(img.width, img.height, set.fullScreen);
    toggleClass(src);
    anim_ul(); 
}

 function picFullToggle(width, height, act){
  type = (height>h || width>w.width() ? true : false);
  showFullBut();
  if(act==true){
   img_fulldiv.css({
		height: height, 
		width: width, 
		"margin-top": (img_block.height()-height-(set.height+20))/2,
		"margin-left": (width>w.width() ? (w.width()-width)/2 : "auto"),
		cursor: "move"
   });
  }else{ 
   img_fulldiv.css({
		height: (height>width ? (height>h ? h : height) : (height>h ? h : height)),
		width: (height<width && width>w.width() ? (height>h ? width*(h/height) : w.width()) : (height>h ? width*(h/height) : width)),
		"margin": (height>h ? 0 : (img_block.height()-height-(set.height+20))/2)+"px auto auto",
		cursor: "pointer"
   });
  }
 }
 
 function anim_ul(){
  if(img_ul.width()>w.width()){
   var num = $("li", img_ul).index($(".ouiPicPrewImgAct"));
   var ml = num*(set.width+9.5)-w.width()/2;
   ml = (ml > img_ul.width()-w.width() ? img_ul.width()-w.width() : ml);
   img_ul.stop().animate({marginLeft: -(ml<0 ? 0 : ml)});
  }
 }
 
 function toggleClass(href){
  $(".ouiPicPrewImgAct").removeClass("ouiPicPrewImgAct");
  img_ul.find("li a[href='"+href+"']").parent().addClass("ouiPicPrewImgAct"); 
 }
 
 function showNextImg(left){
  if(!left){
   $(".ouiPicPrewImgAct").next()[0] ? $(".ouiPicPrewImgAct").next().children().mousedown() : img_ul.find("li:first a").mousedown();
  }else{
   $(".ouiPicPrewImgAct").prev()[0] ? $(".ouiPicPrewImgAct").prev().children().mousedown() : img_ul.find("li:last a").mousedown();
  }
 }
 
 };
})(jQuery);
