$(document).ready(function(){
        if (!$.cookie('region')) $.cookie('region', 1, { expires: 180, path: "/" });
        my_toggle=function(e,id){
            e.preventDefault();
            if ('none' == $(id).css("display")) {
              if ($(document).width() > (e.pageX+($(id).width()/2)+70)) {
                $(id).css("left",e.pageX-($(id).width()/2));
              } else {
                $(id).css("left",$(document).width()-$(id).width()-70);
              }
              $(id).css("top",e.pageY+20);
              $(id).show("slow");
            } else {
              $(id).hide();               
            }
        };  
        $(".why").click(function(e){my_toggle(e,"#why");});
        $("#why").click(function(){
            $("#why").hide("slow");
        });     
        $("#profile_region").change(function(){
            $.cookie('region', $(this).val().split('_')[1], { expires: 180, path: "/" });
            $.cookie('partner', $(this).val().split('_')[0], { expires: 180, path: "/" });
            window.location='/stock'+$(this).val().split('_')[0]+'/film';
        });
        $("option").each(function () {
            if ($.cookie('region') == $(this).val().split('_')[1]) 
               $(this).attr('selected', 'yes');
        });        
        $(".film_remove").each(function(e){
             id='#film'+$(this).attr("id").split("_")[1];
             $(id).hide();
        });
        $(".film_add").click(function(e){
             e.preventDefault();
             t=$(this);
             t.hide().after("<img border=0 height=14 src='/img/wait16trans.gif'>");
             $.ajax({
                type: "GET",
                url: $(this).attr("href"),
                error: function(msg){
                  alert("Не удалось добавить фильм в корзину!");
                },
                success: function(msg){
                    $("#basket").stop()
                    .animate( { opacity: 0 }, 150, null, 
                        function() {$("#basket").html(msg).animate({ opacity: 1 }, 1000);}
                    );
                    $("img",t.parent()).remove();
                }
             });
        });     
});
