 var BaseUrl = "";

 function setbaseUrl(){
     BaseUrl = $('base').attr("href");
 }

 function getLoc(loc){
     return BaseUrl + loc;
 }

 function loc(loc){
     return document.location.href = getLoc(loc);
 }

 $(document).ready(function() {
    {if(uSettings.shortcut_key != "off")}
        var ctrlKey = "{if(uSettings.shortcut_key)}{{uSettings.shortcut_key}}{else}Ctrl{close}";
        $.Shortcuts.add({
        type: 'down',
        mask: ctrlKey + '+G',
        handler: function() {
            name = prompt("{lang('Enter group name or id')}");
            if(name)
                document.location = "{{HomeDir}}/groups-go/search:" + name;
        }
        });
        $.Shortcuts.add({
	    type: 'down',
	    mask: ctrlKey + '+Q',
	    handler: function() {
	        id = prompt("{lang('Enter problem ID')}");
	        if(parseInt(id))
		        document.location = "{{HomeDir}}/problems/" + id;
	    }
        });
        $.Shortcuts.add({
	    type: 'down',
	    mask: ctrlKey + '+S',
	    handler: function() {
	        if($("a#send-problem-solution").length) {
		    if($("a#send-problem-solution").attr("href") != "javascript: void(0)")
		        document.location = $("a#send-problem-solution").attr("href");
		    else
		        $("a#send-problem-solution").click();
	        } else {
		    document.location = "{{HomeDir}}/solutions-send";
	        }
	    }
        });
        $.Shortcuts.add({
	    type: 'down',
	    mask: ctrlKey + '+M',
	    handler: function() {
    	        if($("a#send-user-message").length) {
		    if($("a#send-user-message").attr("href") != "javascript: void(0)")
		        document.location = $("a#send-user-message").attr("href");
		    else
	    	        $("a#send-user-message").click();
	        } else {
		    document.location = "{{HomeDir}}/message/";
	        }
	    }
        });
        $.Shortcuts.add({
	    type: 'down',
	    mask: ctrlKey + '+O',
	    handler: function() {
	        document.location = "{{HomeDir}}/competitions/";
	    }
        });
        $.Shortcuts.add({
	    type: 'down',
	    mask: ctrlKey + '+P',
	    handler: function() {
	        document.location = "{{HomeDir}}/problems/";
	    }
        });
        $.Shortcuts.add({
	    type: 'down',
	    mask: ctrlKey + '+F',
	    handler: function() {
	        if($("div.search input.text, #search-line").length) {
		    $("div.search input.text, #search-line").focus();
	        } else {
		    search = prompt("{lang('Enter search text')}");
		    if(search)
		        qSearch(search);
	        }
	    }
        });
        {if(uid)}
            $.Shortcuts.add({
	        type: 'down',
	        mask: ctrlKey + '+L',
	        handler: function() {
	            document.location = "{{HomeDir}}/users-logout/";
	        }
            });
            {assign("groups", "group-list")}
            {set(count, 0)}
            {foreach(list, l)}
                {set(count, count + 1)}
                $.Shortcuts.add({ // {{l.grp_name}}
                    type: 'down',
                    mask: 'Alt+F{{count}}',
                    handler: function() {
                        document.location = "{{HomeDir}}/groups/{{l.grp_id}}";
                    }
                });
                {if(count == 12)}{break}{close}
            {close}
        {close}
        $.Shortcuts.start();
        $("a[title^='Shortcut']").addClass("shortcut");
    {close}



    $(".search input.text").bind("keypress", function(e){
        if(e.keyCode == 13) qSearch();
    });

     setbaseUrl();
     $("#content input[type='submit']").css('font-weight', 'bold');

     /* {if(uSettings.fast != "off")} */
     if($(".infoman div.box div.box-item").length >= 1) {
         var t = this;
         t.box_counter = 0;
         t.slide_pause = 0;
         t.boxes = $(".infoman div.box div.box-item");
         title = $(t.boxes[t.box_counter]).attr('title');
         if(title) $("div.infoman h1").html(title);
         if(t.boxes.length > 1) {
             t.boxes.hide();
             $(".infoman div.box").bind('mouseenter', function() { t.slide_pause = 1; });
             $(".infoman div.box").bind('mouseleave',  function() { t.slide_pause = 0; });
             $(t.boxes[t.box_counter]).show();
             setInterval(function() {
                if(t.slide_pause) return;
                $(t.boxes[t.box_counter]).css('z-index', 10);
                /* {if(uSettings.fast == "one")} */
                $(t.boxes[t.box_counter]).slideUp();
                /* {else} */
                $(t.boxes[t.box_counter]).hide();
                /* {close} */
                t.box_counter++;
                if(t.box_counter >= t.boxes.length) t.box_counter = 0;
                title = $(t.boxes[t.box_counter]).attr('title');
                if(title) $("div.infoman h1").html(title);
                $(t.boxes[t.box_counter]).css('z-index', 9);
                $(t.boxes[t.box_counter]).show();
             }, 5000);
         }
     }
     $("input[type='button'][href]").each(function() {
         $(this).click(function() {
             /*alert($(this).attr('href'));*/
             location.href = $(this).attr('href');
         });
     });
     /* {close} */

     proccessATag();

     /* {assign("config", "media", "imageHightQuality", "off")} */
     /* {if(imageHightQuality == "on")} */
     $("img.media-image").css("opacity", 0.2);
     $("img.media-image").each(function() {
         var getIDReg = /media-get\/([0-9]+)/;
         w = $(this).attr('width');
         h = $(this).attr('height');
         s = $(this).attr('src');
         if(getIDReg.test(s)) {
            arr =  getIDReg.exec(s);
            id = arr[1];
            $(this).attr("src", "{{RootDir}}/media-get-resized/" + id + "/" + w + "/" + h + "/");
         }
         $(this).css("opacity", 1);
     });
     /* {close} */
     /* {if(uSettings.fast == "one")} */
     setInterval(function () {
         $(".hot").animate({opacity: 0}, 200, function() {
             $(".hot").animate({opacity: 1}, 200);
         });
     }, 1000);
     /* {close} */
     /* {if(uSettings.fast != "off")} */
     $("div.tab").each(function() {
        var t = this;
        t.tabLocation = $("a", this).attr('href');
         if(!/ajax$/.test(t.tabLocation)) t.tabLocation = t.tabLocation + 'ajax';
        $("a", this).attr('href', 'javascript: void(0)');
        $(this).click( function() {
             if($("#ajaxprocess").css('display') != 'none') return;
             $("#ajaxprocess").show();
             /* {if(uSettings.fast != "off")} */
                 $("#content").css({opacity: '0.5'});
             /* {close} */
                 $(".tabbox .tab.current").removeClass('current');
                 $(t).addClass('current');
                 $.get(t.tabLocation, function(data) {
                    $("#content").css('opacity', '1');
                    $("#ajaxprocess").hide();
                    $("#content").html(data);
                    bindLinks();
                    proccessATag();
                 });
         });
     });
     if(/#ajax:(.*)/.test(document.location)) {
         arr = /#ajax:(.*)/.exec(document.location);
         $("#content").append("<a href='" + arr[1] + "' class='toajax' id='ajax-redirect' style='display:none'>#</a>");
     }
     bindLinks();
     $("#ajax-redirect").click();
     /* {close} */
 });

 function proccessATag() {
     $("a[confirm]").each( function() {
         var hLocation = $(this).attr('href');
         if(hLocation != 'javascript: void(0)') {
             $(this).attr('href', 'javascript: void(0)');
             $(this).click(function() {
                   if(confirm($(this).attr('confirm'))) location.href = hLocation;
             });
         }
     });
 }

 function bindLinks() {
     $("#content input[type='submit']").css('font-weight', 'bold');
     $("#content a.page, #content a.toajax, #content input.toajax[type='button'][href]").each(function() {
         if($(this).hasClass("bind-success")) return true;
         var t = this;
         t.ajaxLocation = $(t).attr('href');
         if(!/ajax$/.test(t.ajaxLocation)) t.ajaxLocation = t.ajaxLocation + 'ajax';
         $(t).attr('href', 'javascript: void(0)');
         $(t).addClass('bind-success');
         $(t).click(function() {
             if($("#ajaxprocess").css('display') != 'none') return;
             $("#ajaxprocess").show();
             /* {if(uSettings.fast != "off")} */
                 $("#content").css({opacity: '0.5'});
             /* {close} */
                 $.get(t.ajaxLocation, function(data) {
                    al = new String(document.location);
                    al = al.replace(/#(.*)$/, "");
                    document.location = al + "#ajax:" + t.ajaxLocation.replace(/ajax$/, "").replace(/^{print(str_replace('/', '\/', RootDir))}/, "");
                    $("#content").css('opacity', '1');
                    $("#ajaxprocess").hide();
                    $("#content").html(data);
                    $(".tabbox .tab.current").each(function() {
                        this.tabLocation = t.ajaxLocation;
                    });
                    bindLinks();
                    proccessATag();
                 });
         });
     });
 }

    function qSearch(search_text) {
	if(typeof search_text == 'undefined') search_text = $(".search input.text").val();
        document.location = "problems/search:" + search_text;
    }
    
    $(function() {
	var ponetimer = setTimeout(function() {
	    $(".google-plus-one").fadeOut("slow");
	}, 20000);
	$(".header").hover(function() {
	    clearTimeout(ponetimer)
	    $(".google-plus-one").stop(true, true).fadeIn("fast");
	}, function() {
	    clearTimeout(ponetimer)
	    $(".google-plus-one").delay(6000).fadeOut("fast");
	});
    });
