var theForm, fCont1, fCont2, fCont3, medBu, isIE;

Shadowbox.init({
 players: ["img","swf", "iframe"],
 continuous: true
});


$(function(){
  $('a').focus( function(){ this.blur() });
  $('object').focus( function(){ this.blur() });
  
  $("input").click( function(){
    var valAttr = $(this).attr("value");
    if ((valAttr == "-- YOUR NAME --") || (valAttr == "-- YOUR EMAIL --")) {
      $(this).attr({ value : "" });
    }
    return true;
  });
  
  if (navigator.userAgent) {
	  if ((navigator.userAgent).substr(25, 4) == 'MSIE') {
	    $('#shaderbox').css("filter", "alpha(opacity = 80)");
	    isIE = true;
	  } else {
	    isIE = false;
	  }
	} 
  
  theForm = $('#nsSignup');
  theForm.attr({action: "javascript:void(0)"});
  
  theForm.submit(function() {
    fCont1 = $.trimFunc($('input:first').attr('value'));
    fCont2 = $.trimFunc($('input:gt(0)').attr('value'));
    fCont3 = $('input:gt(2)').attr('checked');
    
    if (((fCont1 != '') && (fCont2 != '')) && ((fCont1 != '-- YOUR NAME --') && (fCont2 != '-- YOUR EMAIL --'))) {
      if (fCont2.indexOf('@') != -1) {
        if (fCont1.length <= 160) {
          if (fCont2.length <= 160) {
            if (fCont3 == true) {
              $.subscribe(fCont1, fCont2);
            } else {
              alert("You have to confirm that you agree to the privacy policy!");
            }
          } else {
            alert("Your email address is too long! Max 160 characters");
          }
        } else {
          alert('Your name is too long! Max 160 characters');
        }
      } else {
        alert('The email address you entered seemes to be invalid. Please check spelling and try again');
      }
    } else {
      alert('Please enter your name and email, they are both required!');
    }
    return false;
  });
});

$.subscribe = function(achre34s, s12hrzc) {
  var en = escape(achre34s);
  var ea = escape(s12hrzc);
  
  $.ajax({
    type: 'POST',
    url: 'newslettersignup.php',
    data: 'achre34s='+en+'&s12hrzc='+ea+'&terms=true',
    success: function(html) {
      if (html == 'success') {
        $.success();
      }
      else if (html == 'empty') {
        alert('Please enter your name and email, they are both required!');
      }
      else if (html == 'noconf') {
        alert("You have to confirm that you agree to the privacy policy!");
      }
      else if (html == 'error') {
        alert('The email address you entered seemes to be invalid. Please check spelling and try again');
      }
      else if (html == 'exists') {
        alert('The email is already subscribed');
      }
      else {
        alert('Database error, could not write to database! Please try again later');
      }
    },
    error: function() {
      alert('Database error, could not write to database! Please try again later');
    }
  });
}

$.trimFunc = function(str) {
  return str.replace(/^\s+|\s+$/g, '');
}

$.success = function() {
  urchinTracker("/submit");
  $('#fCon').fadeOut('slow', function() {
    $('#fCon').html('<p>Thank you! A verification email has been sent to your email inbox.</p>');
    $('#fCon').fadeIn('slow');
  });
  return true;
}

$.mediaBuzz = function(wtD) {
  var mediaBuzz, btn, cont;
  
  if (wtD == undefined) {
    mediaBuzz = true;
    btn = $("#mbuzz");
    cont = $("#infobox");
  } else {
    mediaBuzz = false;
    btn = $("#feat");
    cont = $("#featBox");
  }

  if (medBu == undefined) { medBu = 0; }
  
  if (medBu == 0) {
    if (mediaBuzz == true) {
      urchinTracker("/media_buzz");
      $("#close").attr("href", "javascript:jQuery.mediaBuzz()");
    } else {
      urchinTracker("/features");
      $("#close").attr("href", "javascript:jQuery.mediaBuzz(2)");
    }
    
    $('#shaderbox').fadeIn('slow', function() {
      cont.fadeIn('fast');
      $('#shaderbox').css("opacity", "0.8");
      if (isIE == true) $('#shaderbox').css("filter", "alpha(opacity = 80)");
     });
    medBu = 1;
  } else {
    var secClose = btn;
    
    $('#close').attr('href', 'javascript:void(0)');
    $("#mbuzz").attr('href', 'javascript:void(0)');
    $("#feat").attr('href', 'javascript:void(0)');
    
    if (mediaBuzz == true) {
      $('#featBox').fadeOut('fast');
    } else {
      $('#infobox').fadeOut('fast');
    }
    
    cont.fadeOut('fast', function() {
      $('#shaderbox').fadeOut('slow', function() {
        $('#close').attr('href', 'javascript:jQuery.mediaBuzz()');
          $("#mbuzz").attr('href', 'javascript:jQuery.mediaBuzz()');
          $("#feat").attr('href', 'javascript:jQuery.gameInfo()');
      });
    });
    medBu = 0;
  }
}

$.launchTest = function(wed) {
  urchinTracker("/initiation_test");
  
  Shadowbox.open({
    content: 'assets/personalitytest/test.swf?wed=' + wed,
    player: 'swf',
    width: 953,
    height: 604,
    options: {modal:true, viewportPadding:0}
  });
}

$.launchArt = function() {
  urchinTracker("/artwork");
  
  $("#sb-body").attr("style", "border: 1px solid #303030;");
  Shadowbox.open({
    content: 'http://cdn.funcom.com/tsw/artwork/17.jpg',
    gallery: 'artwork',
    player: 'img',
    options: {continuous: true}
  });
}

$.launchScreens = function() {
  urchinTracker("/screenshots");
  
  $("#sb-body").attr("style", "border: 1px solid #303030;");
  Shadowbox.open({
    content: 'http://cdn.funcom.com/tsw/screenshots/tsw_screen_11.jpg',
    gallery: 'screens',
    player: 'img',
    options: {continuous: true}
  });
}

$.gameInfo = function() {
  urchinTracker("/game_info");
  var theWidth = 855;
  
  if ((navigator.appName).toLowerCase() == "microsoft internet explorer") {
    var version=parseInt($.browser.version);
    
    if (version < 8) {
      theWidth = 1050;
    }
  }
  
  Shadowbox.open({
    content: 'assets/gameinfo/index.php',
    gallery: 'gameInfo',
    height: 1128,
    width: theWidth,
    player: 'iframe'
  });
  
  $("#sb-body").attr("style", "border: none;");
}