﻿
String.prototype.urlEncode = function() { return encodeURIComponent(this); }

var WBS = {

//========================================================
//  WBS.UI
//========================================================

    
    UI : {
        Init : function() 
        {
            $("#aspnetForm,#form1").submit(function() {
                if (window._abortSubmit) return false;
            });
            
            
            // Navigation
            WBS.UI.SetupNavigation();
            WBS.UI.BindActionLinks();
            WBS.Search.BindSearchForms();
            
            // Fix PNG
            $("#nav img, #home-sidebar, #search-submit, #search-button, #popular-searches img, #sidebar h2, #footer img").pngfix();
            
            // External Urls
            $("a.exturl,a.newwin").click(function() {
				return !window.open($(this).attr("href"),'','');
            });
        },
        
        
        DisableSubmit : function() {
			window._abortSubmit = true;
        },
        
        EnableSubmit : function() {
			window._abortSubmit = false;
        },
        
        //========================================================
        //  Loops through navigation items without classname "on"
        //  and adds rollover functionality.
        //========================================================
        SetupNavigation : function()
        {
            var bodyClass = $("body").attr("class");
            
            $("#nav li").each(function(i, el)
            {
                var li = $(this);
                
                // Check if <body class""> is found in the id attr
                // of the current <li>, if so, set image to "on"
                if (li.attr("id").indexOf(bodyClass) > -1 && bodyClass.length > 0)
                {
                    li.css("z-index", 100);
                    
                    var img = $("img", li);

                    var newSrc = img.attr("src").replace(".png", "-on.png");
                    img.attr("src", newSrc);
                }
                else
                {
                    var img = $("img", li);
                    img.bind("mouseover", function()
                    {
                        var newSrc = img.attr("src").replace(".png", "-over.png");
                        img.attr("src", newSrc);
                    });
                    img.bind("mouseout", function()
                    {
                        var newSrc = img.attr("src").replace("-over.png", ".png");
                        img.attr("src", newSrc);
                    });
                }
            });
        },
        
        
        PreloadImages : function()
        {
            for (var i = 0; i<arguments.length; i++)
            {
                $("<img>").attr("src", arguments[i]);
            }
        },
        
        
        BindActionLinks: function(el)
        {
			if (!el) el = $(document);
			
            //================================================
            //   USER LINKS
            //================================================ 
            
            $("a.userlink", el).bind("click", function () {
               
                var offset = $(this).offset();
                var url = $(this).attr("href");
               
                $.ajax({
                    type: "GET",
                    url: url, 
                    success: function(html) {
                        var div = $(html);
                        div.hide();
                        div.css({
                            top: offset.top + "px",
                            left: offset.left + "px"
                        });
                        
                        $(".closeUserInfo", div).click(function() {
							div.fadeOut("fast");
                        });
                        
                        $(".userInfoBg", div).css("opacity", .7);
                        
                        WBS.UI.BindActionLinks(div);
                        
                        div.appendTo("body");
                        div.fadeIn("fast");
                    }
                });
                
                return false;
            });
            
            
            //================================================
            //   PEER SPHERE
            //================================================ 

            $("a.peersphere", el).bind("click", function () {
                var link = $(this);
                var url = link.attr("href");
                
                $.ajax({
                    type: "GET",
                    url: url, 
                    success: function(html) {
						$(link).before(html);
						link.remove();
                    }
                });

                return false;
            });
            
            
            //================================================
            //   VOTING CODE
            //================================================ 

            $("a.commentVoteYes,a.commentVoteNo,a.topicVoteYes,a.topicVoteNo", el).bind("click", function () {
                var link = $(this);
                var url = link.attr("href");
                
                if (WBS.Data.Comment.Vote(url))
                {
                    var vote = (url.toLowerCase().indexOf("vote=yes") > -1) ? "Yes" : "No";
                    disableVoting(link.parent(), vote);
                }
                return false;
            });

        },
        
        
        // This depends on jquery.wysiwyg.js
        WYSIWYG : function(el, options) {
			var settings = {
				controls : {
					separator05 : {visible : false},
					createLink : {visible : false},
					insertImage : {visible : false},
					separator06 : {visible : false},
					h1 : {visible : false},
					h2 : {visible : false},
					h3 : {visible : false},
					h1mozilla : {visible : false},
					h2mozilla : {visible : false},
					h3mozilla : {visible : false},
					separator08 : {visible : false},
					increaseFontSize : {visible : false},
					decreaseFontSize : {visible : false},
					separator09 : {visible : false},

					separator00 : {visible : true},
					insertOrderedList : {visible : true},
					insertUnorderedList : {visible : true}
				}
			};
			$.extend(settings, options);
			$(el).wysiwyg(settings);
        }
    },
    
    
//===============================================
// WBS.Search
//===============================================
    
    Search : {
        Submit: function(searchTerm)
        {
            window._abortSubmit = true;
            window.location = $("#search-url").val() + encodeURIComponent(searchTerm);
        },
        
        BindSearchForms: function()
        {
            if ($("#search-submit,#search-button").length)
            {
                $("#search-submit,#search-button").click(function() {
                    return WBS.Search.Submit($("#search-box").val());
                });
            }

            if ($("#footer-search-button").length)
            {
                $("#footer-search-button").click(function() {
                    return WBS.Search.Submit($("#footer-search-box").val());
                });
            }
            
            if ($("#search-box").length || $("#footer-search-box").length)
            {
                $("#search-box,#footer-search-box").keydown(function(e) {
                    if (e.keyCode == "13")
                    {
                        WBS.Search.Submit($(this).val());
                        return false;
                    }
                });
            }
        }
    },



//===============================================
// WBS.Data
//===============================================

    Data : {
    
    
        //=====================================
        //  WBS.Data.Topic
        //=====================================

        Topic : {
            
            Vote : function(url)
            {
                var result = $.ajax({
                    async: false,
                    type: "GET",
                    url: url, 
                    error: function(a,b,c) {
                        alert("Could not save vote");
                    }
                }).responseText;
                
                return(result == "OK");
            },
            
            Save : function(id)
            {
                var result = $.ajax({
                    async: false,
                    type: "GET",
                    url: "/handlers/topic.ashx/Save", 
                    data: { topicId: id },
                    error: function(a,b,c) {
                        alert("Could not save");
                    }
                }).responseText;
                
                return(result == "OK");
            },
            
            Email : function(emailData)
            {
                var result = $.ajax({
                    async: false,
                    type: "POST",
                    url: "/handlers/topic.ashx/Email", 
                    data: emailData,
                    error: function(a,b,c) {
                        alert(c);
                    }
                }).responseText;
                
                return(result == "OK");
            },
            
            Comment : function(commentData, options)
            {
                var settings = {
                    type: "POST",
                    url: "/handlers/topic.ashx/Comment", 
                    data: commentData,
                    dataType: "json"
                };

				if (options) jQuery.extend(settings, options);
				
                $.ajax(settings);
            },
            
            Flag : function(id)
            {
                var result = $.ajax({
                    async: false,
                    type: "GET",
                    url: "/handlers/topic.ashx/Flag", 
                    data: { topicId: id },
                    error: function(a,b,c) {
                        alert("Could not save");
                    }
                }).responseText;
                
                return(result == "OK");
            }

           
        },
        
        //=====================================
        //  WBS.Data.Poll
        //=====================================

        Poll : {
            
            Vote : function(url)
            {
                var result = $.ajax({
                    async: false,
                    type: "GET",
                    url: url, 
                    error: function(a,b,c) {
                        alert("Could not save vote");
                    }
                }).responseText;
                
                return(result == "OK");
            },
                        
            Comment : function(commentData, options)
            {
                var settings = {
                    type: "POST",
                    url: "/handlers/poll.ashx/Comment", 
                    data: commentData,
                    dataType: "json"
                };

				if (options) jQuery.extend(settings, options);
				
                $.ajax(settings);
            }

           
        },        
        
        //=====================================
        //  WBS.Data.Comment
        //=====================================
        Comment : {
        
            Vote : function(url)
            {
                var result = $.ajax({
                    async: false,
                    type: "GET",
                    url: url, 
                    error: function(a,b,c) {
                        alert("Could not save vote");
                    }
                }).responseText;
                
                return(result == "OK");
            },
            
            Flag : function(url)
            {
                var result = $.ajax({
                    async: false,
                    type: "GET",
                    url: url, 
                    error: function(a,b,c) {
                        alert("Could not save");
                    }
                }).responseText;
                
                return(result == "OK");
            }
        },
        
        
        
        //=====================================
        //  WBS.Data.User
        //=====================================
        User : {
            
            Authenticate : function(email, password, options)
            {
				var settings = {
                    type: "GET",
                    dataType: "json",
                    url: "/handlers/user.ashx/Authenticate", 
                    data: { e: email, p: password }
                };
                
				if (options) jQuery.extend(settings, options);
				
                $.ajax(settings);
            },
            
            AddIndustry : function(industryId)
            {
				var result = $.ajax({
                    async: false,
                    type: "GET",
                    url: "/handlers/user.ashx/AddIndustry",
                    data: { industryId: industryId }
                }).responseText;
                
                return result;
            },
            
            RemoveIndustry : function(userIndustryId)
            {
                $.ajax({
                    async: false,
                    type: "GET",
                    dataType: "json",
                    url: "/handlers/user.ashx/RemoveIndustry", 
                    data: { userIndustryId: userIndustryId },
                    error: function(a,b,c) {
                        WBS.UI.DisableSubmit();
                    }
                });
            }
        }        
    }
};

function requireAnswer() {
    var answered = false;
    if (!answered && $("#ctl00_sb_psb_PollAnswers input[type=radio]:checked").size())
        answered = true;

    if (!answered) {
        alert('Please select an answer');
        causeValidation = true;
    }

    return answered;
}