jQuery( document ).ready ( function() {

  var pageLocation = document.location.href.split('/');
  if(!pageLocation[3])
    jQuery('#navigation ul li:first-child').addClass('current');

  if( pageLocation[3] && pageLocation[4] )
    jQuery('.reference').next('.section_object').hide();


  jQuery( '#facebook .button' ).toggle
  ( function() {
    var obj  = jQuery( this ),
      text = obj.find( 'span' ).html();

    obj.data( 'text', text )
      .find( 'span' ).html( 'Dölj' ).end()
      .next().show();
  }, function() {
    var obj = jQuery( this );

    obj.find( 'span' )
      .html( obj.data( 'text' ) ).end()
      .next().hide();
  } );

  jQuery( window ).resize( function() {
      jQuery( '#transition' ).height(
      jQuery( document ).height()
    ).width(
      jQuery( document ).width()
      );
  } ).resize();
} );

function change( color ) {
  return;
  var body  = jQuery( 'body' ),
    shader  = jQuery( '#transition' );

  if ( !shader.is( ':animated' ) ) {
    body.css( { background : shader.css( 'background' ) } );
    shader.fadeTo( 0, 0.0 ).css( { background : color } ).fadeTo( 1600, 1.0 );
  }
}

var ref = '';
function listReferenceImages( id ) {
  if( id ) {
    ref = id;
    doAjaxRequest (
      'CoMeT_function=run_module&module=Ref&function=references&action=list_reference_images&reference_id=' + id,
      listReferenceImagesResponse
    );
  }
}

function listReferenceImagesResponse( html ) {
  jQuery( '#bilder_' + ref )
    .html( html );
}

function doAjaxRequest( pars, callback ) {
  jQuery.ajax( {
    type  : "POST",
    url   : "?",
    data  : pars,
    success : callback
  } );
}

function validateEmail(elementValue) {
  var emailPattern = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
  return emailPattern.test(elementValue);
}

/* GOD JUL! */

function godjul() {
  jQuery( '#content .left:first' ).gifpopup( {
    id:"tomte1",
    gifurl : "/site/nollfyranoll/images/static/jul/tomte_01.png",
    followMouse : false,
    x: 30,
    y: -120,
    initialOpacity:0.1,
    previewOpacity:0.1,
    onallclicked:allClicked,
    ongifclicked:oneClicked
  } );

  jQuery( '.page-om-040 #footer hr' ).gifpopup( {
    id:"tomte2",
    gifurl : "/site/nollfyranoll/images/static/jul/tomte_02.png",
    followMouse : false,
    x: 780,
    y: -190,
    initialOpacity:0.1,
    previewOpacity:0.1,
    onallclicked:allClicked,
    ongifclicked:oneClicked
  } );

  jQuery( '.page-vem-bjorn #footer hr' ).gifpopup( {
    id:"tomte3",
    gifurl : "/site/nollfyranoll/images/static/jul/tomte_03.png",
    followMouse : false,
    x: 0,
    y: -88,
    initialOpacity:0.1,
    previewOpacity:0.1,
    onallclicked:allClicked,
    ongifclicked:oneClicked
  } );

  jQuery( '#content .main h2' ).gifpopup( {
    id:"tomte4",
    gifurl : "/site/nollfyranoll/images/static/jul/tomte_04_right.png",
    followMouse : false,
    x: 460,
    y: -70,
    initialOpacity:0.1,
    previewOpacity:0.1,
    onallclicked:allClicked,
    ongifclicked:oneClicked
  } );

  jQuery( '.page-portfolio #footer hr' ).gifpopup( {
    id:"tomte5",
    gifurl : "/site/nollfyranoll/images/static/jul/tomte_04_left.png",
    followMouse : false,
    x: 20,
    y: -1270,
    initialOpacity:0.1,
    previewOpacity:0.1,
    onallclicked:allClicked,
    ongifclicked:oneClicked
  } );
}

function oneClicked( obj, nofClicks ) {
  if ( nofClicks == 1 )
    showJulkort();
}

function allClicked() {
  doAjaxRequest (
    'CoMeT_function=run_module&module=Godjul&function=foundAll',
    allClickedResponse
  );
}

function allClickedResponse( html ) {

  jQuery( '#godjul' ).html( html );

  jQuery( '#godjul form' ).ajaxForm( {
    type: 'post',
    url: '?CoMeT_function=run_module&module=Godjul&function=saveUserDetails',
    beforeSubmit: checkFields,
    success: godjulSuccess
  } );

  function godjulSuccess( html ) {
    jQuery( '#godjul' ).html( html );
  }

  function checkFields() {
    var name = jQuery( '#godjul .name' ),
        mail = jQuery( '#godjul .mail' );

    if ( !name.val().length ) {
      name.addClass( 'fail' );
      return false;
    }
    else {
      name.removeClass( 'fail' );
    }

    if ( !validateEmail( mail.val() ) ) {
      mail.addClass( 'fail' );
      return false;
    }
    else {
      mail.removeClass( 'fail' );
    }
  }

  showJulkort();
}

function showJulkort() {
  var godjul = jQuery( '#godjul' ),
      shadow = jQuery( '#shadow' );

  godjul.center().fadeIn( 400 );
  shadow.fadeTo( 400, 0.4 );

  shadow.click( close );
}

function closeGodjul() {
  var godjul = jQuery( '#godjul' ),
      shadow = jQuery( '#shadow' );

  godjul.center().fadeOut( 400 );
  shadow.fadeOut( 400 );
}
