function set_cookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure ){
    var cookie_string = name + "=" + escape ( value );
    if ( exp_y ){
        var expires = new Date ( exp_y, exp_m, exp_d );
        cookie_string += "; expires=" + expires.toGMTString();
    }

    if ( path ){
        cookie_string += "; path=" + escape ( path );
    }
    if ( domain ){
        cookie_string += "; domain=" + escape ( domain );
    }
    if ( secure ){
        cookie_string += "; secure";
    }
    document.cookie = cookie_string;
}

function get_cookie ( cookie_name ){
    var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
    if ( results ){
        return ( unescape ( results[2] ) );
    }
    else{
        return null;
    }
}

function delete_cookie ( cookie_name ){
    var cookie_date = new Date ( );  // current date & time
    cookie_date.setTime ( cookie_date.getTime() - 1 );
    document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

(function($){
function neo_banner_movable(){
     if(get_cookie('bannerfirst')){
       delete_cookie ('bannerfirst');
    }

    if(get_cookie('bannersec')){
       delete_cookie ('bannersec');
    }


    if(get_cookie('bannerThird')!='false'){
        set_cookie ("bannerThird", "true", 2015, 01, 15, '/' );
    }

    if(get_cookie('bannerFourth')!='false'){
        set_cookie ("bannerFourth", "true", 2015, 01, 15, '/' );
    }

    var height_hide =$(document).height()/4;
    var body_height =(height_hide*4)/2;
    var neo_ban = $('#neo_banner_one');
    var neo_ban_sec =  $('#neo_banner_two');
    neo_ban.addClass('expander');
    neo_ban_sec.addClass('expander');
    function nf_close(){
        neo_ban.find('.neo_ban_close').fadeOut();
    }
    function ns_close() {
        neo_ban_sec.find('.neo_ban_close').fadeOut();
    }
    function nf_open(){
        neo_ban.find('.neo_ban_close').fadeIn();
    }
    function ns_open() {
        neo_ban_sec.find('.neo_ban_close').fadeIn();
    }
    $('.neo_ban_close').click(function(){
        var obj = $(this);
        var parent_id = obj.parents('.neo_banner').attr('id');
        obj.data({
            position:'closed'
        });
        obj.fadeOut().parents('.neo_banner').animate({
            right:'-296px'
        }, 350);
        if(parent_id=='neo_banner_one'){
            set_cookie ("bannerThird", "false", 2015, 01, 15, '/' );
        }
        if(parent_id=='neo_banner_two'){
            set_cookie ("bannerFourth", "false", 2015, 01, 15, '/' );
        }
        return false;
    });

    $('.neo_banner').mouseenter(function(){
        var obj = $(this);
        if(obj.hasClass('expander')){
            obj.stop().animate({
                right:0
            }, 350, function(){
                obj.find('.neo_ban_close').fadeIn();
            });
        }
    });

    $('.neo_banner').mouseleave(function(){
        var obj = $(this);
        if(obj.hasClass('expander')){
            if(obj.css('right')!='-296px'){
                obj.stop().animate({
                    right:'-296px'
                }, 350, function(){
                    obj.find('.neo_ban_close').fadeOut();
                });
            }
        }
    });

    $(window).scroll(function(){
        var top_pos =$(window).scrollTop();
        if(top_pos<height_hide){
            neo_ban.stop().animate({
                right:'-296px'
            }, 350,  nf_close);
            neo_ban_sec.stop().animate({
                right:'-296px'
            }, 350, ns_close);
            neo_ban.removeClass('expander').addClass('expander');
            neo_ban_sec.removeClass('expander').addClass('expander');
        }
        else if(top_pos>height_hide && top_pos<body_height){
            //      if(neo_ban.css('right')=='-296px'){

            if(get_cookie('bannerThird')!='false'){
                neo_ban.stop().animate({
                    right:0
                }, 350, nf_open);
                neo_ban.removeClass('expander').addClass('collapser');
            }
            neo_ban_sec.stop().animate({
                right:'-296px'
            }, 350, ns_close);
            neo_ban_sec.removeClass('collapser').addClass('expander');

            //      }
            return false;
        }
        else if(top_pos>height_hide && top_pos>body_height){
            //      if(neo_ban.css('right')!='-296px'){
            neo_ban.stop().animate({
                right:'-296px'
            }, 350, nf_close);
            neo_ban.removeClass('collapser').addClass('expander');
            if(get_cookie('bannerFourth')!='false'){
                neo_ban_sec.stop().animate({
                    right:0
                }, 350, ns_open);
                neo_ban_sec.removeClass('expander').addClass('collapser');
            }
            //      }
            return false;
        }
    });

    $('.neo_banner').removeClass('hidden');
}


$(document).ready(function(){
    neo_banner_movable();
    });
})(jQuery);
