﻿/// <reference path="jquery-1.3.2-vsdoc2.js" />
// By adding the tripple commented reference tag at the top of this page it will pull in the vsdoc
// file and give us intellisense for this script.

//AddThis is fucking up the design with some flash bullshit
var addthis_config = {data_use_flash: false};

google.load("jquery", "1");
google.load("jqueryui", "1");
google.load("swfobject", "2");
google.setOnLoadCallback(initialize);

var iRandomShirt = Math.floor(Math.random() * 11) + 1;
var sEnvironment = "liveContent";

function initialize() {
    $(function() {
        initUi();
        truncPostsForSearch();
        customTracking();
    });
}

function initUi() {
	$('.post-share-link').css('color', '#fff');
	$('div.post-comment-optional-background:odd').css('background', 'url(http://mjf.me/NinjaCamp/' + sEnvironment + '/images/commentBack.gif) repeat-y top center');
	$('div.post-comment:last').css('background', 'url()');
	$('#commentBodyField').addClass('customCommentBodyField');

	$('#randomShirt').attr('src', 'http://mjf.me/NinjaCamp/' + sEnvironment + '/images/merchItems/' + iRandomShirt + '.png');

	$('.uiNinja-state-default').hover(function() {
		$(this).addClass("uiNinja-state-over");
	}, function() {
		$(this).removeClass("uiNinja-state-over");
	});

	$('.uiNinja-state-default').mousedown(function() {
		$(this).addClass("uiNinja-state-down");
	}).mouseup(function() {
		$(this).removeClass("uiNinja-state-down");
	});

	$('.uiNinja-widget-input-text').click(function() {
		$(this).val('').css('color', '#000');
	});

	$('.post-trunc-control').hover(function() {
		$(this).find(':first-child').attr('src', 'http://mjf.me/NinjaCamp/' + sEnvironment + '/images/miniNinjaSkullIcon_over.png');
	}, function() {
		$(this).find(':first-child').attr('src', 'http://mjf.me/NinjaCamp/' + sEnvironment + '/images/miniNinjaSkullIcon.png');
	});
	
	$('.imgRoll').hover(function() {
		var currentImg = $(this).attr('src');
		$(this).attr('src', $(this).attr('hover'));
		$(this).attr('hover', currentImg);
	}, function() {
		var currentImg = $(this).attr('src');
		$(this).attr('src', $(this).attr('hover'));
		$(this).attr('hover', currentImg);
	});

	$('a.authorBlock').each(function() {
		$(this).attr('href', getAuthorEmail_fromAuthor($(this).text()));
	});

	$('.post-share-link').each(function() {
		$(this).hover(function() {
			attack(this, '#f4c523', '#fff')
		}, function() {
			$(this).stop(true).stopTime();
			$(this).css('color', '#fff');
		});
	});

	$('#nwordSubmit').click(function() {
		var s = $('#nwordAddress').val();
		if (bCheckEmail(s)) {
			//ajax_AddToNword(s);
		} else {
			return false;
		}
    });

    $('#versionNum').text('1.0.8');
    var dt = new Date();
    $('#copyDate').text(dt.format("yyyy"));
}

function customTracking() {
    $('.spreadShirtLink').click(function() { pageTracker._trackPageview('/shop/ninjacamp.spreadshirt.com'); });
}

function getCustomDateArray_fromTimestamp(sTimestamp) {
	var timeSplitArr = [2];
	var timeSplit = "";
	
	if (sTimestamp != "") 
	{
		timeSplit = sTimestamp.split(",")[1].split(" ");
		timeSplitArr[0] = timeSplit[1].substring(0, 3).toLowerCase();
		timeSplitArr[1] = timeSplit[2].toLowerCase();
	}

	return timeSplitArr;
}

function getAuthorEmail_fromAuthor(sAuthor) {
	var authEmail = trimInner(sAuthor);
	authEmail = "mailto:" + authEmail + "@ninjacamp.com";
	return authEmail;
}

function trimInner(s) {
	return $.trim(s.replace(/ /g,""));
}

function attack(obj, sFlashHexColor, sHexColor) {
	$(obj).everyTime(10, function() {
		$(this).animate({ color: sFlashHexColor }, 100).animate({ color: sHexColor }, 100);			
	});
}

function truncPostsForSearch() {
	if (bIsSearchResult() || bIsArchive()) {
		$('.post-container').addClass('post-container-trunc');
		$('.movieContainer').css('display', 'none');
		$('.post-truncControl-container').css('display', 'block');
		$('.post-footer-container').css('display', 'none');		
	} else {
		$('.post-container').removeClass('post-container-trunc');
		$('.post-truncControl-container').css('display', 'none');
		$('.movieContainer').css('display', 'block');
	}
}

function bIsSearchResult() {
	var _regEx = new RegExp(/search?/i);
	var match = _regEx.test(window.location)	
	return match;
}

function bIsArchive() {
	var _regEx = new RegExp(/_archive/i);
	var match = _regEx.test(window.location)
	return match;
}

function ajax_AddToNword(sVerifiedEmail) {
	debugger;	
	$.ajax({
        type: "POST",
        url: "http://ninjaengine.appspot.com/add/",
        data: ({address : "testies"}),
        success: function(msg) {
                alert("Data Saved: " + msg);
        }
    });

}

function bCheckEmail(sEmailAddress) {
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(sEmailAddress)) {
		alert('That is no email address! Try again champ.');
		return false;
	} else {
		return true;
	}
}
