$(document).ready(function() {
    // add in page loading
    $("input.search").bind("focus",function() {
        if ($(this).val()=="Search") {
            $(this).val("");
        }
    });

    $("input.search").bind("blur",function() {
        if ($(this).val()=="") {
            $(this).val("Search");
        }
    });
    
    $("li.spot>a.item").bind("click",function(ev) {
        var vthis = $(this).parent();
        if ($("ul.submenu",vthis).is(":hidden")) {
            $("ul.submenu",vthis).slideDown();
        } else {
            $("ul.submenu",vthis).slideUp();
        }
        $("a.close",vthis).bind("click",function(ev) {
            $("ul.submenu",vthis).slideUp();
            ev.stopImmediatePropagation();
            return false;
        })
        ev.stopImmediatePropagation();
        return false;
    })
    
    $("input#twittersend").bind("click",function() {
        if ($("input#twitterlink").val()=="") {
            $("div.twittermsg").addClass("error").hide().append("Please enter a valid twitter link").slideDown();

            setTimeout(function() {
                $("div.twittermsg").slideUp(function() {
                    $(this).removeClass("error").html("");
                });
            },3000);
            return;
        }
        $.post("/ajax.php?function=sendtwitter",{link: $("input#twitterlink").val()},function(data) {
            $("input#twitterlink").val("");
            $("div.twittermsg").addClass("status").hide().append(data.output).slideDown();
            
            setTimeout(function() {
                $("div.twittermsg").slideUp(function() {
                    $(this).removeClass("status").html("");
                });
            },3000)
        },"json");
    });

    $("input#photosend").bind("click",function() {
        if ($("input#photolink").val()=="") {
            $("div.photomsg").addClass("error").hide().append("Please enter a valid photo link").slideDown();

            setTimeout(function() {
                $("div.photomsg").slideUp(function() {
                    $(this).removeClass("error").html("");
                });
            },3000);
            return;
        }

        $.post("/ajax.php?function=sendphoto",{link: $("input#photolink").val()},function(data) {
            $("input#photolink").val("");
            $("div.photomsg").addClass("status").hide().append(data.output).slideDown();

            setTimeout(function() {
                $("div.photomsg").slideUp(function() {
                    $(this).removeClass("status").html("");
                });
            },3000)
        },"json");
    });

    $("input#videosend").bind("click",function() {
        if ($("input#videolink").val()=="") {
            $("div.videomsg").addClass("error").hide().append("Please enter a valid video link").slideDown();

            setTimeout(function() {
                $("div.videomsg").slideUp(function() {
                    $(this).removeClass("error").html("");
                });
            },3000);
            return;
        }

        $.post("/ajax.php?function=sendvideo",{link: $("input#videolink").val()},function(data) {
            $("input#videolink").val("");
            $("div.videomsg").addClass("status").hide().append(data.output).slideDown();

            setTimeout(function() {
                $("div.videomsg").slideUp(function() {
                    $(this).removeClass("status").html("");
                });
            },3000)
        },"json");
    });
    
    $("a.page","div.pagelist").live("click",function(ev) {
        var url = $(this).attr("href");
        $.get(url,function(data) {
            $("#directory").html($("#directory",data).html());
            $("#leftcolumnlist").html($("#leftcolumnlist",data).html());
            $("#rightcolumnlist").html($("#rightcolumnlist",data).html());
            $("#pagelist").html($("#pagelist",data).html());
            $("#pagelist2").html($("#pagelist2",data).html());
        },"html");
        
        ev.stopImmediatePropagation();
        return false;
    });
    
    $("img#hotstory").bind("click",function(ev) {
        $.get("/hotstory",function(data) {
            $("#loverlay").fadeIn();
            $("#lbox").html("");
            $("#lbox").append($("div#story",data));
            $("#lbox").prepend("<img src='/images/icons/pencilcross.png' id='pencilcross' />");
            $("img#pencilcross","#lbox").bind("click",function() {
                $("#lbox").fadeOut(function() {
                    $("#lbox").html("");
                    $("#loverlay").fadeOut();
                })
            })
            $("#lbox").css({"width": "656px","marginLeft": "-328px","marginTop": "-220px"}).fadeIn(function() {
                $("div.small").hide();
                $(".thefield").live("focus",function() {
                    $("div.small",$(this).parent()).show();
                }).bind("blur",function() {
                    $("div.small",$(this).parent()).hide();
                });                
                
                $("input#sendit","#lbox").live("click",function(ev) {
                    $("div#story","#lbox").fadeOut();
                    $.post("/hotstory",{
                        "title": $("#title","#lbox").val(),
                        "where": $("#where","#lbox").val(),
                        "scoop": $("#scoop","#lbox").val()
                    },function(data) {
                        $("div#story","#lbox").html($("div#story",data).html());
                        $("#lbox").css({"marginTop": "-50px"});
                        $("div#story","#lbox").stop(true,true).fadeIn();
                    },"html");
                    ev.stopImmediatePropagation();
                    return false;
                })
            });
            
        },"html");
        ev.stopImmediatePropagation();
        return false;        
    })
    
    $("#loverlay").bind("click",function() {
                $("#lbox").fadeOut(function() {
                    $("#lbox").html("");
                    $("#loverlay").fadeOut();
                })
    })
    
})

function doJobList(start,thesize) {
    var jl = $("ul.joblist");
    var counter = 0;
    var h = 0;
    var toHeight = 0;
    $("li",jl).slice(0,thesize+1).each(function() {
        var pad = parseInt(/[0-9]+/.exec($(this).css("paddingTop"))) + parseInt(/[0-9]+/.exec($(this).css("paddingBottom")));
        var mar = parseInt(/[0-9]+/.exec($(this).css("marginTop"))) + parseInt(/[0-9]+/.exec($(this).css("marginBottom")));
        h = h + $(this).height() + pad + mar;
    })
    $("div.jobscroller").css("height",h + "px");
    
    $("div.arrow","li.sidebar").css("opacity",0).show().animate({opacity: 0.5});
    $("div.arrow").bind("mouseenter",function() {
        $(this).stop(true).animate({opacity: 1});
    }).bind("mouseleave",function() {
        $(this).stop(true).animate({opacity: 0.5});    
    }).bind("click",function() {
        if ($(this).hasClass("down")) {
            counter+=thesize;
        } else {
            counter-=thesize;
        }
        
        if (counter>$("li",jl).size()-(thesize-1)) {
            counter = $("li",jl).size()-(thesize-1);
        }
        
        if (counter<0) {
            counter=0; 
            
        }

        var newM = 0;
        if (counter>0) {
            $("li",jl).slice(0,counter).each(function() {
                var pad = parseInt(/[0-9]+/.exec($(this).css("paddingTop"))) + parseInt(/[0-9]+/.exec($(this).css("paddingBottom")));
                var mar = parseInt(/[0-9]+/.exec($(this).css("marginTop"))) + parseInt(/[0-9]+/.exec($(this).css("marginBottom")));
                var lH = $(this).height() + pad + mar;
                newM = newM - lH;
            });            
        }
        jl.stop(true);
        jl.animate({marginTop: newM + "px"});
    });

    if (start>0) {
            counter = start;
            var newM = 0;
            if (counter>0) {
                $("li",jl).slice(0,counter).each(function() {
                    var pad = parseInt(/[0-9]+/.exec($(this).css("paddingTop"))) + parseInt(/[0-9]+/.exec($(this).css("paddingBottom")));
                    var mar = parseInt(/[0-9]+/.exec($(this).css("marginTop"))) + parseInt(/[0-9]+/.exec($(this).css("marginBottom")));
                    var lH = $(this).height() + pad + mar;
                    newM = newM - lH;
                });            
            }
            jl.stop(true);
            jl.css({marginTop: newM + "px"});            
    }
    
}
