var currentImage;
var nextImage;
var prevImage;
var currentNode;
var nodeOnNextImage;
var nodeOnPrevImage;
var currentThumbPos = 0;
var infoShowing = false;
var ignoreHachChange = false;



function showMe($object) {
  $($object).delay(1000).fadeTo(500,1);
}
function showNextImage() {
  ignoreHachChange = true;
  currentImage ++;
  if (currentImage > images[currentNode].length - 1) {
    if (currentNode == nodes[nodes.length-1]) {
      currentNode = nodes[0];
    } else {
      for (var i in nodes) {
        if (currentNode == nodes[i]) {
          currentNode = nodes[++i];
          break;
        }
      }
    }
    currentImage = 0;
    document.location.hash = '/' + currentNode + '/' + imagesPk[currentNode][0];
  } else {
    document.location.hash = '/' + currentNode + '/' + imagesPk[currentNode][currentImage];
  }
  getNextAndPrevImages();
  fadeImage();
}

function showPrevImage() {
  ignoreHachChange = true;
  currentImage --;
  if (currentImage < 0) {
    if (currentNode == nodes[0]) {
      currentNode = nodes[nodes.length-1];
    } else {
      for (var i in nodes) {
        if (currentNode == nodes[i]) {
          currentNode = nodes[--i];
          break;
        }
      }
    }
    currentImage = images[currentNode].length-1;
    document.location.hash = '/' + currentNode + '/' + imagesPk[currentNode][imagesPk[currentNode].length-1];
  } else {
    document.location.hash = '/' + currentNode + '/' + imagesPk[currentNode][currentImage];
  }
  getNextAndPrevImages();
  fadeImage();
}

function showImage() {
  //currentThumbPos = Math.floor((currentImage)/7);
  fadeImage();
}

function fadeImage() {
  if (images[currentNode].length > 0) {
    $('#images-wrapper').show();
    /*if (document.location.hash !="" || currentImage != 0) {
      document.location.hash = imagesPk[currentImage];
    }*/
    var $show_image = $('<img />').attr('src', images[currentNode][currentImage]).attr('width', imagesWidth[currentNode][currentImage]).css('left','-' + (imagesWidth[currentNode][currentImage]/2) + 'px');
    $('#image img').delay(60).fadeTo(500,0, function() {
        $(this).remove();
        ignoreHachChange = false;
    });
    $('#image').append($show_image);
    scaleImage();
    $show_image.fadeTo(0,0);
    $show_image.delay(200).fadeTo(500, 1);
    
    //Info-texten
    $('#info-text').text(info[currentNode]);
    Cufon.replace('#info-text');
    
    //Nästa och föregående
    var imageNext = $('<img />').attr('src',  images[nodeOnNextImage][nextImage]);
    var imagePrev = $('<img />').attr('src',  images[nodeOnPrevImage][prevImage]);
    $('#preloader').empty();
    $('#preloader').append(imageNext);
    $('#preloader').append(imagePrev);
    
    

    //$('#image').css("background-image", "url(" + images[currentImage] + ")");
  }
}


function setCursor(e) {
  var x = e.pageX - $('#image').offset().left;
  if (x<0) {
    $('#image').css('cursor','url(/left.cur), pointer');
  } else {
    $('#image').css('cursor','url(/right.cur), pointer');
  }
}

function scaleFrontpageWrapper() {
  if (!isMobile()) {
    $('#frontpage-wrapper').height($(window).height() - $('#frontpage-wrapper').offset().top);
  }
}
function scaleImage() {
  if ($('#image img').length > 0) {
    $wHeight = $(window).height();
    $maxHeight = Math.max(300,$wHeight - $('#top-wrapper').outerHeight() - 60);
    $multiplyer = $maxHeight / 600;
    $('#image img:last').css('max-height',$maxHeight);
    $('#image img:last').css('max-width',Math.round(imagesWidth[currentNode][currentImage] * $multiplyer));
    $('#image img:last').css('left',-Math.min(imagesWidth[currentNode][currentImage] /2, Math.round(imagesWidth[currentNode][currentImage] * $multiplyer/2)));
    $('#image').css('max-height',$maxHeight);
    $('#clicker').css('max-height',$maxHeight);
  }
}

function getNodeAndAnchor() {
  if (document.location.hash.split('#').length >= 2 && document.location.hash != "#/home") {
    currentNode = document.location.hash.split('#')[1].split('/')[1];
    if (document.location.hash.split('#')[1].split('/').length >= 3) {
      anchor = parseInt(document.location.hash.split('#')[1].split('/')[2]);
      // Hämtar vilket bildnummer som man länkat till
      for (var i in imagesPk[currentNode]) {
        if (anchor == imagesPk[currentNode][i]) {
          currentImage = i;
          break;
        }
      }
      getNextAndPrevImages();
      $('#home').show();
    } else {
      currentImage = 0;
      anchor = imagesPk[currentNode][0];
    }
  } else {
    currentNode = '';
    anchor = '';
    $('#home').hide();
  }
}

function getNextAndPrevImages() {
  // Tar fram nästa och föregående bild
  if (currentImage == 0) {
    nodeOnNextImage = currentNode;
    nextImage = parseInt(currentImage) + 1;
    if (currentNode == nodes[0]) {
      nodeOnPrevImage = nodes[nodes.length-1];
      prevImage = images[nodeOnPrevImage].length -1;
    } else {
      for (var i in nodes) {
        if (currentNode == nodes[i]) {
          nodeOnPrevImage = nodes[i-1];
          prevImage = images[nodeOnPrevImage].length -1;
        }
      }
    }
  } else if (currentImage == imagesPk[currentNode].length - 1) {
    nodeOnPrevImage = currentNode;
    prevImage = currentImage - 1;
    if (currentNode == nodes[nodes.length-1]) {
      nodeOnNextImage = nodes[0];
      nextImage = 0;
    } else {
      for (var i in nodes) {
        if (currentNode == nodes[i]) {
          nodeOnNextImage = nodes[parseInt(i)+1];
          nextImage = 0;
        }
      }
    }
  } else {
    nodeOnNextImage = currentNode;
    nodeOnPrevImage = currentNode;
    nextImage = parseInt(currentImage) + 1;
    prevImage = currentImage - 1;
  }
}

$(document).ready(function() {
  
  //Cufon.replace('a', {hover:true});
  Cufon.replace('.contact-link');
  
  var currentImage = 0;
  
  getNodeAndAnchor();

  
  //Kopplar eventlyssnare
  $(window).resize(scaleFrontpageWrapper);
  scaleFrontpageWrapper();
  $(window).resize(scaleImage);
  scaleImage();
  
  //Navigation
  getNodeAndAnchor();
  $('#frontpage-wrapper a').click(function() {
    ignoreHachChange = true;
    $('#frontpage-wrapper').fadeTo(500,0,function() {
      $('#frontpage-wrapper').hide();
      getNodeAndAnchor();
      getNextAndPrevImages();
      showImage();
      ignoreHachChange = false;
    });
  });
  $(window).hashchange(function() {
    if (!ignoreHachChange) {
      if (document.location.hash != '#/home' && document.location.hash != '#' && document.location.hash != '') {
        if ($('#frontpage-wrapper').css('display') != 'none') {
          $('#frontpage-wrapper').fadeTo(500,0,function() {
            $('#frontpage-wrapper').hide();
          });
        }
        getNodeAndAnchor();
        showImage();
      } else {
        $('#frontpage-wrapper').fadeTo(500,1);
        if (document.location.hash == '#/home') {
          ignoreHachChange = true;
          document.location.hash = '';
          ignoreHachChange = false;
        }
        $('#home').hide();
      }
    }
  });
  //getNodeAndAnchor();
  
  if (currentNode != '' && anchor != '') {
    $('#frontpage-wrapper').hide();
    getNextAndPrevImages();
    showImage();
  }
  

  $('#image img').mousemove(setCursor);
  $('#image').mousemove(setCursor);
  $('#image').click(setCursor);

  $('#info-btn').click(function() {
    if (infoShowing) {
      $('#info-text').fadeTo(200,0);
      infoShowing = false;
    } else {
      $('#info-text').fadeTo(200,1);
      infoShowing = true;
    }
  });
  $('#image').click(function(eventObj) {
    var location = $(this).offset();
    var x = eventObj.pageX - location.left;
    if (x < $(this).width() / 2) {
      showPrevImage();
    } else {
      showNextImage();
    }
    //showNextImage();
  });

  $('#next-image').click(function() {
    showNextImage();
  });
  $('#prev-image').click(function() {
    showPrevImage();
  });

  $('.go-home').click(function() {
    $('#image img').fadeTo(300,0);
  });
  
  $('.portrait-image').each(function() {
    var $height = (Math.ceil($(this).find('img').height() / 20)) * 20;
    $(this).height($height);
  });
  
  // Splash-screen
  if (document.location.hash != "#/home") {
    $('#white-cover img').delay(1500).fadeTo(1500,1, function () {
      $('#white-cover img').fadeTo(1500,0, function () {
        $('#white-cover').fadeTo(1500,0, function () {
          $('#white-cover').hide();
        });
      });
    });
  } else {
    $('#white-cover').hide();
      ignoreHachChange = true;
      document.location.hash = '';
      ignoreHachChange = false;
  }

  if (browserIE.version() <= 7) {
    if (pageType == 'node_page') {
      $('#contact-wrapper').each( function() {
        var child = $(this).find('.inline-contact-wrapper');
        var childHeight = child.outerHeight();
        $(this).find('.inline-contact-wrapper').css('top',($(this).height() - childHeight)/2);
        $(this).find('.inline-contact-wrapper').css('display','block');
      });
    }
  }



});


