$(document).ready(function(){
    $('#loadingicon').css('display','none');                
    var url = window.location.hash;    
    var width = $(window).width();
    var step = (width-960)/2+960;
    //step = 1000;
    var i = 0;

    $('#container').height($('.container1').height());
    $('.container2').css('margin-left',step);
    //$('.container2').css('opacity','0');                
    if(url.charAt(0)=='#'){          
        $.post('/go',{'action' : 'firstopen','url' : url}, function(data){                        
            if(data.result == 'ok'){                               
                i+=1;
                document.location.hash = data.url; 
                $.config.id = data.id;
                if(i%2==1){
                    $('.container2').html(data.content);
                    $('#container').animate({
                        height: $('.container2').height()
                    },1000,'easeInOutQuart');
                    $('.container1').animate({
                        marginLeft: '-='+step,
                        opacity: '0'
                    },1000,'easeInOutQuart');
                    $('.container2').animate({
                        marginLeft: '-='+step,
                        opacity: '100'
                    },1000,'easeInOutQuart');
                } else {
                    $('.container1').html(data.content);
                    $('#container').animate({
                        height: $('.container1').height()
                    },1000,'easeInOutQuart');
                    $('.container1').animate({
                        marginLeft: '+='+step,
                        opacity: '100'
                    },1000,'easeInOutQuart');
                    $('.container2').animate({
                        marginLeft: '+='+step,
                        opacity: '0'
                    },1000,'easeInOutQuart');
                }
                $("a[rel=example_group]").fancybox({
                    'transitionIn'		: 'none',
                    'transitionOut'		: 'none',
                    'titlePosition' 	: 'over'
                });
            }
            $('#loadingicon').css('display','none');
        },'json');
    }                

    $('#again').click(function(){                    
        $('#loadingicon').css('display','block');
        $.post('/go',{'action' : 'viewfilm'}, function(data){                        
            if(data.result == 'ok'){                               
                i+=1;
                $.config.id = data.id;
                document.location.hash = data.url;                            
                if(i%2==1){
                    $('.container2').html(data.content);
                    $('#container').animate({
                        height: $('.container2').height()
                    },1000,'easeInOutQuart');
                    $('.container1').animate({
                        marginLeft: '-='+step,
                        opacity: '0'
                    },1000,'easeInOutQuart');
                    $('.container2').animate({
                        marginLeft: '-='+step,
                        opacity: '100'
                    },1000,'easeInOutQuart');
                } else {
                    $('.container1').html(data.content);
                    $('#container').animate({
                        height: $('.container1').height()
                    },1000,'easeInOutQuart');
                    $('.container1').animate({
                        marginLeft: '+='+step,
                        opacity: '100'
                    },1000,'easeInOutQuart');
                    $('.container2').animate({
                        marginLeft: '+='+step,
                        opacity: '0'
                    },1000,'easeInOutQuart');
                }
                $("a[rel=example_group]").fancybox({
                    'transitionIn'		: 'none',
                    'transitionOut'		: 'none',
                    'titlePosition' 	: 'over'
                });
            }
            $('#loadingicon').css('display','none');
        },'json');
    });
    $('#changesettings').click(function(){
        $('#categories').slideToggle();
    });
    $('#closesettings').click(function(){
        $('#categories').slideToggle();
    });
    $('#sugget').click(function(){
        $('#suggetmovie').slideToggle();
    });
    $('#closesugget').click(function(){        
        $('#suggetmovie').slideToggle();
        $('.senderror').html(''); 
    });
    $('.smovie').click(function(){
        $('.smovie').val('');
    });
    $('#sendmovie').click(function(){
        var moviename = $('.smovie').val();
        if(moviename != ''){
            $.post('/go',{'action' : 'sendmovie', 'moviename' : moviename}, function(data){   
                if(data.result == 'error'){
                    $('.senderror').html(data.content);
                    $('#sendassept').click(function(){
                        $.post('/go',{'action' : 'accepted'},function(data){
                            $('.senderror').html('You movie has been sended.'); 
                            $('.smovie').val('');
                        },'json');
                    });
                    $('#sendstop').click(function(){
                        $('.senderror').html(''); 
                    });
                } else {
                    $('.smovie').val('');
                    $('.senderror').html('You movie has been sended.');
                }
            },'json');
        }
    });    
    $("a[rel=example_group]").fancybox({
            'transitionIn'		: 'none',
            'transitionOut'		: 'none',
            'titlePosition' 	: 'over'
    });
    $('#dontshowfilm').click(function(){
        var filmid = getCookie('dontshowfilmid');
        if(filmid == '')
            var arr = [];
        else
            var arr = filmid.split(',');
        var find = false;
        for(var i=0; i< arr.length; i++){
            if(arr[i] == $.config.id){
                arr.splice(i,1);
                find = true;
                break;
            }               
        }
        if(find == false)
            arr.push($.config.id);
        var str = implode(',',arr);
        alert(str);
        setCookie('dontshowfilmid',str,"Mon, 01-Jan-2021 00:00:00 GMT");
    });
});
function setCookie (name, value, expires, path, domain, secure) {
    document.cookie = name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}
function getCookie(name) {
    var cookie = " " + document.cookie;
    var search = " " + name + "=";
    var setStr = null;
    var offset = 0;
    var end = 0;
    if (cookie.length > 0) {
        offset = cookie.indexOf(search);
        if (offset != -1) {
            offset += search.length;
            end = cookie.indexOf(";", offset)
            if (end == -1) {
                    end = cookie.length;
            }
            setStr = unescape(cookie.substring(offset, end));
        }
    }
    return(setStr);
}
function changesettings(id){
    $.post('/settings',{'action' : 'settgenres','id' : id}, function(data){},'json');
}
function implode( glue, pieces ) {
    return ( ( pieces instanceof Array ) ? pieces.join ( glue ) : pieces );
}
