$(window).load(function () {$("#wrapper").fadeIn();});
$(document).ready(function () {
    var $html = $('html');
    function setBodyFontSize(){        
        //1em = 1px @ 1300px width
        
        var windowWidth = $(window).width();
        
        var width = Math.min(windowWidth / 1300, 1.3); 
        
        width = Math.round(width * 100) / 100.0;
        
        if(windowWidth > 1000){
            width = Math.max(width, 1);
        }
        $html.css('font-size', width * 100);
        $("#header").animate({"opacity":"1"});
      
    }
	if(navigator.userAgent.match(/iphone/i)){
		var headHTML = document.getElementsByTagName('head')[0].innerHTML;
		headHTML    += '<link type="text/css" rel="stylesheet" href="/index.php/stylesheets/iphone">';
		document.getElementsByTagName('head')[0].innerHTML = headHTML;
		orientThisBadBoy();
		function orientThisBadBoy() {
			if (orientation == 0 || orientation == 180) {
				$(".wrapper-btn-tw, .fb_iframe_widget").removeClass("wide");
			}
			else if ( orientation == 90 || orientation == -90 ) {
				$(".wrapper-btn-tw, .fb_iframe_widget").addClass("wide");
			}		
		}
		$(window).bind('orientationchange', function(event) {orientThisBadBoy()});
	}    
	$("#btn-launch-gallery").click(
	function() {
		$("#gallery a").orangeBox("create");
			return false;

	});
	$(".nav-mobile ul li a").click(
	function() {
		var thedrop = $(this).parents("li").first().children(".mobile-drop");
		if (thedrop.is(":visible")) {
			thedrop.slideUp();
			$(this).parents("li").first().removeClass("active");
		}
		else {
			thedrop.slideDown();
			$(this).parents("li").first().addClass("active");
		}
		return false;
	});
	if ($("article").hasClass("singlepost") && $("#bg-single").length>0) {
		$img = $("#datpage");
		$("#page-background .image").html();
		$("#page-background .image").html("<img src='"+$("#bg-single img").attr("src")+"' alt='' />");
	}
    $(window).on('resize', setBodyFontSize).trigger('resize');
	if ($(".blog-title")) {
		var tempText = $(".blog-title").html();
		tempText = tempText.replace(/<br>/gi,"</h1><h1>");
		$(".blog-title").html(tempText);
		}
	$(".btn-read").hover(
		function() {	
			if ($(window).width()>800) {
			$(this).parent(".wide .bg-entry").find(".excerpt").children("p").addClass("hideit").css({"opacity":"0"});
			$(this).parent(".wide .bg-entry").find(".excerpt").css({"width":"0"}).show().stop().animate({"width":"30%"}, "slow", 
				function() { 
					$(this).children("p, cite").removeClass("hideit").addClass("showit").stop().animate({"opacity":"1"});
				});
			}				
		},
		function() {
			if ($(window).width()>800) {			
				$(this).parent(".wide .bg-entry").find(".excerpt").stop().css({"width":"0"}).hide().children("p").removeClass("showit");
		}
	});
	$(".backtop").click(
	function() {location.href="#wrapper";});
    var $kando = $('#kando'),
        $modalOverlay = $('#modal-overlay');
    $('#login, #sign-up, #my2kAction').on('click', function(){
     
	  var url = $(this).data('href');

	  if(!navigator.userAgent.match(/iphone|ipad/i)){
		$('.iframe-container', $kando).html('<iframe src="' + url + '"/>');
		$modalOverlay.show();
		$kando.show();
	  } else {
		url = url.replace('use_social_popups=1', 'use_social_popups=0');
		window.open(url);
	  }
    });
    
	$('.fancybox-close', $kando).on('click', function(){
        $modalOverlay.hide();
        $kando.hide();
		$('.iframe-container', $kando).empty();		
    });       
    $(document).on('click', '.login-btn, .comment-login', function(){$('#login').trigger('click');});
    
    $(document).on('click', '.signup-btn', function(){$('#sign-up').trigger('click');});	
    if($(window).innerWidth() <= 650)
    {
        $footer = $("#footer");
		$footer.find('.social').parent().parent().parent().on("mouseleave", function() {$social.hide();});

         $footer.find('.social').on("mouseleave", function() {$social.hide();});
    }	

	$("a.btn-searchdrop").click(
	function() {
		var theMenu = $(this).parents("li").children("ul");
		if (theMenu.is(":visible")) {theMenu.hide();}
		else {
			theMenu.fadeIn();
			$("#keywords").focus();
		}
	});
	$(".btn-closeblog").click(
	function() {
		var theMenu = $(this).parents("ul").first();
		if (theMenu.is(":visible")) {theMenu.stop().fadeOut();}
		else {theMenu.show();}		
	});
	$(".searchdrop form").on("submit", function() {
		if ($("#keywords").val().length<3) {
			alert("Search strings must be 3 characters or longer");
			return false;
		} 
	});	
	$("#btn-submitblogsearch").click(
	function() {
		if ($("#keywords").val()!="") {$(this).parents("form").first().submit();}
	});
	var thisPage = window.location.pathname;
	$("#blog-header ul.nav-top li.hasdrop").hover(
	function() {
		var theDrop = $(this).children("ul").first();
		theDrop.children("li").css({"opacity":"0"});
		theDrop.stop().hide().css({"height":"auto"}).slideDown(function() {theDrop.children("li").animate({"opacity":"1"}, 100);});
	},
	function() {
		var theDrop = $(this).children("ul").first();
		theDrop.children("li").animate({"opacity":"0"}, 50);	
		$(this).children("ul").stop().animate({"height":"0"}, function() {theDrop.children("li").css({"opacity":"1"});});
	
	}
	);
    var $community = $('.community');
    var $arrow = $('.community').find('.sub-arrow');
    $community.on("hover",function() {
        $arrow.css("background-position-y", "0");
    });
    $community.on("mouseleave", function() {
        $arrow.css("background-position-y", "-16px");
    });

    var $language = $('.language');
    var $languageArrow = $('.language').find('.sub-arrow');
     $language.on("hover",function() {
        $languageArrow.css("background-position-y", "0");
    });
    $language.on("mouseleave", function() {
        $languageArrow.css("background-position-y", "-16px");
    });

    var $language = $('.family');
    var $familyArrow = $('.family').find('.sub-arrow');
     $language.on("hover",function() {
        $familyArrow.css("background-position-y", "0");
    });
    $language.on("mouseleave", function() {
        $familyArrow.css("background-position-y", "-16px");
    });
    var $footer = $('#footer');
    var $social = $('.social-popup');
    $footer.find('.facebook').on("mouseenter", function() {
        $social.hide();
        $social.removeClass("youtubePosition");
        $social.removeClass("twitterPosition");
        $social.addClass("facebookPosition");


        $social.find('ul li:eq(0) a').attr("href", "http://www.facebook.com/2kgames");
        $social.find('ul li:eq(1) a').attr("href", "http://www.facebook.com/2KSports");
        $social.show();
    });

     $footer.find('.twitter').on("mouseenter", function() 
     {
        $social.hide();
        $social.removeClass("facebookPosition");
        $social.removeClass("youtubePosition");
        $social.addClass("twitterPosition");
        

        $social.find('ul li:eq(0) a').attr("href", "https://twitter.com/2KGames");
        $social.find('ul li:eq(1) a').attr("href", "https://twitter.com/2KSports");
        $social.show();
    });

    $footer.find('.youtube').on("mouseenter", function() 
    {
        $social.hide();
        $social.removeClass("facebookPosition");
        $social.removeClass("twitterPosition");
        $social.addClass("youtubePosition");
       
        $social.find('ul li:eq(0) a').attr("href", "http://www.youtube.com/user/2KGames");
        $social.find('ul li:eq(1) a').attr("href", "http://www.youtube.com/user/2KSports");
        $social.show();
    });

	$(".entry-content h2 a").each(
	function() {
		if ($(this).html().length>46) {
			var diff = $(this).html().length-46;
			var $excerpt = $(this).parents(".bg-entry").first().children(".excerpt").first().children("p").first();
			
			var toolong = $excerpt.html();
			$excerpt.html(toolong.substr(0,toolong.length-40-parseInt(diff*1.8))+"...");
			
		}
	});
    $social.find('ul li:eq(1)').on(
    {
        mouseenter:function() {
            $('.backarrow').css('background-color', '#8e0c08');
        },
        mouseleave:function() {
            $('.backarrow').css('background-color', '#e51315');
        }
    });

    $footer.find('.social-popup').on("mouseleave", function() {$social.hide();});

    $footer.find('.social').parent().on("mouseleave", function() {$social.hide();});

    $footer.find('.newsletter').on('click', function(){
	  $('#kando .iframe-container').empty().append('<iframe src="' + $(this).data('href') + '"></iframe>');
	  $('#modal-overlay, #kando').show();
    });

	$("article.blog").animate({"opacity":"1"}, 'fast');
	
	if ($("#comments").length>0) {
		$("body").append("<div id='moot'></div>");
		window.makeCommentForm("#comments",window.location.pathname,document.title);
	}

	$("#twitter-widget-0").load(
	function() {$(this).css({"width":"80px"});
	});
});

window.mootInterval = null;
window.makeCommentForm = function(containerId, url, title){
    var $comments = $(containerId),
    $moot = $('#moot');
    if($moot.length != 0){

    $comments.empty().append($moot);
    var sso = {};
    if(window.mootConfig.signature){
	  sso.key = window.mootConfig.apiKey;
	  sso.message = window.mootConfig.message;
	  sso.timestamp = window.mootConfig.timestamp;
	  sso.signature = window.mootConfig.signature;
    } else {
	  $comments.prepend('<span class="log-title">Login to comment</span><a class="comment-login">log in</a>');
	  $(document).off('login-complete');
	  $(document).on('login-complete', function(){
		window.makeCommentForm(containerId, url, title);
	  });	  
    }
    $moot.moot({
	url: window.mootConfig.url + url,
	title: title,
	sso : sso
    });
	//alert(window.mootConfig.url + url);
    if(!window.mootInterval){
	  window.mootInterval = setInterval(disableMootLinks, 500);
    }
    
    function disableMootLinks(){
	  $('.m-facelink[href], .m-time[href]').removeAttr('href');
	  $('#moot .m-cat a').off('click');
    }
    
    var mootAnalyticsInit = false;
    if(!mootAnalyticsInit);
    moot(function(app) {
	  
	  mootAnalyticsInit = true;
	 // types
	 var id = app.analytics && app.analytics.ga;

	 if (!id) return;

	 function track(type, title) {
	    if (typeof _gat == 'undefined') {
		 return $.getScript("//google-analytics.com/ga.js", function() {
		    track(type, title)
		 })
	    }

	    var tracker = _gat._getTracker(id)
	    // console.info("anal", type, title);

	    tracker._trackEvent("Moot: " + type, title, app.user.path || "anonymous")
	 }

	 app.channel.on("login join", function() {
	    setTimeout(function() { track("login", app.page.path) }, 10)
	 })

	 app.on("before:load", function(page) {

	    // load page (or moot page)
	    page.one("load", function() {
		 track("load", page.title)
		 if (page.type == 'moot') track("topic", page.title)
	    })

	    page.on("before:moot", function(moot) {
		 track("post", moot.seed.title) // new post

	    }).on("moot", function(moot) {
		 moot.on("load before:reply like", function(type) {
		    if (type == "load") type = "topic";
		    if (type == "before:reply") type = "post";
		    track(type, moot.seed.title)
		 })
	    })

	 })

    });
	}
}
var Kando = (function($){
    
    function loginComplete(avatar, mootConfig) {
        login(avatar, mootConfig);
        closeModal();
    }
    
    function signupComplete(avatar, mootConfig) {
        login(avatar, mootConfig);
        $('#kando iframe').css('height', '470em');
    }
    
    function verifyComplete(avatar, mootConfig) {
        login(avatar, mootConfig);
        $('#kando iframe').css('height', '470em');
    }
    
    function linkComplete(){
	    location.href = location.href;
    }
    
    function login(avatar, mootConfig) {
        if (location.pathname == '/user' || location.pathname == '/user/') {
            location.href = location.href;
        } else {
            window.mootConfig = mootConfig;
            updateAvatar(avatar);
            $(document).trigger('login-complete');
        }
    }
    
    function closeModal() {
        $('#modal-overlay').hide();
        $('#kando').hide();
    }
    
    function updateAvatar(avatar) {
        var $myAccount = $('<a href="https://www.2k.com/user"><img src="' + avatar + '"/>My Account</a>');
        var $logOut = $('<a href="https://www.2k.com/user/logout">Log Out</a>');
        $('.login').empty().append($myAccount).append($logOut);
        if (!avatar) {
            $('.login a img').attr('src', 'https://www.2k.com/img/myAccount/avatars/2k_blankuser.jpg');
        }
    }
    
    return {
	    loginComplete: loginComplete,
	    linkComplete: linkComplete,
	    signupComplete: signupComplete,
	    verifyComplete: verifyComplete,
	    closeModal: closeModal
    };
    
})(jQuery);
!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="https://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");