function thisMovie(movieName) {
  if (navigator.appName.indexOf("Microsoft") != -1) {
    return window[movieName]
  }else {
    return document[movieName]
  }
}

function latestNewsToggle(index){
  $(".latest_news_inner .first_item").each(function(){$(this).addClass("hidden");});
  $(".latest_news_inner .first_item."+index).removeClass("hidden");
  $(".latest_news_inner .item").each(function(){
      $(this).children("a").children(".strip_overlay").detach();
  });
  $(".latest_news_inner .item."+index+" a").prepend("<div class='strip_overlay'></div>");
}


$(function(){
  /* checkbox */
  $(".checkbox_wrap").click(function(){
    if($(this).children('input').attr("checked")){
      $(this).children("label").removeClass("LabelSelected");
      $(this).children('input').removeAttr("checked")
    }else{
      $(this).children("label").addClass("LabelSelected");
      $(this).children('input').attr("checked","checked")
      if($(this).children("label").hasClass("checkbox_error")) $(this).children("label").removeClass("checkbox_error");
    }
  });

  $(".checkbox").change(function(){
    if($(this).is(":checked")){
      $(this).next("label").removeClass("checkbox_error");
      $(this).next("label").addClass("LabelSelected");
    }else{
      $(this).next("label").removeClass("LabelSelected");
    }
  });

  $(".input-reset").focus(function() {if(this.value == this.title){this.value="";}});
  $(".input-reset").blur(function() {if(this.value==""){this.value = this.title;}});

  $(".clear_search").click(function(){
    $(".input_wrap input.text").val($(".input_wrap input.text").attr("title"));
  });


  if($.browser.mozilla && navigator.appVersion.indexOf("Mac")!=-1){$('.social_media_share').css('letter-spacing','-1px');}
  
  $.nyroModalSettings({
    processHandler: function(settings) {
      var from = settings.from;
      if (from && from.href && from.href.indexOf('http://www.youtube.com/watch?v=') == 0) {
        $.nyroModalSettings({
          type: 'swf',
          height: 426,
          width: 720,
          url: from.href.replace(new RegExp("watch\\?v=", "i"), 'v/')
        });
      }
    }
  });
  
});


/* Social Medias */
setTimeout(function() {
  if (document.getElementById('twitter_update_list')) {
    $.getScript('http://twitter.com/statuses/user_timeline/funcom_conan.json?count=1&callback=twitterCallback2');
  }
  $('#facebook_container').append('<iframe src="http://www.facebook.com/plugins/likebox.php?href=http%3A%2F%2Fwww.facebook.com%2Fageofconanhyborianadventures&amp;width=250&amp;colorscheme=light&amp;show_faces=true&amp;stream=false&amp;header=false&amp;height=200" scrolling="no" frameborder="0" style="background: transparent; margin:-64px 0px 0px -5px;border:none; overflow:hidden; width:250px; height:200px;" allowTransparency="true"></iframe>');
}, 1000);


var run_animation = false;

// Setup carousel
var timerSwitch;
var timerDelay = 3000;

function carousel_click() {
  window.clearTimeout(timerSwitch);
  
  var adjust = 1;
  if (this.id && this.id.match(/left/)) adjust = -1;

  var slides = $('#carousel_wrap .carousel_slide_link');
  var current = slides.filter(':visible');
  var current_index = parseInt(current.attr('id').replace(/^.*\-(\d+)$/, '$1'));
  var target_index = ((current_index + slides.length - 1 + adjust) % slides.length) + 1;
  var target = $('#' + current.attr('id').replace(/\-(\d+)$/, '-' + target_index));
  var timer = 500;
  if($.browser.msie && (parseInt($.browser.version)==7 || parseInt($.browser.version)==8)){
    timer = 0;
  }

  current.fadeOut(timer,function(){
    if(target.length > 0) {
      target.fadeIn(timer,function(){
        timerSwitch = setTimeout(carousel_click,timerDelay);
      });
    } else {
      $('#carousel_wrap .carousel_slide_link:first').fadeIn(timer,function() {
        timerSwitch = setTimeout(carousel_click,timerDelay);
      });
    }
  });

  return false;
}

$(document).ready(function(){
  if ($('#carousel_wrap').length > 0) {
    timerSwitch = setTimeout(carousel_click,timerDelay);
    $('#caroussel_right_arrow, #caroussel_left_arrow').click(carousel_click);
  }
});

//newsletter widget
function newsletter_subscribe(){
  function validate(email) {
     var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
     var address = email;
     if(reg.test(address) == false) {
        return false;
     }else{
        return true;
     }
  }
  
  if($('#checkbox_agree').is(':checked')&&validate($('#email_address').val())&&$('#real_name').val()!="- Your Name -"){
    $.ajax({
      type: 'GET',
      url: '{site_url}subscribe.php',
      data: $('#newsletter').serialize(),
      cache: true,
      async: true,
      success: function(msg){
        if(msg.substr(0,2)=="Ok"){
          $("form#newsletter").detach();
          $("#news_error").html("");
          $("#newsletter_content").html($("#newsletter_content").html()+"<p style='margin-top:15px;'>You have successfully subscribed to the newsletter.</p>");
        }else{
          $("#news_error").html("Subscription failed. Please try again later.");
        }
      }
    });
  } else {
    if(!$('#checkbox_agree').is(':checked')){
      $("#news_error").html("You must agree to the Privacy Policy.");
      $('.checkbox_label_class').removeClass('checkbox_error');
      $('.checkbox_label_class').addClass('checkbox_error');
    }else if(!validate($('#email_address').val())){
      $("#news_error").html("Please enter a valid email.");
      $("#email_address").addClass('error');
    }else{
      $("#news_error").html("Please enter a name.");
      $("#real_name").addClass('error');
    }
  }
}

$(function(){
  $('#newsletter input').focus(function(){$(this).removeClass('error');});
});


function bypass_cdn() {
  var l = $('img[src^="http://l3cdn."]').each(function(){this.src = this.src.replace(/l3cdn\./,'l3cdn-fill.')}).length;
  if (console && console.log) {
    console.log('Replace ' + l + ' images');
  }
}

function setCookie(c_name,value,exdays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	var c_path="; path=/";
	document.cookie=c_name + "=" + c_value + c_path;
}

