var last_page = '';

$.noConflict();
jQuery(document).ready( function($) {
    $("a[rel*=colorbox]").colorbox();

    $("#accordion").accordion({active: false, collapsible: true, autoHeight: false});
    $("#accordion a.req").click(function(event){
        event.preventDefault();
        jQuery.facebox({ ajax: '/ajax/a_schedule/index/'+jQuery(this).attr('rel')});
    });


    $("ul.ajax_menu li a").click(function(event){
        event.preventDefault();

        var page = $(this).attr('href');
        if (last_page==page) {
            $("#article_content").html('&nbsp;');
            last_page = '';
        } else {
            last_page = page;
            $.ajax({
                type: "POST",
                url: "/ajax/a_pages/get_page_by_url",
                data: "p="+page,
                success: function(data){
                    $("#article_content").html(data);
                }
            });
        }
    });  
});

//##############################################################################
function JCWPSlider() {
    var _this = this;
    var ajax_url = '';
    var pages_l=0;
    var pages_r=0;
    var news_date='';
    var condition=0;
    var cur_page_l = 0;
    var cur_page_r = 0;
    var container = null;
    var width_item = 0;
    var aleft = null;
    var aright = null;
    var count_right = 0;
    var count_left = 0;
    var step = 0;
    var is_run = false;

    this.init = function (url, id, left, right, width, pagesl, pagesr, date, cond) {
        ajax_url = url;
        pages_l = pagesl;
        pages_r = pagesr;
        news_date = date;
        condition = cond;
        container = jQuery(id);
        width_item = width;
        
        aleft = jQuery(left);
        aleft.click(_this.arrow_left);
        aright = jQuery(right);
        aright.click(_this.arrow_right);
    }
        
    this.arrow_left = function(event) {
        event.preventDefault();
        _this.getContent('l');
    }
        
    this.arrow_right = function(event) {
        event.preventDefault();
        _this.getContent('r');
    }
        
    this.getContent = function(d) {
        if (is_run) return;
        is_run = true;
        var cur_page = 0;
        if (d=='l') {
            cur_page = cur_page_l;
        } else {
            cur_page = cur_page_r;
        }
        
        var left = parseInt(container.css("margin-left"));
        if (d=='r' && count_right>0) {
            left -= width_item;
            container.stop().animate({"margin-left":left},1000,'swing', _this.animateComplete);
            count_right--;
            count_left++;
        } else if (d=='l' && count_left>0) {
            left += width_item;
            container.stop().animate({"margin-left":left},1000,'swing', _this.animateComplete);
            count_right++;
            count_left--;
        } else {
            jQuery.ajax({
                type: "POST",
                url: ajax_url,
                data: "d="+d+"&p="+(cur_page+1)+"&date="+news_date+"&c="+condition,
                dataType: "json",
                success: function(data){
                    var width = parseInt(container.css("width"));
                    var left = parseInt(container.css("margin-left"));
                    var left2 = left;
                    
                    width += data.count*width_item;
                    left-=data.count*width_item;
                    var html = container.html();
                    if (data.d=='l') {
                        container.html(data.html+html);
                        container.css({"width": width+"px", "margin-left":left});
                        if (data.count>1) {
                            left2-=width_item;
                            container.stop().animate({"margin-left":left2},1000,'swing', _this.animateComplete);
                            count_left+=data.count-1;
                        } else {
                            container.stop().animate({"margin-left":0},1000,'swing', _this.animateComplete);
                        }
                        count_right++;
                        cur_page_l++;
                    } else {
                        container.html(html+data.html);
                        container.css({"width": width+"px"});
                        if (data.count>1) {
                            left2-=width_item;
                            container.stop().animate({"margin-left":left2},1000,'swing', _this.animateComplete);
                            count_right+=data.count-1;
                        } else {
                            container.stop().animate({"margin-left":left},1000,'swing', _this.animateComplete);
                        }
                        count_left++;
                        cur_page_r++;
                    }
                }
            });             
        }
    }
        
    this.redrawArrow = function() {
        if (count_left>0 || (condition==1 && cur_page_l<pages_l) || (condition==2 && cur_page_l+1<pages_l)) {
            aleft.removeClass("notshow");
        } else {
            aleft.addClass("notshow");
        }
    
        if (count_right>0 || (cur_page_r+1<pages_r)) {
            aright.removeClass("notshow");
        } else {
            aright.addClass("notshow");
        }
    }
    
    this.animateComplete = function() {
        _this.redrawArrow();
        is_run = false;
    }
}
