var mid,
    popupVideoHTML;

var MultipleImgDisp = function() {
  var self = this;
  
  $("#prev").click(function() {self.prev();});
  $("#next").click(function() {self.next();});
};
MultipleImgDisp.prototype = {
  setDetails: function(ele, imgs, video, imgWidth) {
    var self = this,
        flashvars,
        params,
        attributes;
    
    this.ele = ele;
    this.imgs = [];
    this.imgWidth = imgWidth;
    $.each(imgs,  function(i, v) {
                    var aImg = new Image();
                    aImg.src = this;
                    self.imgs.push(aImg);
                  });
    this.title = ele.attr("title");
    this.curImg = 0;
    this.maxNum = imgs.length;
    
    $("#dwinTitle").html(this.title);
    $("#dwinImg").attr({src: this.imgs[0].src}).show();
    
    if (this.imgWidth && this.imgWidth !== '') {
      $('#dwinImg').css('width', this.imgWidth + 'px');
    }
    
    
    // Optional Prev and Next arrows to cycle through multiple images
    if (imgs.length > 1) {
      $("#imgCur").html("1");
      $("#imgMax").html(this.maxNum);
      $("#multipleImgs").show();
    }
    else {
      $("#multipleImgs").hide();
    }
    
    // Optional Video
    if (video && video !== "") {
      $("#popupVideo").show();
      
      flashvars = {};
      
      params = {};
      params.movie = video;
      params.allowFullScreen = "true";
      params.allowscriptaccess = "always";
      
      attributes = {};
      
      swfobject.embedSWF(video, "popupVideoFilm", "445", "364", "9.0.0","expressInstall.swf", flashvars, params, attributes);
      //alert("embeded");

    }
    else {
      $("#popupVideo").html("").hide();
    }
    
  },
  
  prev: function() {
    this.curImg -= 1;
    if (this.curImg < 0) {
      this.curImg = this.maxNum - 1;
    }
    this.dispImg();
  },
  
  next: function() {
    this.curImg += 1;
    if (this.curImg >= this.maxNum) {
      this.curImg = 0;
    }
    this.dispImg();
  },
  
  dispImg: function() {
    var img = this.imgs[this.curImg].src,
        self = this;

    $("#dwinImg").fadeOut(function() {
                            $("#imgCur").html(self.curImg + 1);
                            $(this).attr({src: img});
                            $(this).fadeIn();
                          });
  },
  
  onClose: function() {
    $("#dwinImg").hide().attr({src: ""});
    $('#dwinImg').css('width', 'auto');
    $("#popupVideo").hide().html(popupVideoHTML);
  }
};



$(function() {
  
  mid = new MultipleImgDisp();
  
  $(".singleimg").mousedown(function() {
                                        mid.setDetails($(this), [$(this).attr("img")]);
                                      });
                      
  // Open the template when the user clicks
  $(".singleimg, .multipleimgs").openDOMWindow({ 
    eventType:'click', 
    loader:1, 
    loaderImagePath:'../images/loadingAnimation.gif', 
    loaderHeight:13, 
    loaderWidth:208,
    width:850,
    height:920,
    fixedWindowY: 10,
    overlayColor: '#535233'
    
    }); 

  // Attach the close event
  $('.closeDOMWindow').closeDOMWindow({eventType:'click',
                                        functionCallOnClose: mid.onClose,
                                        functionCallAfterClose: mid.onClose
                                      });
                                      
  popupVideoHTML = $("#popupVideo").html();
  
  // Language Links
  $("#langLinks").css({opacity:0.3})
                  .show()
                  .hover( function() {$(this).css({opacity:1.0});},
                          function() {$(this).css({opacity:0.3});});
});

