﻿$(function () {
	LoadTwitterPostCount();

	var fixZindexes = function () {
		zIx = 10000;
		$('#headerSocialDiv span').each(function () {
			$(this).css('zIndex', zIx);
			zIx -= 10;
		});
	};
	setTimeout(fixZindexes, 400);
	setTimeout(fixZindexes, 1000);
});

// start post count

function UpdateCountsTabClicked(provider) {
	var buttonClicked = null;
	if (provider == 'facebook') {
		buttonClicked = $('.SocialFacebookButton');
	}
	else if (provider == 'twitter') {
		buttonClicked = $('.SocialTwitterButton');
	}

	if (buttonClicked && buttonClicked.size() > 0) {
		HideProviderPostCount(buttonClicked, provider);
	}
};

function LoadTwitterPostCount() {
	var postCount = -1, 
		cookieName = 'BRONSWERK_SOCIAL_COOKIE_TWITTER',
		cookieValue = GetCookie(cookieName),
		cookieDisabledValue = 'hide';
	
	if ((cookieValue == null || cookieValue == '') || (cookieValue == cookieDisabledValue && TwitterPostsRecountRequired())) {
		LoadPostCountFromTwitter(function (count) {
			postCount = count;
			if (postCount > 0) {
				CreateCookie(cookieName, postCount, SocialCookieExpireDate());
				CreateTwitterPostCountHtml(postCount);
			}
			else {
				CreateCookie(cookieName, cookieDisabledValue, SocialCookieExpireDate());
			}
		});
	}
	else if (cookieValue != cookieDisabledValue) {
		postCount = parseInt(cookieValue);
		if (postCount > 0) {
			CreateTwitterPostCountHtml(postCount);
		}
	}
};

var TwitterPostsRecountRequired = function () {
	var providerName = 'twitter',
		recountRequired = false,
		socialDateCookieValue = GetValueFromSocialDateCookie(providerName);

	if (socialDateCookieValue != null && socialDateCookieValue != '') {
		var tempDate = new Date(socialDateCookieValue);
		if (tempDate != null) {
			var today = new Date(),
				compareDate = tempDate;
			compareDate.setHours(tempDate.getHours() + 2);
			if (today > compareDate) {
				SetSocialDateCookie(providerName);
				recountRequired = true;
			}
		}
	}
	return recountRequired;
};

function CreateTwitterPostCountHtml(postCount) {
	var button = $('.SocialTwitterButton'), 
		cssClass = (postCount <= 9 ? 'PostCount' : 'PostCountLarge'),
		postCountHtml = $('<span class="' + cssClass + '"><span class="PostCountText">' + postCount + '</span></span>');

	button.find('.PostCount,.PostCountLarge').remove();
	$('.SocialTwitterButton img').after(postCountHtml);
};

var GetTwitterpostUrl = function () {
	var twitterUser = 'Bronswerk',
		proto = ('https:' == document.location.protocol ? 'https:' : 'http:');
	return proto + '//api.twitter.com/1/statuses/user_timeline.json?screen_name=' + twitterUser + '&include_rts=1&callback=?';
};

function LoadPostCountFromTwitter(callback) {
	var compareDate = GetPostsSinceDateTime('twitter'),
		twitterUrl = GetTwitterpostUrl();
	$.ajax({
		url: twitterUrl,
		dataType: "json",
		success: function (data) {
			var items = data;

			$.each(items, function (i, item) {
				var created = new Date(parse_date(item.created_at));
				item.created_at_date = created;
			});

			items = jQuery.grep(items, function (n, i) {
				var createdDate = n.created_at_date;
				return (createdDate > compareDate);
			});

			callback(parseInt(items.length));
			return;
		}
	});
	callback(0);
};

function parse_date(date_str) {
	// The non-search twitter APIs return inconsistently-formatted dates, which Date.parse
	// cannot handle in IE. We therefore perform the following transformation:
	// "Wed Apr 29 08:53:31 +0000 2009" => "Wed, Apr 29 2009 08:53:31 +0000"
	return Date.parse(date_str.replace(/^([a-z]{3})( [a-z]{3} \d\d?)(.*)( \d{4})$/i, '$1,$2$4$3'));
};

function HideProviderPostCount(buttonClicked, provider) {
	var cookieName = 'BRONSWERK_SOCIAL_COOKIE_' + provider.toUpperCase();
	CreateCookie(cookieName, 'hide', SocialCookieExpireDate());
	if (buttonClicked && buttonClicked.size() > 0) {
		var postCountHtml = buttonClicked.find('.PostCount,.PostCountLarge');
		if (postCountHtml && postCountHtml.size() > 0) {
			postCountHtml.remove();
			SetSocialDateCookie(provider);
		}
	}
};

function SetSocialDateCookie(provider) {
	var cookieName = 'BRONSWERK_SOCIAL_COOKIE_DATE_' + provider.toUpperCase(),
		today = new Date(),
		cookieValue = today.toUTCString();
	CreateCookie(cookieName, cookieValue, SocialCookieExpireDate());
};

var SocialCookieExpireDate = function () {
	var currentDate = new Date(),
		expireYear = currentDate.getFullYear(),
		expireMonth = currentDate.getMonth(),
		expireDay = currentDate.getDate() + 1,
		socialCookieExpireDate = new Date(expireYear, expireMonth, expireDay);
	socialCookieExpireDate.setHours(07);
	return socialCookieExpireDate;
};

var GetPostsSinceDateTime = function (provider) {
	var postsSinceDateTime = DefaultPostsSinceDateTime(),
		socialDateCookieValue = GetValueFromSocialDateCookie(provider);
	if (socialDateCookieValue != null && socialDateCookieValue != '') {
		var tempDate = new Date(socialDateCookieValue);
		if (tempDate != null) {
			postsSinceDateTime = tempDate;
		}
	}
	return postsSinceDateTime;
};

var GetValueFromSocialDateCookie = function (provider) {
	var cookieName = 'BRONSWERK_SOCIAL_COOKIE_DATE_' + provider.toUpperCase(),
		cookieValue = GetCookie(cookieName);
	return cookieValue;
};

var GetValueFromSocialCookie = function (provider) {
	var cookieName = 'BRONSWERK_SOCIAL_COOKIE_' + provider.toUpperCase(),
		cookieValue = GetCookie(cookieName);
	return cookieValue;
};

var DefaultPostsSinceDateTime = function () {
	var currentDate = new Date(),
		defaultYear = currentDate.getFullYear(),
		defaultMonth = currentDate.getMonth(),
		defaultDay = currentDate.getDate() - 1,
		defaultPostsSinceDateTime = new Date(defaultYear, defaultMonth, defaultDay);
	return defaultPostsSinceDateTime;
};

// end post count

//Social media

var SocialMediaCenterLoaded = function () {
	var $socialMediaDiv = $('.ContactCenterContainerDiv'),
		isSocialMediaCenterLoaded = $socialMediaDiv.has('#contactCenterDiv').size() > 0;
	return isSocialMediaCenterLoaded;
};

function InitializeSocialMedia() {
	$('.SocialLiveSupportButton,.SocialLinkedinButton').bind('click', function () {
		$('.SocialLiveSupportButton,.SocialTwitterButton,.SocialFacebookButton,.SocialLinkedinButton').unbind('click');
		GetSocialMediaData($(this).attr('class'));
	});

	$('.SocialTwitterButton').bind('click', function () {
		$('.SocialLiveSupportButton,.SocialTwitterButton,.SocialFacebookButton,.SocialLinkedinButton').unbind('click');
		var $_this = $(this);
		GetSocialMediaData($_this.attr('class'), function () {
			HideProviderPostCount($_this, 'twitter');
		});
	});

	$('.SocialFacebookButton').bind('click', function () {
		$('.SocialLiveSupportButton,.SocialTwitterButton,.SocialFacebookButton,.SocialLinkedinButton').unbind('click');
		var $_this = $(this);
		GetSocialMediaData($_this.attr('class'), function () {
			HideProviderPostCount($_this, 'facebook');
		});
	});
};

function LoadSocialMediaCenter(buttonClicked, callback) {
	$('.SocialLiveSupportButton,.SocialTwitterButton,.SocialFacebookButton,.SocialLinkedinButton').unbind('click');
	GetSocialMediaData(buttonClicked, callback);
};

function SuccessGetDataCallbackSocialMedia(data, callback) {
	var response = eval(data);
	var $socialMediaDiv = $('.ContactCenterContainerDiv');

	$socialMediaDiv.empty();
	$socialMediaDiv.html(response.d);
	if (typeof (callback) === "function") {
		callback();
	}
};

function GetSocialMediaData(buttonClicked, callback) {
	$.ajax({
		type: "POST",
		contentType: "application/json; charset=utf-8",
		url: 'Index.aspx/GetLanguageControlHTML',
		data: "{'controlLocation':'~/controls/SocialMediaShowControl.ascx', 'objectId':'" + buttonClicked + "', 'languageId':'" + GetLgeId() + "'}",
		success: function (data) {
			SuccessGetDataCallbackSocialMedia(data, callback);
		}
	});
};
//end social media
