﻿$(document).ready( function() {

  // Navi separators
  $("#navi a").not(":last").after('<img src="/files/mark/kultajyva_images/navi_separator.png" class="separator" alt="" />');
    
  // Navi hovers
  $("#navi a").not(".selected").hover(
    function () { $(this).addClass("selected") },
    function () { $(this).removeClass("selected") }
  );  

  // Navi2 height
  /*
  var contentHeight = $("#content").height();
  var naviHeight = $("#navi").height();
  $("#navi2").css("height", (contentHeight - naviHeight - 175) +"px");
  */
   
  // Form elements
  $("input:text").addClass("text");
  $("input:submit").addClass("submit");

  
  /* 
  
    Participation form 
  
  */
  
  // Set class on submit
  $(".participation").submit( function () {
    var classes = new Array();
    $(".participation input[name=sarja]:checked").each( function () {
      var cl = $(this).val();
      /*
      // Also set the main class
      var mc = (cl.split('.'))[0];
      if (mc != cl) classes.push(mc);
      */
      classes.push(cl);
    });
    if (classes.length) {
      $("#q1").val( classes.join(", ") );
      return true;
    }
    else {
      alert("Valitse ainakin yksi sarja!");
      return false;
    } 
  });
  
  
  /*
  
    Campaign page image changer
  
  */
  
  var images = $("div.images");
  var imagelinks = $("div.imagelinks");
  var imagecount = images.find("img").length;
  if (imagecount) {
  
    // Show first image
    images.find("a:eq(0)").css("display", "block");
    
    // Add imagelinks
    var image = 1;
    images.find("a").each( function () {
    
      $(this).click( function (e) {
        // alert("ei toimi vielä ...");
        e.preventDefault();
        img_popup($(this).attr('href'));
      });
      var _this = $(this);
      
      imagelinks.append(
        $(document.createElement('a'))
          .text( image )
          .click( function () {
            images.find("a").hide();
            _this.show();
            imagelinks.find("a").removeClass("selected");
            $(this).addClass("selected");
          })
        );
      image++;
    });
    
    imagelinks.find("a:eq(0)").addClass("selected");
  }  
  
    
  // Hacks
  if ($.browser.msie) {
    // Participation form clear hack
    $(".participation p.line, .participation span.class").after('<i class="clear"></i>');
  }

});



function img_popup (src) {

  var popup = new Popup();
  
  var navi = $(document.createElement('div')).addClass('navi');
  popup.append(navi);
  
  if ($("div.images a").length > 1) {
  
    $("div.images a").each( function (index) {
      var a = $(document.createElement('a'))
        .attr('href', $(this).attr('href'))
        .addClass($(this).attr('href') == src ? 'current' : '')
        .appendTo(navi)
        .text(index + 1)
        .click( function (e) {
          e.preventDefault();
          $(this).parents('.popup').find('.picture').attr('src', $(this).attr('href'));
          $('a', this.parentNode).removeClass('current');
          $(this).addClass('current');
        });
      
    });
    
    /*
    var prev = $(document.createElement('a'))
      .text("<")
      .addClass("link")
      .prependTo(navi)
      .click( function () { 
        if ($('.current', navi).prev('a').length)
          $('.current', navi).prev('a').click(); 
      })
      
    var next = $(document.createElement('a'))
      .text(">")
      .addClass("link")
      .appendTo(navi)
      .click( function () { 
        if ($('.current', navi).next('a').length)
          $('.current', navi).next('a').click();
      })
    */  
    
    /*
    var next = $(document.createElement('img'))
      .attr('src', '/files/laattapiste/img/arrow_right.png')
      .addClass('clickable next')
      .css('height', '5px')
      .appendTo(navi)
      .click(function() { $('.current', navi).next('a').click(); });
  
    var prev = $(document.createElement('img'))
      .attr('src', '/files/laattapiste/img/arrow_left.png')
      .addClass('clickable prev')
      .css('height', '5px')
      .prependTo(navi)
      .click(function() { $('.current', navi).prev('a').click(); });
    */
    
    $(document).keydown(function(e) {
      // next by right arrow or space
      if (e.which == 39 || e.which == 8) $(next).click();    

      // prev by left arrow or backspace
      else if (e.which == 37 || e.which == 13) $(prev).click();
    });
  }
  
  var img = $(document.createElement('img')).attr('src', src).addClass('picture');
  popup.append(img);
  
  popup.show();
}


/* Popup object */

Popup = function(element) {
  this.create();
  this.append(element);
}

Popup.prototype.close = function() {
  this.popup.hide();
  this.shadow.hide();
}

Popup.prototype.create = function() {

  var shadow = $(document.createElement('div'))
    .addClass('shadow')
    .height($(document).height())
    .text('&nbsp;')
    .appendTo(document.body);

  var popup = $(document.createElement('div'))
    .addClass('popup')
    .appendTo(document.body);

  var _this = this;

  /*
  var close = $(document.createElement('img'))
    .attr('src', '/files/laattapiste/img/image_popup_close.png')
    .addClass('close clickable')
    .click(function() { _this.close() })
    .appendTo(popup);
  */
  
  var close = $(document.createElement('a'))
    .text(" x ")
    .addClass('close link')
    .click(function() { _this.close() })
    .appendTo(popup);
  
  $(document).keydown(function(e) {
    if (e.which == 27) _this.close();  // close by esc
  });
  
  this.popup = popup;
  this.shadow = shadow;
}

Popup.prototype.show = function() {
  this.popup.show();
  this.shadow.show();
}

Popup.prototype.append = function(elements) {
  this.popup.append(elements);
}

