/*
    sexslurp.com porn archive
    (c) 2009 sexslurp.com development team, all rights reserved
*/

var sex = "http://sexslurp.com/";
var api = "http://sexslurp.com/api/";
var cdn = "http://cdn.sexslurp.com/";

function in_array(needle, haystack, argStrict)
{
    var key = '', strict = !!argStrict;
 
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }
 
    return false;
}

$(document).ready(function() {
    
    /* global vars on load */
    var origwidth = 0;
    if($("div.mainimg").length > 0)
    {
        var origwidth = $("div.mainimg").width();
    }
    var lastwidth = 0;
    
    var exclude_hentai = 1;
    var exclude_fetish = 1;
    var exclude_shemale = 1;
    
    /* resize main image viewing to visible pane */
    function resizeimg()
    {
        var m = 460;
        var i = 0;
        if($("div.mainimg").length > 0)
        {
            var i = $("div.mainimg").width();
        }
        var w = $(window).width() - m;
        if(i > w)
        {
            $("div.mainimg").css("width", w);
            $("div.mainimg img").css("width", w);
            lastwidth = w;
        }
        if(w > lastwidth)
        {
            if(w > origwidth)
            {
                $("div.mainimg").css("width", origwidth);
                $("div.mainimg img").css("width", origwidth);
            }
            else
            {
                $("div.mainimg").css("width", w);
                $("div.mainimg img").css("width", w);
            }
            lastwidth = w;
        }
    }
    
    /* default searchbox text */
    function searchtext()
    {
        var eg = "eg: lesbian teens fucking"
        if($("input#q").val() == "")
        {
            $("input#q").val(eg);
            $("input#q").css("color", "#999");
        }
        $("input#q").focus(function(){
            if($("input#q").val() == eg)
            {
                $("input#q").val("");
                $("input#q").css("color", "#000");
            }
        });
        $("input#s").focus(function(){
            if($("input#q").val() == eg)
            {
                $("input#q").val("");
            }
        });
    }
    
    /* colour swatch box */
    function colourswatch()
    {
        $("#color").change(function(){
            v = $("#color").val();
            if (v.length == 7 && coloursearching == 0)
            {
                coloursearch(v.substring(1));
            } 
        });
        $(".sl-marker").mouseup(function(){
            v = $("#color").val();
            if (v.length == 7 && coloursearching == 0)
            {
                coloursearch(v.substring(1));
            } 
        });
        $(".h-marker").mouseup(function(){
            v = $("#color").val();
            if (v.length == 7 && coloursearching == 0)
            {
                coloursearch(v.substring(1));
            } 
        });
    }
    
    /* check our colour search preferneces */
    function coloursearchpref()
    {
        $("#include_hentai").change(function(){
            if($('#include_hentai').is(':checked'))
            {
                exclude_hentai = 1;
            }
            else
            {
                exclude_hentai = 0;
            }
        });
        $("#include_fetish").change(function(){
            if($('#include_fetish').is(':checked'))
            {
                exclude_fetish = 1;
            }
            else
            {
                exclude_fetish = 0;
            }
        });
        $("#include_shemale").change(function(){
            if($('#include_shemale').is(':checked'))
            {
                exclude_shemale = 1;
            }
            else
            {
                exclude_shemale = 0;
            }
        });
    }
    
    /* perform an API request for a colour search */
    function coloursearch(hex)
    {
        coloursearching = 1;
        /* magic category filtering */
        var x = [];
        if (exclude_hentai == 1)
        {
            x.push("31");
        }
        if (exclude_fetish == 1)
        {
            x.push("11");
        }
        if (exclude_shemale == 1)
        {
            x.push("30");
        }
        $("#results").fadeOut(100, function(){
            $("#results span").remove();
            $("#results a").remove();
            $("#results img.ajaxloader").remove();
            $("<img />").attr("src", "/static/images/ajax-loader.gif").attr("class", "ajaxloader").appendTo("#results");
            $("#results").fadeIn(100);
        });
        $.getJSON(api + "?request=colour&num=50&hexcode=" + hex + "&exclude=" + x + "&jsoncallback=?",
            function(data){
                $("#results img.ajaxloader").remove();
                coloursearching = 0
                $.each(data.items, function(i,item){
                    if(!in_array(item.categoryid, data.exclude))
                    { 
                        $("<img />").fadeIn(1000).attr("src", cdn + item.galleryhash.substring(0,1) + '/' + item.galleryhash + '/t_' +  item.imagehash + '.jpg').appendTo("#results").wrap(
                        '<a class="i" rel="' + item.galleryid + '" href="' + sex + 'image/' + item.ihex + '"></a>' + "\n");
                    }
                });
            }
        );
    }
    
    /* binds / runtime */
    var coloursearching = 0
    $(window).bind("resize", resizeimg);
    resizeimg();
    searchtext();
    $("<span></span>").text("Just pick a colour from the colour wheel!").appendTo("#results");
    coloursearchpref();
    colourswatch();

});

/*
	eof
*/
