$(document).ready(function(){
    var create_message = function(message) {
        var msg = $("<div></div>");
        msg.css({
            "background": "#234",
            "color": "#fff",
            "font-size": "12px",
            "left": 0,
            "padding": "6px",
            "position": "absolute",
            "top": 0,
            "z-index": 99999
        });
        msg.text(message);
        msg.click(function(){
            msg.animate({
                "opacity": 0,
                "top": "+=30"
            }, 300, "linear", function() { $(this).remove()})
        });
        setTimeout(function() {
            msg.trigger("click");
        }, 3000);
        return msg;
    }

    var show_message = function(message) {
        var msg = create_message(message);
        var form_elem = $("form#send_to_friend");
        var form_pos = form_elem.offset();
        msg.css({
            "left": form_pos.left,
            "top": form_pos.top + form_elem.height() + 4,
            "width": form_elem.width()
        });
        $("body").prepend(msg);
    }

    var send_to_friend_callback = function(data, textStatus) {
        if (textStatus !== "success" || data.result === "ERROR") {
            show_message("Tavsiyeniz gönderilemedi.");
        } else if (data.result === "UNAUTHORIZED") {
            show_message("Davetiye göndermek için giriş yapmanız gerekiyor.");
        } else if (data.result === "INVALID") {
            show_message("Geçersiz bir e-posta adresi girdiniz!");
        } else {
            i = $("form#send_to_friend input:first");
            show_message("Tavsiyeniz " + i.val() + " adresine gönderildi. " +
                                                          "Teşekkür ederiz.");
            i.blur();
        }
    }

    $("form#send_to_friend").submit(function() {
        $.post($(this).attr("action"),
                    $(this).serialize(),
                    send_to_friend_callback,
                    "json");
        return false;
    });
    $("form#send_to_friend input:first").blur(function() {
        $(this).val("Arkadaşına gönder")
    }).focus(function() { $(this).val("")});
});



$(document).ready(function(){
    $("div#sponsored_companies div.domestic, div#sponsored_companies div.commercial").hover(function() {
        $(this).addClass("hover");
    }, function() {
        $(this).removeClass("hover");
    });
});



$(document).ready(function(){
    var rows = 5;
    var column_width = 250;
    var interval_msecs = 10000;
    var anim_speed = 1000;

    var make_column = function() {
        var col = $("<div></div>").css({
            width: column_width+"px",
            float: "left"
        });
        return col;
    }
    var cols = Math.ceil($("div#sponsored_companies > div").length/rows);
    if (cols > 1) {
        var frame = $("<div></div>").css({
            width: column_width+"px",
            overflow: "hidden"
        });
        var sliding = $("<div></div>").css(
                "width", String(cols*column_width)+"px").append(make_column());
        $("div#sponsored_companies > div").each(function(i) {
            var col = sliding.children(":last");
            if (col.children("div").length >= rows) {
                var col = make_column();
                sliding.append(col);
            }
            col.append(this);
        });
        $("div#sponsored_companies").replaceWith(frame);
        sliding.attr("id", "sponsored_companies").appendTo(frame);

        var slide_anim = function() {
            for (var i=0; i<cols-1; i++) {
                setTimeout(function() {
                    sliding.animate({
                        marginLeft: "-="+column_width+"px"
                    }, anim_speed);
                }, (i+1)*interval_msecs);
            }
            setTimeout(function() {
                sliding.animate({
                    marginLeft: "+="+String((cols-1)*column_width)+"px"
                }, anim_speed);
                slide_anim();
            }, cols*interval_msecs);
        }
        slide_anim();
    }
});

