$(document).ready(function(){
var BASE = $('base').attr('href');
var LANG = $('html').attr("lang");
//var PAGE = $('meta[name=page]').attr("content");
var TEMPLATE = $('meta[name=template]').attr("content");
//var url_start = LANG+'/'+PAGE+'/';
var EXT_LINK_TTL = (LANG == 'el')? 'Εξωτερικός σύνδεσμος - Ανοίγει σε νέο παράθυρο':'External link - Opens in a new tab';


// ------------------------- Initiate js-only elements for js-enabled clients

$('#pd_images_frame').css('overflow-x','hidden');
$('#pd_images div').css({
  'opacity':'0.2',
  'cursor':'pointer'
});
$('#pd_images div:eq(0)').addClass('current').css('opacity','1');
$('#arrow_nav').css('visibility','visible');



// ------------------------- NEWS

if (TEMPLATE == 'news') {
  $('.newspost:last').css('border-bottom', 'none');
  $('.expand').show();
  $('.collapse').hide();
  $('.newstext').hide();

  $('.expand').click(function(){
    var parent = $(this).parents('.newsbody');
    parent.find('.newstext').slideDown(500, function(){
      parent.find('.expand').hide();
      parent.find('.collapse').show();
    });
  });
  $('.collapse').click(function(){
    var parent = $(this).parents('.newsbody');
    parent.find('.newstext').slideUp(400, function(){
      parent.find('.expand').show();
      parent.find('.collapse').hide();
    });
    $('body').scrollTo(parent, 500, {offset:-75});
  });
}

// ------------------------- PROJECTS

if (TEMPLATE == 'projects') {

  $('.project_thumb').live('mouseenter', function(){
    var $this = $(this);
    var title = '<p class="title">'+$this.find('.project_title').html()+'</p>';
    var subtitle = '<p class="subtitle">'+$this.find('.project_subtitle').html()+'</p>';
    $('#hover_link').text($this.attr('href'));
    $('#hover_content').html(title+subtitle);
    var top = $this.parents('.project').offset().top;
    var left = $this.parents('.project').offset().left;
    $('#hover').css({'top':top, 'left':left, 'width':$this.width(), 'height':$this.height()}).show();
  }).live('mouseleave', function(){
    $('#hover').css('display','none');
  });

  $('#hover').live('mouseover', function(e){
    $('#hover').show();
  });

  $('#hover').live('mouseleave', function(){
    $(this).css('display','none');
  });

  $('#hover').live('click', function(){
    if ($('#hover_link').text() == '') {
      return false;
    }
    window.location = $('#hover_link').text();
    return true;
  });

  // ----- Details

  $('#prevArrow').addClass('noHover');

  var $scrollTarget = $('#pd_images_frame');

  function show_slide(next){
    if (next != $('#now_showing').text()) {
      var position = $('#header').offset();
      $scrollTarget.stop().scrollTo('#pd_images div:eq('+next+')',400,{axis:'x', offset:-position.left, onAfter:function(){
        $('#pd_images div.current').animate({'opacity':0.2}, 350, function(){$(this).removeClass('current')});
        $('#pd_images div:eq('+next+')').animate({'opacity':1}, 350, function(){
          $('#pd_images div:eq('+next+')').addClass('current');
        });
      }});
      $('#now_showing').text(next);
      $('#pd_nav div').removeClass('current');
      $('#pd_nav div:eq('+next+')').addClass('current');
      var last = $('#pd_images div').length-1;
      if (next < last) {
        $('#nextArrow').removeClass('noHover');
      } else {
        $('#nextArrow').addClass('noHover');
      }
      if (next >= 1) {
        $('#prevArrow').removeClass('noHover');
      } else {
        $('#prevArrow').addClass('noHover');
      }
    }
  }

  function next_project() {
    var next = parseInt($('#now_showing').text())+1;
    var last = $('#pd_images div').length-1;
    if (next > last) {
      return false;
    }
    show_slide(next);
  }

  function previous_project() {
    var next = parseInt($('#now_showing').text())-1;
    if (next < 0) {
      return false;
    }
    show_slide(next);
  }

  function highlightButtons(e) {
    if (e.which == 37) { // left
      if (!$('#prevArrow').hasClass('noHover')) {
        $('#prevArrow').mouseover();
      }
    } else if (e.which == 39) { // left
      if (!$('#nextArrow').hasClass('noHover')) {
        $('#nextArrow').mouseover();
      }
    }
  }

  $('#prevArrow').mouseover(function(){
    $(this).addClass('likehover');
  }).mouseout(function(){
    $(this).removeClass('likehover');
  });

  $('#nextArrow').mouseover(function(){
    $(this).addClass('likehover');
  }).mouseout(function(){
    $(this).removeClass('likehover');
  });

  function handleArrowKeys(e) {
    if (e.which == 37) { // left
      previous_project();
    } else if (e.which == 39) { // right
      next_project();
    }
    $('#prevArrow').removeClass('likehover');
    $('#nextArrow').removeClass('likehover');
  }

  document.onkeyup = handleArrowKeys;
  document.onkeydown = highlightButtons;

  $('#prevArrow').click(function(){
    previous_project();
  });
  $('#nextArrow').click(function(){
    next_project();
  });

  $('.bullet').live('click',function(){
    var next = $('.bullet').index(this);
    show_slide(next);
  });

  $('#pd_comments_link').live('click',function(){
    var next = $('.bullet').length;
    show_slide(next);
  });

  $('#pd_images div').click(function(){
    var next = $('#pd_images div').index(this);
    show_slide(next);
  });

  function set_project_imgs_position() {
    var position = $('#header').offset();
    $('#pd_images').css('margin-left',position.left);
    $('#extra_grey_border').css('width',position.left+5);
  }

  set_project_imgs_position();

  $(window).resize(function() {
    set_project_imgs_position();
  });

  function make_project_nav_bullets() {
    var elements = $('#pd_images div').length-1;
    var bullet = '<div class="bullet"></div>';
    var bullet_nav = '';
    $('#pd_images img').each(function(){
      bullet_nav += bullet;
    });
    bullet_nav += '<div id="pd_comments_link">'+$('#pd_comments_link_content').text()+'</div>'
    $('#pd_nav').html(bullet_nav);
    $('.bullet:eq(0)').addClass('current');
  }

  make_project_nav_bullets();

}

// ------------------------- HOME

if (TEMPLATE == 'home') {

  function set_home_picture_size() {
    var img_width = $('#home_picture img').width();
    var window_width = $(window).width();
    if (window_width < img_width) {
      var diff = (img_width - window_width)/2;
      $('#home_picture img').css('margin-left','-'+diff+'px');
    }
  }

  set_home_picture_size();

  $(window).resize(function() {
    set_home_picture_size();
  });

  $('#home_picture').cycle({
      timeout: 9000,
      speed:   1500,
      delay:   0,
      slideExpr: 'img',
      pause:   0,
      sync:    1
    });

}

// ------------------------- GENERAL

$('#projects img').lazyload({threshold:0, effect:"fadeIn", placeholder:"graphics/white.gif", failurelimit:10});

$('.linkedin').prepend('<span class="LinkedInIcon"></span>');

function add_grey_border() {
  var position = $('#header').offset();
  $('#extra_grey_border').css('width',position.left+5);
}

add_grey_border();

$(window).resize(function() {
  add_grey_border();
});

$("a[href$='.jpg']").addClass("preview");
$("a[href$='.png']").addClass("preview");

if ($('.contactMail').length > 0){
  var name;
  var domain;
  var displayed;
  $('.contactMail').each(function(){
    name = $('span:last', this).text();
    domain = $('span:first', this).text();
    displayed = name+"@"+domain;
    $(this).append('<a href=mailto:' + name + '@' + domain + '>' + displayed + '</a>');
  });
}

if ($('#contact_form').length) {
  $("#contact_form").validate({
    errorPlacement: function(error, element) {
       error.appendTo(element.parent("p").next("br"));
     }
  });
}

$('#lang_menu a').click(function(e){
  var userlang = $(this).attr('rel');
  createCookie('userlang',userlang,180);
});

$('#lang_menu').mouseenter(function(){
  $('#lang_menu_arrow').css('background-position','-211px -23px');
}).mouseleave(function(){
  $('#lang_menu_arrow').css('background-position','-211px 0');
});


$("a[href^='http']").not("[href^='"+BASE+"']").addClass('external').attr('title',EXT_LINK_TTL).click(function() {
  window.open($(this).attr('href'));
  return false;
});
$('a[href$=".pdf"]').addClass('external').attr('title',EXT_LINK_TTL).click(function() {
  window.open($(this).attr('href'));
  return false;
});

function createCookie(name,value,days) {
	var expires = "";
  if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expires = "; expires="+date.toGMTString();
	} else {
    expires = ""; // cookie will be deleted immediatly
  }
  document.cookie = name+"="+value+expires+"; path=/";
}

});
