// GLOBAL VARS [start]
window.OnlinePlayer = null;
window.OnlinePlayerURL = window.RootVirtualURL + "AudioPlayer.aspx";
window.DefaultCoverURL = window.RootVirtualURL + "upload/cover65x65.jpg";
window.DefaultDJPicURL = window.RootVirtualURL + "upload/djthumb_65x65.png";
window.CustomWsURL = window.RootVirtualURL + "CustomWS/SwfWS.asmx";
window.MediaGalleryURL = window.RootVirtualURL + "MediaGallery.aspx";
// GLOBAL VARS [end]

function CurrentInfo(options, el)
{
	if (options.wsPath == undefined) options.wsPath = window.CustomWsURL;
	if (options.defaultCoverURL == undefined) options.defaultCoverURL = window.DefaultCoverURL;
	if (options.defaultDJPicURL == undefined) options.defaultDJPicURL = window.DefaultDJPicURL;

	var SetDefaultCover = function (el) { $(el).attr("src", options.defaultCoverURL); };
	var OpenOnlinePlayer = function ()
	{
		if (window.OnlinePlayerURL != undefined)
		{
			var doOpen = false;
			if (window.OnlinePlayer == null)
			{
				doOpen = true;
			}
			else
			{
				if (window.OnlinePlayer.closed == true) doOpen = true;
			}
			if (doOpen) window.OnlinePlayer = window.open(window.OnlinePlayerURL, "OnlinePlayer", "location=1,status=0,scrollbars=1,width=932,height=612");
		}
		window.OnlinePlayer.focus();
		return false;
	};

	$.ws(
	{
	    wsUrl: options.wsPath,
	    wsMethod: "GetLatestSongs",
	    wsParams: { stationID: options.stationID, count: 11 },
	    success: function (data)
	    {
	        if (data != null) data = data.d;
	        if (data.length > 0)
	        {
	            $("#CurrentArtist").text(data[0].AuthorTitle.toUpperCase());
	            $("#CurrentSongTitle").text(data[0].Title);
	            var coverUrl = data[0].CoverURL.length > 0 ? data[0].CoverURL : options.defaultCoverURL;
	            var songCover = $("#CurrentSongCover");
	            $(songCover).attr("alt", data[0].AuthorTitle + ": " + data[0].Title);
	            $(songCover).error();
	            $(songCover).attr("src", coverUrl);
	            $(songCover).click(OpenOnlinePlayer);
	            $("#CurrentArtist, #CurrentSongTitle").click(OpenOnlinePlayer);

	            var playlistContainer = $("#PlaylistScroller > div");
	            for (var i = 1; i < data.length; i++)
	            {
	                var item = $(document.createElement("div")).addClass("ucInfo_CoverSmall");
	                coverUrl = data[i].CoverURL.length > 0 ? data[i].CoverURL : options.defaultCoverURL;
	                var cover = $(document.createElement("img"));
	                $(cover).attr("alt", data[i].AuthorTitle + ": " + data[i].Title);
	                $(cover).error();
	                $(cover).attr("src", coverUrl);
	                $(item).append(cover);
	                $(playlistContainer).append(item);
	            }
	            $("#PlaylistScroller").scroller(
                {
                    grid: { x: 4 },
                    onItemsReady: function (api, items)
                    {
                        setInterval(function () { api.JumpBy(1); }, 4000);
                        $(items).mouseover(function (e)
                        {
                            var txt = $(this).children("img").attr("alt");
                            if (txt != undefined) $("#PlaylistEntryTitle").html(txt).show();
                        });
                        $(items).mouseout(function (e) { $("#PlaylistEntryTitle").hide(); });
                        $(items).click(OpenOnlinePlayer);
                    }
                });
	        }
	    },
	    error: function (data) { }
	});
	$.ws(
	{
		wsUrl: options.wsPath,
		wsMethod: "GetShowInfo",
		wsParams: null,
		success: function (data)
		{
			if (data != null)
			{
				data = data.d;
				if (data != null)
				{
					$("#CurrentShow").text(data.Title != undefined ? data.Title : "Antena Zagreb");
					if (data.Images != null)
					{
						if (data.Images.length > 0) $("#CurrentDJ").attr("alt", data.Title != undefined ? data.Title : "Antena Zagreb").attr("src", data.Images[0].URL).error(function () { $(this).attr("src", options.defaultDJPicURL); });
					}
				}
			}
			else
			{
				$("#CurrentShow").text("Antena Zagreb");
				$("#CurrentDJ").attr("alt", "Antena Zagreb").attr("src", options.defaultDJPicURL);
			}
			$("#CurrentDJ, #CurrentShow").click(OpenOnlinePlayer);
		},
		error: function (data) { }
	});
}

function CurrentSong(el)
{
	var id = 0;
	if (el.id != undefined)
	{
		try { id = Number(el.id.split("_")[1]); } catch (err) { }
		if (id > 0)
		{
			$.ws(
	        {
	        	wsUrl: window.CustomWsURL,
	        	wsMethod: "GetLatestSongs",
	        	wsParams: { stationID: id, count: 1 },
	        	success: function (data)
	        	{
	        		data = data.d;
	        		if (data != null)
	        		{
	        			if (data[0].AuthorTitle != undefined && data[0].Title != undefined) $(el).text(data[0].AuthorTitle + " - " + data[0].Title);
	        		}
	        		else
	        		{
	        			$(el).parent().remove();
	        		}
	        	}
	        });
		}
	}
}

function ChannelPlaylist(options, el)
{
    var GetSongInfo = function (songID)
    {
        parent.window.GetSongInfoFromPlayer(songID);
        return false;
    };
	if (options.stationID > 0 && options.count > 0)
	{
		$.ws(
	    {
	    	wsUrl: window.CustomWsURL,
	    	wsMethod: "GetLatestSongs",
	    	wsParams: { stationID: options.stationID, count: options.count },
	    	success: function (data)
	    	{
	    		data = data.d;
	    		if (data != null)
	    		{
	    			if (data.length > 0)
	    			{
	    				$(data).each(function (i)
	    				{
	    					var div = $(document.createElement("div"));
	    					var mid = $(document.createElement("div")).addClass("valignMiddle");
	    					var midInner = $(document.createElement("div"));
	    					var midTT = $(document.createElement("tt"));
	    					var h2 = $(document.createElement("h2")).addClass("reset");
	    					var h3 = $(document.createElement("h3")).addClass("reset").addClass("calibriBold");
	    					var authorLink = $(document.createElement("a")).text(data[i].AuthorTitle).attr("href", "#");
	    					var titleLink = $(document.createElement("a")).text(data[i].Title).attr("href", "#");
	    					var imgUrl = data[i].CoverURL;
	    					if (imgUrl == null || imgUrl.length <= 0) imgUrl = window.DefaultCoverURL;
	    					var img = $(document.createElement("img")).attr("src", imgUrl).error(function (e)
	    					{
	    						$(this).attr("src", window.DefaultCoverURL);
	    					});
	    					$([img, authorLink, titleLink]).each(function (j)
	    					{
	    						$(this).bind("click", { songID: data[i].ID }, function (e)
	    						{
	    							GetSongInfo(e.data.songID);
	    						});
	    					});

	    					$(h2).append(authorLink);
	    					$(h3).append(titleLink);
	    					$(midInner).append(h2).append(h3);

	    					$(img).attr("alt", data[i].AuthorTitle + " - " + data[i].Title).addClass("left");
	    					$(mid).append(midInner).append(midTT);
	    					$(div).append(img).append(mid);
	    					$(el).append(div);
	    				});
	    			}
	    		}
	    	}
	    });
	}
}

function Headlines(el)
{
	var intervalID = -1;
	var id = "#" + $(el).attr("id");
	var panels = $(id + " .PANELS > li");
	var tabs = $(id + " .TABS > li");
	var RunInterval = function ()
	{
		ClearInterval();
		intervalID = setInterval(function ()
		{
			var scroller = $(id + " .PANELS > li:visible .WRAPPER").data("scroller");
			if (scroller != undefined) scroller.JumpBy(1);
		}, 5000);
	}
	var ClearInterval = function ()
	{
		if (intervalID > -1) clearInterval(intervalID);
	}
	$($(panels)[0]).show();
	$(tabs).mouseover(function (e)
	{
		$($(panels).hide()[$(this).index()]).show();
	});
	$(id + " .SUBS").each(function (i)
	{
		$(this).children(".WRAPPER").scroller(
        {
        	grid: { x: 1 },
        	prev: $(this).children(".PREV").click(RunInterval),
        	next: $(this).children(".NEXT").click(RunInterval)
        });
		$(this).mouseover(ClearInterval).mouseout(RunInterval);
	});
	RunInterval();
}

function ColBoxCtrl(el)
{
	$(".colBox").each(function (i)
	{
		var tabs = $(this).children(".TABS");
		if ($(tabs).children().length > 1)
		{
			$(tabs).children().bind("click", { box: this }, function (e)
			{
				$($(e.data.box).children(".PANELS").children().hide()[$(this).index()]).safeFadeIn();
				$(e.data.box).children(".TABS").children().removeClass("selected");
				$(this).addClass("selected");
				return false;
			});
		}
		else
		{
			$(tabs).children().click(function (e) { return false; });
		}
		$(this).children(".PANELS").children(".PANEL").each(function (i)
		{
			var intervalID = -1;
			var ShowSub = function (panel, increment)
			{
				var subs = $(panel).children(".SUBPANELS");
				var index = $(subs).children(":visible").index() + increment;
				var len = $(subs).children().length;
				if (index == len) index = 0;
				if (index < 0) index = len - 1;
				$($(subs).children().hide()[index]).css("opacity", 0).safeFadeIn();
				$(panel).children(".CTRL").children(".STATUS").text(Number(index + 1).toString() + "/" + len.toString());
				return false;
			};
			var subpanels = $(this).children(".SUBPANELS").children();
			var ctrl = $(this).children(".CTRL");
			if (subpanels.length > 1)
			{
				$(ctrl).children(".STATUS").text("1/" + subpanels.length.toString());
				$(ctrl).children(".NEXT").bind("click", { panel: this }, function (e) { return ShowSub(e.data.panel, 1); });
				$(ctrl).children(".PREV").bind("click", { panel: this }, function (e) { return ShowSub(e.data.panel, -1); });
			}
			else
			{
				$(ctrl).html("");
			}

			var config = $(this).children(".SUBPANELS").data("colBoxConfig");
			var StopTimer = function () { clearInterval(intervalID); }
			var SetTimer = function (p)
			{
				StopTimer();
				intervalID = setInterval(function () { ShowSub(p, 1); }, config.interval);
			}
			if (config != undefined)
			{
				if (config.interval != undefined)
				{
					SetTimer(this);
					$(this).mouseover(StopTimer);
					$(this).mouseout(function (e) { SetTimer(this); });
				}
			}
		});
	});
}

function UcTopLists(el)
{
	var topListIntervalId = -1;
	var SetTopListInterval = function (api)
	{
		if (topListIntervalId > 0) clearInterval(topListIntervalId);
		topListIntervalId = setInterval(function () { api.JumpBy(1); }, 5000);
	};

	$(el).find(".ucTopList").each(function (i)
	{
	    $(this).scroller(
        {
            grid: { x: 1, y: 1 },
            vertical: true,
            onItemsReady: function (api, items) { if (i == 0) SetTopListInterval(api); }
        });
	});

	$(el).children(".TABS").children("a").each(function (i)
	{
	    $(this).bind("click", { uc: el, index: i }, function (e)
	    {
	        var panel = $(e.data.uc).find(".PANEL")[e.data.index];
	        var api = $(panel).find(".ucTopList").data("scroller");
	        api.JumpTo(0);
	        SetTopListInterval(api);
	    });
	});
}

function PromoContent(el, options)
{
	var x = 3;
	if (options != undefined)
	{
		if (options.x != undefined) x = options.x;
    }
	$(el).find(".WRAPPER").scroller(
    {
    	grid: { x: x }, prev: $(el).find(".PREV"), next: $(el).find(".NEXT")
    });
}

function ShareCtrl(el, options)
{
    var mailForm = null;
    var mailFormClosed = true;
    var mailFormHeight = 0;
    var notLoggedForm = null;
    var statusText = null;
	if (window.TwitterDynamicAppeded == undefined)
	{
		var js = document.createElement("script");
		$(js).attr("type", "text/javascript").attr("src", "http://platform.twitter.com/widgets.js");
		$("head").append(js);
		window.TwitterDynamicAppeded = true;
	}

	var twitterContainer = document.createElement("div");
	var twitter = document.createElement("a");
	$(twitter).attr("href", "http://twitter.com/share");
	$(twitter).attr("data-count", "horizontal");
	$(twitter).addClass("twitter-share-button");
	$(twitter).text("Tweet");
	$(twitterContainer).addClass("right").append(twitter).css("margin-top", 2);

	var shareMail = document.createElement("a");
	$(shareMail).addClass("right").addClass("ucShareMail").attr("href", "#").css("margin-top", 2);
	$(shareMail).click(function (e)
	{
	    if (options.nickname.length > 0)
	    {
	        // user logged in
	        if (mailFormClosed)
	        {
	            $(mailForm).css("height", 1).show().animate({ height: mailFormHeight }, 200, function () { mailFormClosed = false; });
	        }
	        else
	        {
	            $(mailForm).animate({ height: 1 }, 200, function ()
	            {
	                mailFormClosed = true;
	                $(this).hide();
	            });
	        }
	    }
	    else
	    {
	        // user not logged in
	        $(notLoggedForm).css("height", 1).show().animate({ height: 80 }, 200);
	    }
	    return false;
	});

	var fbLike = document.createElement("iframe");
	$(fbLike).attr("src", "http://www.facebook.com/plugins/like.php?href=" + escape(document.URL) + "&amp;layout=button_count&amp;show_faces=true&amp;width=350&amp;action=recommend&amp;font&amp;colorscheme=light&amp;height=25");
	$(fbLike).attr("scrolling", "no");
	$(fbLike).attr("frameborder", "0");
	$(fbLike).attr("allowTransparency", "true");
	$(fbLike).css("border", "none").css("overflow", "hidden").css("width", 360).css("height", 25);

	var ucPart = document.createElement("div");
	var inner = document.createElement("div");
	var tt = document.createElement("tt");

	var mailHTML = "";
	mailHTML += "<label>E-mail primatelja:</label><br/>";
	mailHTML += "<input type='text' class='MF_MAILTO'/><br/>";
	mailHTML += "<br/>";
	mailHTML += "<label>Va&scaron; komentar:</label><br/>";
	mailHTML += "<textarea class='MF_MAILCOMMENT' rows='5' cols='60'></textarea><br/><br/>";
	mailHTML += "<a href='#' class='MF_SENDMAIL contentBtn right'><span> po&scaron;alji prijatelju </span></a><br class='clear'/>";
	mailHTML += "<div class='mf_statusText clear' style='margin-top:10px;'></div>";

	var notLoggedHTML = "";
	notLoggedHTML += "<p><a href='#' class='MF_LOGIN'>Prijavi se</a> kako bi poslao mail prijatelju.</p>";
	notLoggedHTML += "<p>Nemate korisni&#269;ki ra&#269;un. <a href='#' class='MF_REGISTER'>Registrirajte se.</a></p>";

	var sendMailWrapper = document.createElement("div");
	$(sendMailWrapper).addClass("sendMail").html(mailHTML);

	var notLoggedWrapper = document.createElement("div");
	$(notLoggedWrapper).addClass("notLogged").html(notLoggedHTML);

	$(inner).append(twitterContainer).append(shareMail).append(fbLike);
	$(ucPart).append(inner).append(tt).addClass("valignMiddle");
	$(el).append(ucPart).append(sendMailWrapper).append(notLoggedWrapper);

	mailForm = $(el).find(".sendMail");
	notLoggedForm = $(el).find(".notLogged");
	statusText = $(el).find(".mf_statusText");

	$(notLoggedForm).find(".MF_LOGIN").click(function (e)
	{
	    $(document).userManagementPage(window.RootVirtualURL + "usrlogin.aspx", "loginWrapper");
	    return false;
	});
	$(notLoggedForm).find(".MF_REGISTER").click(function (e)
	{
	    $(document).userManagementPage(window.RootVirtualURL + "usrregister.aspx", "registerWrapper");
	    return false;
	});

	mailFormHeight = $(mailForm).css("height");
	$(mailForm).find(".MF_SENDMAIL").click(function (e)
	{
	    var btn = this;
	    var mailTo = $(mailForm).find(".MF_MAILTO").val();
	    var mailComment = $(mailForm).find(".MF_MAILCOMMENT").val();
	    var mailTitle = document.title;
	    while (mailTitle.indexOf("\"") != -1) mailTitle = mailTitle.replace("\"", "'");
	    if (mailTo.length > 4 && mailTo.indexOf("@") > 0)
	    {
	        var mailBody = "Korisnik <strong>" + options.nickname + "</strong> vam je poslao link: <br/><a href='" + document.location.href + "'>" + document.location.href + "</a><br/><br/>Poruka korisnika:<br/>" + mailComment;
	        $.ws(
			{
			    wsUrl: window.CustomWsURL,
			    wsMethod: "SendMail",
			    wsParams: { mailFrom: "antena@antenazagreb.hr", mailTo: mailTo, mailSubject: mailTitle, mailBody: mailBody },
			    success: function (data)
			    {
			        if (data != null)
			        {
			            if (data.d == true)
			            {
			                $(statusText).html("Va&scaron; mail je uspje&scaron;no poslan!");
			                $(btn).hide();
			            }
			            else
			            {
			                $(statusText).html("<span style='color:red;'>Došlo je do greške prilikom slanja.</span>");
			            }
			        }
			    }
			});
	    }
	    else
	    {
	        $(statusText).html("<span style='color:red;'>E-mail adresa je obavezno polje. Provjerite format i unos.</span>");
	    }
	    return false;
	});
}

function MediaGalleryCtrl(id, url, el)
{
	var KillGallery = function (e)
	{
		$("#MediaGalleryOverlay").remove();
		$("#MediaGalleryWrapper").remove();
	};
	KillGallery();
	var overlay = $(document.createElement("div")).addClass("mediaGalleryOverlay").css("opacity", 0.7).attr("id", "MediaGalleryOverlay").click(KillGallery);
	var wrapper = $(document.createElement("div")).addClass("mediaGalleryWrapper").css("height", $(document).height()).attr("id", "MediaGalleryWrapper");
	var iframe = $(document.createElement("iframe"));

	$(iframe).attr("src", url);
	$(iframe).attr("scrolling", "no");
	$(iframe).attr("frameborder", "0");
	$(iframe).attr("allowTransparency", "true");
	$(iframe).css("border", "none").css("width", "100%").css("height", "100%");

	$(wrapper).append(iframe);
	$("body").append(overlay).append(wrapper);
}
function UserManagementPage(url, classWrapper, el)
{
	var KillUserManagementPage = function (e)
	{
		$("#UserManagementOverlay").remove();
		$("#UserManagementWrapper").remove();
	};
	KillUserManagementPage(overlay);
	var overlay = $(document.createElement("div")).addClass("mediaGalleryOverlay").css("opacity", 0.7).attr("id", "UserManagementOverlay").click(KillUserManagementPage);
	var wrapper = $(document.createElement("div")).addClass(classWrapper).attr("id", "UserManagementWrapper");
	var iframe = $(document.createElement("iframe"));

	$(iframe).attr("src", url);
	$(iframe).attr("scrolling", "no");
	$(iframe).attr("frameborder", "0");
	$(iframe).attr("allowTransparency", "false");
	$(iframe).css("border", "none").css("width", "100%").css("height", "100%");

	$(wrapper).append(iframe);
	$("body").append(overlay).append(wrapper);
}


function GetURL(url)
{
	if (url != undefined) document.location = url;
	return false;
}

function ChangeStation(stationID)
{
	var player = parent.document.getElementById("OnlinePlayer");
	player.ChangeStation(stationID);
	return false;
}

function SafeFadeIn(el)
{
	$(el).css("opacity", 0).show().animate({ opacity: 1 }, 400);
}

function Shoutbox(options, el)
{
	var hasFocus = true;
	var intervalTime = 0;
	var minIntervalTime = 12000;
	var maxIntervalTime = 24000;
	var shoutboxScrolling = null;
	var shoutboxTimeout = null;
	var oldShoutboxContent = null;

	var ResetIntervalTime = function () { intervalTime = minIntervalTime; };
	var FocusOut = function () { hasFocus = false; };
	var FocusIn = function ()
	{
		hasFocus = true;
		RequestMsgs();
	};

	var ShoutboxStopTimer = function ()
	{
		if (shoutboxTimeout != undefined && shoutboxTimeout != null) clearTimeout(shoutboxTimeout);
	};
	var ShoutboxStartTimer = function ()
	{
		ShoutboxStopTimer();
		shoutboxTimeout = setTimeout(ShoutboxTimerTick, intervalTime);
	}
	var ShoutboxTimerTick = function ()
	{
		intervalTime += 1000;
		if (intervalTime > maxIntervalTime) intervalTime = maxIntervalTime;
		hasFocus ? RequestMsgs() : ShoutboxStartTimer();
	};

	this.RequestMessages = function ()
	{
		alert("requesting");
		RequestMsgs();
	};

	var RequestMsgs = function ()
	{
		ShoutboxStopTimer();
		if (shoutboxScrolling == null || shoutboxScrolling == undefined || !shoutboxScrolling)
		{
			$.ws(
			{
				wsUrl: options.wsUrl,
				wsMethod: "GetShoutboxMessages",
				wsParams: { djsID: options.djId },
				success: function (data)
				{
					if (data != null)
					{
						var newContent = data.d;
						if (oldShoutboxContent != newContent)
						{
							$(el).html(newContent);
							oldShoutboxContent = newContent;
							$(el).scrollTop($(el)[0].scrollHeight);
						}
					}
					ShoutboxStartTimer();
				}
			});
		}
		else
		{
			ShoutboxStartTimer();
		}
	};

	$(el).bind("scrollstart", function()
	{
		shoutboxScrolling = true;
	});
	$(el).bind("scrollstop", function ()
	{
		shoutboxScrolling = false;
		RequestMsgs();
	});

	$(options.activityContainer).mouseover(ResetIntervalTime);
	"onfocusin" in document ? document.onfocusin = FocusIn : window.onfocus = FocusIn;
	"onfocusin" in document ? document.onfocusout = FocusOut : window.onblur = FocusOut;
	ResetIntervalTime();
	RequestMsgs();
}

function StandaloneGallery(el, options)
{
    if (parent.window.location == document.location)
    {
        $(".galleryHeaderLeft, .galleryHeaderRight").show();
        $(".btnCloseGallery").hide();
        $("#galleryBanner728x90").openX({ zoneId:options.bannerZoneId, width:options.bannerWidth, height:options.bannerHeight, adServerDomain:options.bannerDomainUrl });
    }
    else
    {
        $("body").css("background", "none transparent");
    }
}

function TrackEvent(el, options)
{
    if (_gaq != undefined && options != undefined)
    {
        if (_gaq != null && options.category != undefined && options.action != undefined)
        {
            try
            {
                _gaq.push(["_trackEvent", options.category, options.action, options.label, options.value]);
            }
            catch (err) { }
        }
    }
}

(function ($)
{
	$.fn.extend(
	{
		changeStation: function (stationID)   { return this.each(function () { new ChangeStation(stationID); }); },
		channelPlaylist: function (options)   { return this.each(function () { new ChannelPlaylist(options, this); }); },
		colBoxCtrl: function ()               { return this.each(function () { new ColBoxCtrl(this); }); },
		currentInfo: function (options)       { return this.each(function () { new CurrentInfo(options, this); }); },
		currentSong: function ()              { return this.each(function () { new CurrentSong(this); }); },
		getURL: function (url)                { return this.each(function () { new GetURL(url); }); },
		headlines: function ()                { return this.each(function () { new Headlines(this); }); },
		mediaGallery: function (id, url)      { return this.each(function () { new MediaGalleryCtrl(id, url, this); }); },
		promoContent: function (options)      { return this.each(function () { new PromoContent(this, options); }); },
		safeFadeIn:function()                 { return this.each(function () { new SafeFadeIn(this); }); },
		share: function (options)             { return this.each(function () { new ShareCtrl(this, options); }); },
		shoutbox:function(options)            { return this.each(function () { $(this).data("shoutbox", new Shoutbox(options, this)); }); },
        trackEvent:function(options)          { return this.each(function () { new TrackEvent(this, options); }); },
		ucTopLists:function ()                { return this.each(function () { new UcTopLists(this); }); },
        userManagementPage:function(url, css) { return this.each(function () { new UserManagementPage(url, css, this); }); },
		standaloneGallery:function(options)   { return this.each(function () { new StandaloneGallery(this, options); }); }
	});
})(jQuery);


window.SwitchUserManagement = function (url, wrapper)
{
    $("#UserManagementOverlay").remove();
    $("#UserManagementWrapper").remove();
    $(document).userManagementPage(url, wrapper);
}

/* ------------------- ADDED BY SERGEJ [start] -------------------------- */
function PlayInline(siteRootUrl, sender, url, title)
{
	if (sender != null && url != null && siteRootUrl != undefined)
	{
	    $("#inlinePlayerWrapper").remove();
		var offset = $(sender).offset();
		var div = $(document.createElement("div"))[0];
		$(div).attr("id", "inlinePlayerWrapper").addClass("inlinePlayer").css("top", Number(offset.top - 123).toString() + "px").css("left", offset.left + "px");
		$("body").append(div);
		$(div).flashembed({ src: siteRootUrl + "InlineMp3Player.swf", id: "inlineMp3Player", wmode: "transparent" }, { url: url, title: title });
		var closeBtn = document.createElement("div");
		$(closeBtn).addClass("inlinePlayerBtn");
		$(div).append(closeBtn);
		$(closeBtn).click(function (e)
		{
		    $(div).remove();
		});
	}
}

function AutoMailFrom(options, el)
{
	var debugText = null;
	var mailFrom = null;
	options.submitText = "pošalji";
	options.msgMandatoryFields = "Niste ispunili obavezna polja!";
	options.msgSuccess = "Vaša prijava je uspješno poslana!";
	options.msgFault = "Došlo je do greške prilikom slanja. Molimo pokušajte ponovo!";
	options.mailSubject = options.mailSubject.replace("&scaron;", "š");
	$(el).addClass("autoForm");
	var ValidateInit = function ()
	{
		var isValid = false;
		if (el != undefined && options != undefined)
		{
			if (options.mailTo != undefined && options.mailSubject != undefined && options.wsPath != undefined && options.fields != undefined)
			{
				if (options.fields.length > 0) isValid = true;
			}
		}
		return isValid;
	};
	var SubmitForm = function (e)
	{
		var csv = "";
		var err = null;
		csv = "<table><tr>";
		$(options.fields).each(function (i)
		{
			csv += "<th>" + this.title + "</th>";
		});
		csv += "</tr><tr>";
		$(el).children(".AUTOFIELD").each(function (i)
		{
			var data = $(this).data("fData");
			if (data.isMandatory == true && $(this).val().length <= 0) err = options.msgMandatoryFields;
			if (data.isEmail == true && data.isMailFrom == true) mailFrom = $(this).val();
			switch (data.type)
			{
				case "textInput":
				case "textArea":
					csv += "<td>" + $(this).val() + "</td>";
					break;
				case "checkbox":
					csv += "<td>" + this.checked + "</td>";
					break;
				default:
					break;
			}
		});
		csv += "</tr></table>";
		if (err == null)
		{
			if (mailFrom != null)
			{
				$.ws(
                    {
                    	wsUrl: options.wsPath,
                    	wsMethod: "SendMail",
                    	wsParams:
                        {
                        	mailFrom: mailFrom,
                        	mailTo: options.mailTo,
                        	mailSubject: options.mailSubject,
                        	mailBody: csv
                        },
                    	success: function (data)
                    	{
                    		$(debugText).text(options.msgSuccess);
                    		$(el).children(".AUTOFIELD").val("");
                    	},
                    	error: function (msg, data)
                    	{
                    		$(debugText).text(options.msgFault);
                    	}
                    });
			}
		}
		else
		{
			$(debugText).text(err);
		}
	};
	if (ValidateInit())
	{
		$(options.fields).each(function (i)
		{
			var field = null;
			switch (this.type)
			{
				case "textInput":
					field = $(document.createElement("input")).attr("type", "text");
					break;
				case "textArea":
					field = $(document.createElement("textarea"));
					break;
				case "checkbox":
					field = $(document.createElement("input")).attr("type", "checkbox");
					break;
				default:
					break;
			}
			if (field != null)
			{
				$(field).addClass("AUTOFIELD").data("fData", this);
				var labelText = (this.isMandatory ? "<span class='MANDATORY'>*</span>" : "") + this.title;
				var label = $(document.createElement("label")).html(labelText);
				if (this.type == "checkbox")
				{
					$(field).css("float", "left");
					$(label).css("float", "left").css("clear", "none");
					$(el).append(field).append(label);
				}
				else
				{
					$(el).append(label).append(field);
				}
			}
		});
		var submitBtn = $(document.createElement("input")).attr("type", "button").attr("value", options.submitText);
		debugText = $(document.createElement("div")).addClass("ERROR");
		$(el).append(submitBtn);
		$(el).append(debugText);
		$(submitBtn).click(SubmitForm);
	}
}

(function ($)
{
	$.fn.extend(
	{
		autoMailForm: function (options)
		{
			return this.each(function () { $(this).data("autoMailForm", new AutoMailFrom(options, this)); });
		}
	});
})(jQuery);
/* -------------------- ADDED BY SERGEJ [end] --------------------------- */

/* -- STAR RATING -- [start] -- */
function refreshRating(){
    $(".star-rating-control div").removeClass("star-rating-hover");
    setVoting();
    setChannelRating();
}

function setVoting(){
	if (!votingEnabled)
    {
		$(".star-rating-control div").addClass("star-rating-readonly");
        $(".star-rating-control div a").each(function(){ $(this).attr("title",""); });
        $(".star-rating-control *").css("cursor", "default");
        $(".star-rating-control div a").unbind("click");
    }
}

function setChannelRating()
{
    //alert("ASD");
	if (channelRating != 0)
    {
        for (i = 0; i < channelRating; i++)
		    $(".star-rating-control div").eq(i+1).addClass("star-rating-on");
    }
}

function setStarRating(votingEnabled, channelRating){
    refreshRating();

	$(".star-rating-control div a").hover(function (e)
	{
		if (!$(this).parent().hasClass("star-rating-readonly"))
		{
		    var vote = parseInt($(this).attr("title"));

		    $(".star-rating-control div").removeClass("star-rating-on");

		    for (i = 0; i < vote; i++)
		        $(".star-rating-control div").eq(i + 1).addClass("star-rating-hover");
		}
    },
    function ()
    {
        $(".star-rating-control div").removeClass("star-rating-hover");
        //if (!$(this).parent().hasClass("star-rating-readonly")) setChannelRating();
	});

	$(".star-rating-control div a").click(function (e)
	{
		if (!$(this).parent().hasClass("star-rating-readonly"))
		{
		    var vote = parseInt($(this).attr("title"));
		    $("#hidVote").val(vote);
            votingEnabled = false;
            channelRating = vote;
            refreshRating();
		}
	});
}
/* -- STAR RATING -- [end] -- */
