var site_menus = {
  'beauty':{'mElement':'site-menu-03', 'mDefaultImg':'Master03.png', 'mOnImg':'Master_on03.png', 'type':'photos'},
  'fashion':{'mElement':'site-menu-04', 'mDefaultImg':'Master04.png', 'mOnImg':'Master_on04.png', 'type':'photos'},
  'female':{'mElement':'site-menu-08', 'mDefaultImg':'Master08.png', 'mOnImg':'Master_on08.png', 'type':'photos'},
  'male':{'mElement':'site-menu-05', 'mDefaultImg':'Master05.png', 'mOnImg':'Master_on05.png', 'type':'photos'},
  'record':{'mElement':'site-menu-06', 'mDefaultImg':'Master06.png', 'mOnImg':'Master_on06.png', 'type':'photos'},
  'life':{'mElement':'site-menu-07', 'mDefaultImg':'Master07.png', 'mOnImg':'Master_on07.png', 'type':'photos'},
  'celebrity':{'mElement':'site-menu-09', 'mDefaultImg':'Master09.png', 'mOnImg':'Master_on09.png', 'type':'photos'},
  'contact':{'mElement':'site-menu-contact', 'mDefaultImg':'contact.png', 'mOnImg':'contact_on.png', 'type':'info'}
};
var currentPhotoSet = [];
var currentPhotoFName = '', currentSection = '';
var isSettingArrow = false;
var isChangeSection = false;
$(document).ready(function() {
  $('.maintable').css('width', $('.side-bar').width() + $('.main-block').width());
  $('#td_contant').css('height', $('.main-block').height() - $('#td_sitebarmenu').height() - 150 - 20);
  currentSection = location.search.replace(/^\?/, '');
  currentPhotoFName = location.hash.replace(/^#/, '');
  //if (currentSection == '') currentSection = 'beauty';
  if (currentSection !== 'contact' && currentSection !== '') {
    if (currentPhotoFName !== '') {
      var tid = $.inArray(currentPhotoFName, thumb_photos[currentSection]);
      if ( tid >= 0) {
        $('<div id=\"photo-div-' + currentPhotoFName + '\" class=\"hidediv\"><img src=\"photos/' + currentPhotoFName + '_o.jpg\"><\/div>')
          .onImagesLoad({
            selectorCallback: function() {
              $('#photo-div-' + currentPhotoFName).fadeIn('slow');
              $('.goleftphoto').click(function() { goPhoto(-1); });
              $('.gorightphoto').click(function() { goPhoto(1); });
              $('#loadingImage').fadeOut('slow').remove();
            }
          }).appendTo('#main-photo-show');
      }
    }
    else {
      currentPhotoFName = thumb_photos[currentSection][0];
      changeLocation();
    }
  }
  else {
    if (currentSection == '') {
      currentSection = 'home';
      $('#main-photo-show').css('height', '601px');
      $('#sub-photo-show').css('margin', '0');
    }
    $('#sub-photo-show').hide();
    $('<div id=\"photo-div-' + currentSection + '\" class=\"hidediv\"><img width=\"867\" src=\"images/' + thumb_photos[currentSection][0] + '_information.jpg\"><\/div>').onImagesLoad({
      selectorCallback: function() {
        if (currentSection == 'contact') {
          var position = $('#main-photo-show').position();
          $('.contact_mail_link')
            .css('left', position.left + 505)
            .css('top', position.top + 144 + 10)
            .show();
        }
        if (currentSection == 'home') $('#photo-div-' + currentSection).css('height', '601px');
        $('#photo-div-' + currentSection).fadeIn('slow');
        $('#loadingImage').fadeOut('slow').remove();
      }
    }).appendTo('#main-photo-show');
  }
  calcEmptyDivPosition();
  setSection();
});

$(window).resize(function() {
  calcEmptyDivPosition();
});
$(window).load(function() {
  if (currentSection !== 'contact' && currentSection !== 'home') makePhotos();
});
function makePhotos() {
  isChangeSection = false;
  isSettingArrow = false;
  $('#sub-photo-show div:first').css('cursor','default').unbind();
  $('#sub-photo-show div:last').css('cursor','default').unbind();
  currentPhotoSet = thumb_photos[currentSection].join(',').split(',');
  addPhoto();
}
function addPhoto() {
  if (currentPhotoSet.length > 0) {
    var tid = currentPhotoSet.splice(0, 1);
    if ($('#photo-div-' + tid).length == 0) {
      var $photodivElement = $('<div id=\"photo-div-' + tid + '\" class=\"hidediv\"><img src=\"photos/' + tid + '_o.jpg\"><\/div>');
      $photodivElement.onImagesLoad({
        selectorCallback: addthumb
      });
    }
    else {
      addthumb($('#photo-div-' + tid));
    }
    if (currentPhotoFName == '') {
      currentPhotoFName = tid;
    }
  }
}
function addthumb($selector) {
  var photoname = $selector[0].id.replace(/^photo-div-/, '');
  if ($('#photo-div-' + photoname).length == 0) $selector.appendTo('#main-photo-show');
  if ($('#thumb-img-' + photoname).length == 0) {
    var $newthumb = $('<li id=\"thumb-li-' + photoname + '\"><img id=\"thumb-img-' + photoname + '\" src=\"photos/' + photoname + '_s.jpg\" height=\"31\"></li>');
    $newthumb.onImagesLoad({selectorCallback: afterAddthumb});
  }
  else {
    afterAddthumb($('#thumb-li-' + photoname));
  }
}
function afterAddthumb($selector) {
  var photoname = $selector[0].id.replace(/^thumb-li-/, 'thumb-img-');
  $selector.css('opacity','0').appendTo('#ul-div ul');
  if ($('#ul-div ul li').length == 1)
    $selector.css('margin','0');
  $('#' + photoname).attr('width', $('#' + photoname).width()).click( function() {changePhoto (this.id);} );
  $selector.fadeTo(200, 1);
  if (isChangeSection == false) {
    isChangeSection = true;
    $('#photo-div-' + currentPhotoFName).fadeIn('slow');
    if ($('#loadingImage').length > 0) $('#loadingImage').fadeOut('slow').remove();
    //changeLocation();
  }
  checkArrowOpen();
  addPhoto();
}
function changePhoto(photoid) {
  var newPhotoFName = photoid.replace(/^thumb-img-/, '');
  if (newPhotoFName !== currentPhotoFName) {
    $('.goleftphoto').unbind();
    $('.gorightphoto').unbind();
    $('#photo-div-' + currentPhotoFName).fadeOut(200, function() {
      $('#photo-div-' + newPhotoFName).fadeIn(200);
      $('.goleftphoto').click(function() { goPhoto(-1); });
      $('.gorightphoto').click(function() { goPhoto(1); });
    });
    currentPhotoFName = newPhotoFName;
    changeLocation();
  }
}
function changeLocation() {
  var baselocation = '?';
  baselocation += currentSection;
  if (currentPhotoFName !== '') baselocation += '#' + currentPhotoFName;
  location.href = baselocation;
}
function setSection() {
  var cElement = null;
  $.each(site_menus, 
    function (key, value) {
      cElement = $('#' + value.mElement);
      if (key == currentSection)
        cElement.attr('src', 'images/' + value.mOnImg);
      else
        cElement.attr('src', 'images/' + value.mDefaultImg);
      if (site_menus[key].type == 'photos')
        cElement.parent().click(function() {
          goSection(key, thumb_photos[key][0]);
        });//cElement.parent().attr('href', '?' + key + '#' + thumb_photos[key][0]);
    }
  );
}
function checkArrowOpen() {
  if ($('#ul-div ul')[0].scrollWidth > $('#ul-div')[0].offsetWidth && isSettingArrow == false) {
    isSettingArrow = true;
    var shiftunit = 450;
    $('#sub-photo-show div').first()
      .css('cursor', 'pointer')
      .click(function(){$('#ul-div ul').animate({scrollLeft: '-=' + shiftunit + 'px'}, 400);})
      .hover(function(){$(this).toggleClass('arrow-left-on');});
    $('#sub-photo-show div').last()
      .css('cursor', 'pointer')
      .click(function(){$('#ul-div ul').animate({scrollLeft: '+=' +shiftunit + 'px'}, 400);})
      .hover(function(){$(this).toggleClass('arrow-right-on');});
  }
}
function calcEmptyDivPosition() {
  var position = $('#main-photo-show').position();
  $('#emptyspace').css('left', position.left).css('top', position.top).show();
  $('#loadingImage').css('left', position.left).css('top', position.top).show();
  if (currentSection == 'contact') {
    $('.contact_mail_link')
      .css('left', position.left + 505)
      .css('top', position.top + 144 + 10);
  }
  if (currentSection == 'home') {
    $('#emptyspace div img').css('height', '601px');
  }
}
function goPhoto(val) {
  var PhotoIndex = $.inArray(currentPhotoFName, thumb_photos[currentSection]) + val;
  var PhotoId = thumb_photos[currentSection][PhotoIndex];
  var $CurrentPhotoDiv = $('#photo-div-' + PhotoId + ':hidden');
  if ($CurrentPhotoDiv.length > 0) {
    changePhoto(PhotoId);
  }
}
function goSection(secValue, photoValue) {
  if (secValue == currentSection) return false;
  location.href = '?' + secValue + '#' + photoValue;
}