﻿// JScript File

function addEditOverlay(selector)
{
	$(selector).each(function(el)
	{
		$(this).find("span").hide();
		$(this).hover(
			function() { $(this).find("span").stop(true, true).slideDown(); }, 
			function() { $(this).find("span").slideUp("slow"); }
		);
		return $(this);
	});			
}

function initProfileSlider() {
	var _slideSpeed = 450;
	$('.profile-nav').each(function(){
		var _opener = $(this).find('a.view-profile');
		var _slider = $(this).parent().find('div.slide-profile');


		_opener.click(function(){
			if(_opener.hasClass('active')) {
				_opener.removeClass('active');
				_slider.slideUp(_slideSpeed);
			} else {
				_opener.addClass('active');
				_slider.slideDown(_slideSpeed);
			}
			return false;
		});
	});
}

this.challengeScroller = function() {

    $("#challoading").hide();
    var loading = false;

    $(".load-more").bind("click", function(e) {
        if (!loading) {
            var p = $("#hcPage").val();
            $("#chalSearchResults").html('');
            $("#challoading").show();
            $("#chalSearchResults").load("/partial_highlightedchallenges.aspx?p=" + p, {},
							    function() {
							        $("#challoading").hide();
							        $("#chalSearchResults").show();
							        loading = false;
							        //tooltip();
							    }
						    );
            loading = true;
        }
    });

}

this.searchLayer = function() {
    var ft = true;
    $(".search input").bind("focus click keydown", function(e) {
        if (ft) { $(this).val(''); ft = false; }
        $('#searchOptionsPanel').show();
    });

    $('.search').hover(
	    function() { $(this).find(".options").show(); },
	    function() { $(this).find(".options").hide(); }
    );
}

this.partialPagePaging = function(toHide, partialPageUrl, perPage, endbit) {

    $("#pcloading").hide();
    var loading = false;

    $(".paging").bind("click", function(e) {

        if (!loading) {
            $(".paging").removeClass("active");
            $(toHide).html('');
            $("#pcloading").show();
            var st = parseInt($(this).html());
            var p = st - 1;
            $(toHide).load(partialPageUrl + "?p=" + p + "&ps=" + perPage + endbit.replace("&amp;","&"), {},
						    function() {
						        $("#pcloading").hide();
						        $(toHide).show();
						        loading = false;
						    }
					    );
            loading = true;
            $(this).addClass("active");
        }
    });

    $(".pagingNext").bind("click", function(e) {
        doPaging(toHide, partialPageUrl, perPage, endbit, 0, $(this));
    });

    $(".pagingPrev").bind("click", function(e) {
        doPaging(toHide, partialPageUrl, perPage, endbit, -2, $(this));
    });

    function doPaging(toHide, partialPageUrl, perPage, endbit, dir, clicked) {
        if (!loading) {
            var fpp = $("#fppPage").val();
            $(".paging").removeClass("active");
            $(toHide).html('');
            $("#pcloading").show();
            var st = parseInt(clicked.html());
            var p;
            if (st > 0) p = st - 1;
            else { p = parseInt(fpp) + dir; }
            $(toHide).load(partialPageUrl + "?p=" + p + "&ps=" + perPage + endbit.replace("&amp;", "&"), {},
						    function() {
						        $("#pcloading").hide();
						        $(toHide).show();
						        loading = false;
						    }
					    );
            loading = true;
            clicked.addClass("active");
        }
    }
}

/*
 * Tooltip script 
 * powered by jQuery (http://www.jquery.com)
 * 
 * written by Alen Grakalic (http://cssglobe.com)
 * 
 * for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
 *
 */

this.tooltip = function(){	
	/* CONFIG */		
		xOffset = 30;
		yOffset = 14;		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result		
	/* END CONFIG */		
	$("a.tooltip").hover(function(e){									  
		this.t = this.title;
		this.title = "";									  
		$("body").append("<p id='tooltip'>"+ this.t +"</p>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#tooltip").remove();
    });	
	$("a.tooltip").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};

this.formtooltip = function(){	
	/* CONFIG */		
		xOffset = 0;
		yOffset = 14;		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result		
	/* END CONFIG */		
	$("input.tooltip").focus(function(e){									  
		this.t = this.title;
		this.title = "";									  
		$("body").append("<span id='formtooltip' class='hint'>"+ this.t +"</span>");
		$("#formtooltip")
			.css("top",($(this).offset().top - xOffset) + "px")
			.css("left",($(this).offset().left + $(this).width() + yOffset) + "px")
			.fadeIn("fast");		
    });
    
    $("input.tooltip").blur(function(){
		this.title = this.t;
		$("#formtooltip").remove();
	});	
};
