﻿/// <reference path="jquery-1.4.4.js" />

(function ($)
{

	$.Watchdog =
	{

		_userAuthenticated: false,

		ready: function ( userAuthenticated, watchListInfo )
		{
			$.Watchdog._userAuthenticated = ( userAuthenticated ? true : false );
			$.Watchdog.watchList._info = watchListInfo;

			$.Watchdog.watchList.update();

			if( $.wdViewer )
				$( $.wdViewer() ).bind( "watchListChanged", $.Watchdog.watchList._onWatchListChanged );

			// Trigger a "hashchange" when the page first load
			$(window).trigger('hashchange');
		},

		userAuthenticated: function ()
		{
			return $.Watchdog._userAuthenticated;
		},

		querystring: function ( name )
		{
			var match = RegExp( '[?&]' + name + '=([^&]*)', 'i' )
							.exec(window.location);
			
			return match && decodeURIComponent(match[1].replace(/\+/g, ' '));				
		},

		popup: function ( theTitle, theBody, thePref )
		{
			var thePopupSizeCss = ( thePref && thePref.popupSize && thePref.popupSize === "large" ? "wd-popup-900" : "wd-popup-550" );

			var thePopup = $( "#wdPopup" );

			if( thePopup.length != 0 )
			{
				thePopup.remove();
			}


			thePopup = $("<div class='wd-popup' id='wdPopup'><div class='wd-popup-header'></div><div class='wd-popup-body'></div><div class='wd-popup-footer'></div></div>");
				$(document.body).append(thePopup);

			thePopup
				.removeClass( thePopupSizeCss === "wd-popup-900" ? "wd-popup-550" : "wd-popup-900" )
				.addClass( thePopupSizeCss );

			$( ".wd-popup-header", thePopup )
				.html( theTitle );
			$( ".wd-popup-body", thePopup )
				.html( theBody );

			var thePopupPref = $.extend
				( 
					{
						top: "center",
						closeOnClick: false,
						closeOnEsc: true,
						api: true,
						speed: 0,
						load: false,
						closeSpeed: 0,
						mask:
							{
								color: 'black',
								loadSpeed: 200,
								opacity: 0.3,
								zIndex: 99999
							},
						onLoad: function () { $("html").css("overflow", "hidden"); },
						onClose: function () { $("html").css("overflow", "auto"); }
					}
				,	thePref || {}
				);

			var thePopupOverlay = thePopup.overlay( thePopupPref );

			return thePopupOverlay;
		},

		watchList:
		{
			_watchlistInfo: null,

			count: function ()
			{
				return $.Watchdog.watchList._info ? $.Watchdog.watchList._info.Count : -1;
			},

			max: function ()
			{
				return $.Watchdog.watchList._info ? $.Watchdog.watchList._info.Max : -1;
			},

			update: function ()
			{				
				var theInfo = $.Watchdog.watchList._info,
					theHtml = null,
					theCounterHtml = null,
					theCompany = ( theInfo != null ? ( theInfo.Count == 1 ? "company" : "companies" ) : "" );

				if( theInfo == null || !$.Watchdog.userAuthenticated() )
				{
					theHtml = "";
					theCounterHtml = null;
				}
				else if( theInfo.Count == 0 )
				{
					theHtml = "Your watchlist is empty.";
					theCounterHtml = null;
				}
				else if( theInfo.Count >= theInfo.Max )
				{
					theHtml = "Your watchlist has <span class='wd-red'>" + theInfo.Count + "</span> " + theCompany + " and it's full.";
					theCounterHtml = theInfo.Count;
				}
				else if( theInfo.Max > 9999 )
				{
					theHtml = "Your watchlist has <span class='wd-red'>" + theInfo.Count + "</span> " + theCompany + ".";
					theCounterHtml = theInfo.Count;
				}
				else
				{
					theHtml = "Your watchlist has <span class='wd-red'>" + theInfo.Count + "</span> " + theCompany + ", you can add <span class='wd-red'>" + ( theInfo.Max - theInfo.Count ) + "</span> more.";
					theCounterHtml = theInfo.Count;
				}

				if( theCounterHtml != null )
					theCounterHtml = "<span class='wd-text'>WatchList </span><span class='ws-watchListCounterLeft'></span><span class='ws-watchListCounterMiddle'>" + theCounterHtml + "</span><span class='ws-watchListCounterRight'></span>";
				else
					theCounterHtml = "WatchList";

				// Watch-list bar
				$( "#wdWatchlistBar" )
					.html( theHtml );

				// Nav-bar watch-list Counter
				$( "#wdNavWatchListCounter" )
					.html( theCounterHtml );
			},

			_onWatchListChanged: function ( theEvent )
			{
				$.Watchdog.watchList._info.Count += theEvent.changeType;
				$.Watchdog.watchList.update();
			}

		}

	};


})(jQuery);
