function zoomerContainerSize() {
  // Sets the container size for all zoomers on the page
  
  $('.zoomer').each(function(){
    
    var maskX=400; // Mask width
    var maskY=600; // Mask height
    
    var imgX=$(this).find('img').width(); // Image width
    var imgY=$(this).find('img').height(); // Image height
    
    var diffX=Math.floor(imgX-maskX);
    var diffY=Math.floor(imgY-maskY);
    
    //alert(diffY);
    
    // Set the width and height of the container
    $(this).find('.zoomer-container').css('width',maskX+(2*diffX));
    $(this).find('.zoomer-container').css('height',maskY+(2*diffY));
    
    // Set absolute position of the container
    if(diffX > 0) {
      $(this).find('.zoomer-container').css('left','-' + diffX + 'px');
    }else{
      $(this).find('.zoomer-container').css('left','0px');
    }
    
    if(diffY > 0) {
      $(this).find('.zoomer-container').css('top','-' + diffY + 'px');
    }else{
      $(this).find('.zoomer-container').css('top','0px');
    }
        
  });
  
}

function zoomerCentraliseImage(elemid) {
  // Centralise the image within the zoomer

  $('.zoomer#' + elemid).each(function() {
    
    var maskX=400; // Mask width
    var maskY=600; // Mask height
    
    var imgX=$(this).find('img').width(); // Image width
    var imgY=$(this).find('img').height(); // Image height
    
    var diffX=Math.floor(imgX-maskX);
    var diffY=Math.floor(imgY-maskY);
    
    // Move the image to make it align nicely in the middle
    $(this).find('img').css('left', Math.floor(diffX / 2) + 'px');
    $(this).find('img').css('top', Math.floor(diffY / 2) + 'px');
    
  });
  
}



//****************************
// Requires pathinfo include!!
//****************************

function isArray(obj) {
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}

function zoomer_getPaths(elemid){
  
  var obj=$('.zoomer#' + elemid);

  // Grab current source
  var src=obj.find('img').attr('src');
  var dir=dirname(src);
  var file=basename(src, '.jpg');
  
  var zoom=file.split('_');
  if(zoom.length > 1){
    //alert('pt1');
    var id=zoom[0];
    zoom=zoom[1].split('');
    zoom=zoom[1];
  }else{
    var id=zoom[0];
    zoom=0;
  }
  
  return {
    src: src,
    dir: dir,
    basename: file,
    zoom: zoom,
    id: id
  }
}

function single_zoomer_getPaths(){
  
  var obj=$('#singlebrochure');

  // Grab current source
  var src=obj.find('img').attr('src');
  var dir=dirname(src);
  var file=basename(src, '.jpg');
  
  var zoom=file.split('_');
  if(zoom.length > 1){
    //alert('pt1');
    var id=zoom[0];
    zoom=zoom[1].split('');
    zoom=zoom[1];
  }else{
    var id=zoom[0];
    zoom=0;
  }
  
  return {
    src: src,
    dir: dir,
    basename: file,
    zoom: zoom,
    id: id
  }
}

function zoomer_zoomIn(elemid) {
  
  var paths=zoomer_getPaths(elemid);
  
  if(paths.zoom == 2) {
    // Highest zoom level already reached so abort
    return;
  }
  
  if(paths.zoom == 0){
    // Proceeding to zoom 1
    var newSrc=paths.dir + '/' + paths.id + '_z1.jpg';
    var newX=600;
    var newY=900;
  }
  
  if(paths.zoom == 1){
    // Proceeding to zoom 2
    var newSrc=paths.dir + '/' + paths.id + '_z2.jpg';
    var newX=900;
    var newY=1350;
  }
  
  
  // Change the source of hte image and the size attributes
  $('.zoomer#' + elemid).find('img').attr('src',newSrc).attr('width',newX).attr('height',newY);
  
  // change the container size for the new restrictions
  zoomerContainerSize();
  
  // Centralise the new image in the new container
  zoomerCentraliseImage(elemid);
  
}

function zoomer_zoomOut(elemid) {
  
  var paths=zoomer_getPaths(elemid);
  
  if(paths.zoom == 0) {
    // Lowest zoom level already reached so abort
    return;
  }
  
  if(paths.zoom == 2){
    // Proceeding to zoom 1
    var newSrc=paths.dir + '/' + paths.id + '_z1.jpg';
    var newX=600;
    var newY=900;
  }
  
  if(paths.zoom == 1){
    // Proceeding to zoom 0
    var newSrc=paths.dir + '/' + paths.id + '.jpg';
    var newX=400;
    var newY=600;
  }
  
  
  // Change the source of hte image and the size attributes
  $('.zoomer#' + elemid).find('img').attr('src',newSrc).attr('width',newX).attr('height',newY);
  
  // change the container size for the new restrictions
  zoomerContainerSize();
  
  // Centralise the new image in the new container
  zoomerCentraliseImage(elemid);
  
}


function nextImgs() {
  
  var paths1=zoomer_getPaths('image1');
  var paths2=zoomer_getPaths('image2');

  var collect=false;

  for(key in brochurePics) {
    if(brochurePics[key] == paths1.dir + '/' + paths1.id + '.jpg'){
      // Got the last image, the next 2 are collectables
      var currentKey=key;
    }

  }
  
  var new1=parseInt(currentKey)+2;
  var new2=parseInt(currentKey)+3;
  
  if(new1 > (brochurePics.length-1)){
    return; // End of images
  }
  
  if(new2 > (brochurePics.length-1)){
    // Only move one place
    new2--;
    new1--;
  }
    
  new1=brochurePics[new1];
  new2=brochurePics[new2];
  
  $('.zoomer#image1').find('img').attr('src',new1);
  $('.zoomer#image2').find('img').attr('src',new2);
  
  $('.zoomer#image1').find('img').attr('width','400').attr('height','600');
  $('.zoomer#image2').find('img').attr('width','400').attr('height','600');
  zoomerContainerSize();
  zoomerCentraliseImage('image1');
  zoomerCentraliseImage('image2');
  
  
}

function prevImgs() {
  
  var paths1=zoomer_getPaths('image1');
  var paths2=zoomer_getPaths('image2');
  
  if($('.zoomer#image1').find('img').attr('src')==brochurePics[0]){
    // At the beginning so return false
    return;
  }
  
  for(key in brochurePics) {
    
    if(paths1.dir + '/' + paths1.id + '.jpg'==brochurePics[key]){
      var current1=key;
    }
    if(paths2.dir + '/' + paths2.id + '.jpg'==brochurePics[key]){
      var current2=key;
    }
  }
  
  var new1=current1-2;
  var new2=current2-2;
  
  if(new1 < 0){
    new1=0;
    new2=1;
  }
  
  new1=brochurePics[new1];
  new2=brochurePics[new2];
  
  $('.zoomer#image1').find('img').attr('src',new1);
  $('.zoomer#image2').find('img').attr('src',new2);
  
  $('.zoomer#image1').find('img').attr('width','400').attr('height','600');
  $('.zoomer#image2').find('img').attr('width','400').attr('height','600');
  zoomerContainerSize();
  zoomerCentraliseImage('image1');
  zoomerCentraliseImage('image2');
  
}


$(document).ready(function(){
  
  // Singles
  if(singleBrochurePics.length > 0) {
    singleBrochurePics=singleBrochurePics.split(',');
    
    // First 2
    $('#singlebrochure').find('img').attr('src',singleBrochurePics[0]);    
  }
  
  
  // Load the pictures into the places
  if(brochurePics.length > 0) {
    brochurePics=brochurePics.split(',');
    
    // First 2
    $('.zoomer#image1').find('img').attr('src',brochurePics[0]);
    $('.zoomer#image2').find('img').attr('src',brochurePics[1]);
    
  }
  
  
  
  // Centralise the logo image
  $('#logoPic').css('left',(400 - Math.floor(($('#logoPic').width()/2))));
  
  
  // Initialise the zooming
  
  $('.zoomer').each(function() {
    
    zoomerContainerSize(); // Sets the size of the zoom containers
    $(this).find('img').draggable({ containment: 'parent' }); // Makes the image draggable within the container
    zoomerCentraliseImage($(this).attr('id'));

  });
  
  $('.zoomer-zoomIn').each(function() {
    $(this).click(function(e) {
      e.preventDefault();
      
      var refId=$(this).attr('rel');
      zoomer_zoomIn(refId);
      
    })
  });
  
  $('.zoomer-zoomOut').each(function() {
    $(this).click(function(e) {
      e.preventDefault();
      
      var refId=$(this).attr('rel');
      zoomer_zoomOut(refId);
      
    })
  });
  
  $('.zoomer-next').click(function(e) {
    e.preventDefault();
    nextImgs();
  });
  
  $('.zoomer-prev').click(function(e) {
    e.preventDefault();
    prevImgs();
  });
  
  $('.single-zoomer-next').click(function(e) {
    e.preventDefault();
    singleNextImgs();
  });
  
  $('.single-zoomer-prev').click(function(e) {
    e.preventDefault();
    singlePrevImgs();
  });
  
});

function singleNextImgs() {
    
  for(key in singleBrochurePics){
    if(singleBrochurePics[key]==$('#singlebrochure img').attr('src')){
      var currentKey=key;      
    }
  }
  
  var newKey=parseInt(currentKey) + 1;
  if(newKey > (singleBrochurePics.length - 1)){
    newKey=(singleBrochurePics.length - 1);
  }  
  $('#singlebrochure img').attr('src',singleBrochurePics[newKey]);
  
}

function singlePrevImgs() {
  
  for(key in singleBrochurePics){
    if(singleBrochurePics[key]==$('#singlebrochure img').attr('src')){
      var currentKey=key;
    }
  }
  
  var newKey=parseInt(currentKey) - 1;
  if(newKey < 0){
    newKey=0;
  }
  
  $('#singlebrochure img').attr('src',singleBrochurePics[newKey]);
  
}