
//base site url
var BASE_URL = 'http://'+document.location.hostname+'/';

function siteUrl(path){	

	if (path != undefined)
		return BASE_URL + path;
	else
		return BASE_URL;
}

var Login = {
	callback: function(){},
	args: [],
	self: {},
	message: "",
	call: function()
	{
		if(window.location.pathname == '/' || window.location.pathname == '')
		{
			window.location.href = siteUrl('dashboard/following');
		}
		if(window.location.pathname.match(/login/i))
		{
			window.location.href = siteUrl('login/redirect');
		}
		window.sploggedin = true;
		this.callback.apply(this.self, this.args)
		if(this.message.length)
		{
			try
			{
				$('.nyroModalCont>div>div').html(this.message);
				$(".nyroModalCont").animate({
					height: $(".nyroModalCont>div>div").innerHeight()
				}, 0);
				$(".nyroModalCont").css({
					"background-color": "#f5fcfc"
				});
			}
			catch(e)
			{
				
			}
		}
	},
	reset: function()
	{
		this.callback = function(){};
		this.args = [],
		this.self = {};
		this.message = "";
	},
	valid: function()
	{
		if(window.sploggedin)
			return true;
		
		this.reset();
		if(arguments.length > 0)
			this.callback = arguments[0];
		if(arguments.length > 1)
			this.self = arguments[1];
		if(arguments.length > 2)
			this.args = arguments[2];
		if(arguments.length > 3)
			this.message = arguments[3];
		
		this.modal();
		return false;
	},
	modal: function()
	{
		$.nmManual('#modal-main');
	}
}

function updateFuzzyDatesTimer() {
	updateFuzzyDates();
	timer = setTimeout(updateFuzzyDatesTimer, 60000);
}

function updateFuzzyDates(){
	
	$(".fuzzy-date").each(function(){
	
		var time_stamp = parseInt($(this).attr('alt'));
		var fuzzy_date = '';
		
		if (time_stamp == undefined)
		{	
			fuzzy_date = 'Back to the Future';
		}
		else{
			var d = new Date();
			var time_diff = d.getTime() / 1000 - time_stamp;
			
			var periods = ['second', 'minute', 'hour', 'day', 'week', 'month', 'year'];
			var lengths = [60,60,24,7,4.35,12];
			var j=0;
		
			for (j=0; j<=5 && time_diff >= lengths[j]; j++){
			
				if (j == 6)
					break;
			
				time_diff = time_diff / lengths[j];
			}
		
			time_diff = Math.round(time_diff);
		
			if (time_diff != 1)
			periods[j] += 's';
			
			if (j == 0)
				fuzzy_date = 'Just seconds ago';
			else
				fuzzy_date = time_diff + ' ' + periods[j] + ' ago'; 
		}
		
		$(this).text(fuzzy_date);
	});
}

// menu
 

$(function(){
	$('#reset-modal-form').submit(function(e){
		e.preventDefault();
		$.ajax({
			type: "POST",
			data: $(this).serialize(),
			url: $(this).attr('action'),
			cache: false,
			dataType: 'json',
			success: function(data)
			{
				if(data.sent)
				{
					alert(data.message);
					try
					{
						$.nmTop().close();
					}
					catch(e)
					{
						
					}
				}
				else
				{
					alert(data.error);
				}
			}
		});
		return false;
	});
	$('#signup-modal-form').submit(function(e){
		e.preventDefault();
		$.ajax({
			type: "POST",
			data: $(this).serialize(),
			url: $(this).attr('action'),
			cache: false,
			dataType: 'json',
			success: function(data)
			{
				if(data.signed_up)
				{
					var li = $('#header .nav .active').index('#header .nav li');
					$('#header').replaceWith(data.header);
					if(li != -1)
					{
						$('#header .nav li:eq('+li+')').addClass('active');
					}
					Login.message = data.message + "<br />" + Login.message;
					Login.call();
				}
				else
				{
					alert(data.error);
				}
			}
		});
		return false;
	});
	$('#login-modal-form').submit(function(e){
		e.preventDefault();
		$.ajax({
			type: "POST",
			data: $(this).serialize(),
			url: $(this).attr('action'),
			cache: false,
			dataType: 'json',
			success: function(data)
			{
				if(data.logged_in)
				{
					try{
						$.nmTop().close();
					}
					catch(e)
					{
					
					}
					$("#message-text").html('<div class="success">You have been logged in.</div>');
					initMessage();
					var li = $('#header .nav .active').index('#header .nav li');
					$('#header').replaceWith(data.header);
					if(li != -1)
					{
						$('#header .nav li:eq('+li+')').addClass('active');
					}
					Login.message = data.message + "<br />" + Login.message;
					Login.call();
				}
				else
				{
					alert(data.error);
				}
			}
		});
		return false;
	});
	$('.username-wrap').live('click', function(){
		var $width = $(this).width();
		if($width < 89) {
			if($width >= 83) {
				$(this).parent().addClass("even-un");
				$(".username ul li a").css({
					"width": $width - 13
				});
				$(".username ul").css({
					"width": $width
				});
			} else {
				$(this).parent().addClass("short-un");
			}
		} 
		if($(".username ul").parents(".username-dd-fix").length == 0 && $width > 89){
			$(".username ul").wrap("<div class='username-dd-fix' />");
			$(".username-dd-fix").css({
				"width": $width + 8,
				"height": "100px"
			});
		}
		if($(this).hasClass("open")){
			$(this).removeClass("open");
			$(this).parent().find('ul').css("display","none");
			$(this).find("span.arrow").html("&#x25BC;");
			if($(".username ul").parents(".username-dd-fix").length != 0){
				$(".username ul").unwrap();
			}
		} else {
			$(this).addClass("open");
			$(this).parent().find('ul').css("display","block");	
			$(this).find("span.arrow").html("&#x25B2;");
			if(BrowserDetect.browser == "Explorer" && $(this).hasClass("ie-opened") == false) {
				$(this).addClass("ie-opened");
				$("div.username ul").append('<span class="oval-left-shadow"></span><span class="oval-bottom-shadow"></span><span class="oval-right-shadow"></span><span class="oval-bottom-left-shadow"></span><span class="oval-bottom-right-shadow"></span>');
			}
		}
		if($('.notifications').hasClass("open")){
			$('.notifications').removeClass("open");
			$(this).parents(".box").find(".notifications-wrap").css("display","none");
			$(this).parents(".box").find(".notifications-num-wrap span").css("display","none");
		}
		
		return false;
	})
	$('div.username:has(.username-wrap)').live('mouseleave', function(){
		if($(this).find('.username-wrap').hasClass("open")){
			$(this).find("span.arrow").html("&#x25BC;");
			$(this).find('.username-wrap').removeClass("open");
			$(this).find('ul').css("display","none");	
			if($(".username ul").parents(".username-dd-fix").length != 0){
				$(".username ul").unwrap();
			}
		}
	});
	
});



/*////////////////////////////////
// BEGIN NOTIFICATIONS DROPDOWN //
////////////////////////////////*/

var mouse_position = "body";

$('a.notifications').live({
	mouseenter: function () {
		mouse_position = "notifications-link";
	},
	mouseleave: function () {
		mouse_position = "body";
	},
	click: function () {
		return false;
	}
});

$('.notifications-drop').live({
	mouseenter: function () {
		mouse_position = "notifications-drop";
	},
	mouseleave: function () {
		mouse_position = "body";
	}
});


$("body").mouseup(function(){
	if (mouse_position == "body") {
		if($('.notifications').data('open') == true) {
			closeDropDown();
		}
	} 
	if (mouse_position == "notifications-link") {
		$this = $('.notifications');
		if($this.data('open') != true) {
			$this.data('open', true);
			$this.siblings().css("display","block"); // carot
			$this.parents(".box").find(".notifications-wrap").css("display","block");
			$this.parents(".box").find(".notifications-num-wrap").removeClass('new-notifications');
			$this.parents(".box").find(".notifications-drop")
			.html('<div style="background-color:#ECFCFC;width:100%;height:100px;margin:auto;text-align:center;"><img src="/images/ajax_loader.gif" style="margin-top:40px;" /></div>')
			.load("/notifications/ajax");
		} else {
			closeDropDown();
		}
		return false;
	}
});

function closeDropDown() {
	$('.notifications').data('open', false);
	$('.notifications').parents(".box").find(".notifications-wrap").css("display","none");
	$('.notifications-num-wrap .carrot').hide();
}


/*////////////////////////////////
/// END NOTIFICATIONS DROPDOWN ///
////////////////////////////////*/


// // login-small
// $(function() {
// 	var _width = 0;
// 	$(".box").find(".login").click(function (){
// 		$(this).parents(".box").find("#login-wrap").css("display","block").parents(".box").bind("mouseleave",function(){
// 			$(this).find("#login-wrap").css("display","none");
// 		});
// 		return false;
// 	});
// });



// main modal
/*
$(function() {
	var _width = 0;
	$(".box").find(".or").click(function (){
		$(this).parents("#header").find(".modal-main").css("display","block").parents(".box").bind("click",function(){
			$(this).find(".modal-main").css("display","none");
		});
		return false;
	});
});


// main modal - login
$(function() {
	var _width = 0;
	$(".box").find(".login").click(function (){
		$(this).parents("#header").find(".modal-main-login").css("display","block").parents(".box").bind("click",function(){
			$(this).find(".modal-main-login").css("display","none");
		});
		return false;
	});
});


// main modal - signup
$(function() {
	var _width = 0;
	$(".box").find(".signup").click(function (){
		$(this).parents("#header").find(".modal-main-signup").css("display","block").parents(".box").bind("click",function(){
			$(this).find(".modal-main-signup").css("display","none");
		});
		return false;
	});
});
*/


// popup 
function init_Popup() {
	var _popup_holder = '.popup-block';
	var _popup_box = '.pop-up';
	var _activeClass = 'active';
	$(_popup_holder).each(function(){
		var _holder = $(this);
		var _opener = _holder.find('a.opener');
		_opener.click(function(){
			if(_holder.hasClass(_activeClass)) {
				_holder.removeClass(_activeClass);
			} else {
				_holder.addClass(_activeClass);
			}
			return false;
		});
	});
}

$(function() {
	init_Popup();
});
function initOpenClose() {
	var _activeClass = 'active';
	var _slideSpeed = 750;
	$('div.groups-post, div.threads, div.image-forms, div.faq-holder ul.accord-list li').each(function(){
		var _holder = $(this);
		var _opener = _holder.find('li.alt a, a.opener, strong.heading');
		var _slider = _holder.find('div.options, div.file-form, div.slider');
		var _closer = _holder.find('span.close a, a.closer');

		_opener.unbind();
		
		_opener.click(function(){
			if(_holder.hasClass(_activeClass)) {
				_slider.slideUp(_slideSpeed);
				_holder.removeClass(_activeClass);
			} else {
				_slider.slideDown(_slideSpeed);
				_holder.addClass(_activeClass);
			}
			return false;
		});
		
		_closer.unbind();

		_closer.click(function(){
			_slider.slideUp(_slideSpeed);
			_holder.removeClass(_activeClass);
			return false;
		});
	});
	
	$(".expand").unbind();
	
	$(".expand").click(function()
    {
		var box = $(".holder-post")
		$(this).toggleClass("opening");
        if (!$(this).hasClass("opening"))
		{
			$("div.slider").slideUp(750);
			$(this).text("Expand all");
			$("ul.accord-list li").removeClass("active");
		}
		else {
			
			$(this).text("Collapse all");
			$("div.slider").slideDown(750);
			$("ul.accord-list li").addClass("active");
		}
		return false; 
    });
}

$(function() {
	initOpenClose();
});

//Tooltip

// tabs
$(function() {
	$('div.tabs, div.tab-area').tabs();
});

//message drop down


$(".actions > a").live("click", function(){
	if($(this).parent().hasClass("open") == false) {
		$(this).parent().addClass("open");
		$(this).find("span.down-arrow").html("&#x25B2;");
		if(BrowserDetect.browser == "Explorer" && $(this).hasClass("ie-opened") == false) {
			$(this).addClass("ie-opened");
			$("ul.actions-menu").append('<span class="oval-left-shadow"></span><span class="oval-bottom-shadow"></span><span class="oval-right-shadow"></span><span class="oval-bottom-left-shadow"></span><span class="oval-bottom-right-shadow"></span>');
		}
	} else {
		$(this).parent().removeClass("open");
		$(this).find("span.down-arrow").html("&#x25BC;");
	}
	return false;
});
$(".actions").live("mouseleave", function(){
	$(this).removeClass("open");
	$(this).find("span.down-arrow").html("&#x25BC;");
});

$(".others > a").live("click", function(){
	$(this).parent().find("ul").show();
	return false;
});
$(".others > ul").live("mouseleave", function(){
	$(this).hide();
});

$(".feed .reply-form").live('submit', function(e){
	if(!Login.valid(function(){this.submit();}, this, arguments))
		return false;
	return true;
});



$(".sys-btn").live({
	mouseenter: function(){
		if($(this).hasClass("following") == true) {
			$(this).html("&times; Unfollow").removeClass("following").addClass("unfollow");
		}
	},
	mouseleave: function(){
		if($(this).hasClass("unfollow") == true) {
			$(this).html("&#x2713; Following").removeClass("unfollow").addClass("following");
		}
	}
});


$(".response-user-nav a").live({
	mouseenter: function(){

		if($(this).hasClass("following") == true) {
			$(this).html("Unfollow").removeClass("following").addClass("unfollow");
		}
	},
	mouseleave: function(){
		if($(this).hasClass("unfollow") == true) {
			$(this).html("Following").removeClass("unfollow").addClass("following");
		}
	}
});




function confirmation(msg){
	
	var result = false;
	
	result = confirm(msg);
	
	return result;
}

// begin people view pop out menu

$(function(){	
	$(".people-contact .switch").click(function(){
//		console.log($(this).parents(".people-contact"));
		if($(this).parents(".people-contact").hasClass("open") == false) {
			$(this).parents(".people-contact").stop().animate({
				right: "2px"
			}, 300).addClass("open");
			$(this).find("span").html("&#9654;");
		} else {
			$(this).parents(".people-contact").stop().animate({
				right: "-194px"
			}, 300).addClass("open");
			$(this).find("span").html("&#9664;");
		}
		return false;
	}).parents(".people-contact").mouseleave(function(){
//		console.log($(this));
		if($(this).hasClass("open") === true){
			$(this).stop().animate({
				right: "-194px"
			}, 300).removeClass("open");
		}
		$(this).find(".switch span").html("&#9664;");
	});
});

// end people view pop out menu


// replace broken images

$('img').error(function(){
  $(this).attr('src', '/images/broken-img.png');
});

// end replace broken 

$('#comment-image-form').submit(function () {
	$('.comment-form-box div').html('<img class="image-submit-gif" src="/images/ajax-loader-blue-2.gif" />').addClass("submitting");
});

$('.upload-form').submit(function () {
	$(this).find("fieldset").append('<img class="profile-pic-gif" src="/images/ajax-loader-grey.gif" />');
});

// begin SPotlight DD

$('.sp-dd > a').click(function(){
	if($(this).hasClass("active") == false) {
		$(this).addClass("active");
		$(this).parents('.sp-dd').find("ul").show();
		if(BrowserDetect.browser == "Explorer" && $(this).hasClass("ie-opened") == false) {
			$(this).addClass("ie-opened");
			$("div.sp-dd ul.dd-list").append('<span class="oval-left-shadow"></span><span class="oval-bottom-shadow"></span><span class="oval-right-shadow"></span><span class="oval-bottom-left-shadow"></span><span class="oval-bottom-right-shadow"></span>');
		}
	} else {
		$(this).removeClass("active");
		$(this).parents('.sp-dd').find("ul").hide();
	}
	
	$(".dd-list").mouseleave(function(){
			$('.sp-dd > a').removeClass("active");
			$(this).hide();
	});
	return false;
});

// end SPotlight DD

