/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


function wxSlideWindow(ajax, div, dir){

    var obj = this;
    
	this.ajax = ajax;
	this.div = div;
    this.dir = dir;
    
    this.extra = {};

    // Status das divs
    
    this.prev = 'windowX';
    this.curr = 'windowY';
    this.next = 'windowZ';

    this.first = false;
    this.last = false;

    this.pos = 0;

    this.busy = true;
    this.direction = true;  // Sentido (p/frente ou p/ trás) do slideshow

    // ====================================================
    // INICIALIZA
    // ====================================================
    this.init = function(){


        var html = '<div id="btn-prev"><img border="0" src="' + this.dir + '/img/prev.png"></div>';
        html += '<div id="btn-next"><img border="0" src="' + this.dir + '/img/next.png"></div>';
		html += '<div id="windowX"></div>';
		html += '<div id="windowY"></div>';
        html += '<div id="windowZ"></div>';
		
        
        $('#' + obj.div).html(html);

        // Obtém por AJAX os 3 HTML iniciais

        // Obtem tabela inicial
		$.ajaxSetup({async: true, cache: false});
		var arg = this.extra;				
		arg.action = 'init';		
		$.post(this.ajax, arg, obj.init2);
        
    }


    this.init2 = function(xml){
        
        var objxml = new Parsing(xml);        
		
		var erro = objxml.GetAttr("erro");
		var erroDesc = objxml.GetAttr("erro_desc");


        if (erro){                 
            alert(erroDesc);
            return false;
        }        

        $('#' + obj.div + ' #' + obj.prev).html(objxml.GetAttr("div1"));
        $('#' + obj.div + ' #' + obj.curr).html(objxml.GetAttr("div2"));
        $('#' + obj.div + ' #' + obj.next).html(objxml.GetAttr("div3"));
        
        if (objxml.GetAttr("first")) obj.first = true;
        if (objxml.GetAttr("last")) obj.last = true;

        obj.startEvents();
    }

    // ====================================================
    // TROCA DIV PARA ANTERIOR
    // ====================================================
    this.changePrev = function(){
        
        // EFEITO DE JANELA
        $("#" + this.div + " #" + this.curr).hide("slide", { direction: "right" }, 1000);
        $("#" + this.div + " #" + this.prev).show("slide", { direction: "left" }, 1000, function(){

            var temp = obj.next;
            obj.next = obj.curr;  //Proxima passa a ser atual
            obj.curr = obj.prev;  //Atual passa a ser a anterior
            obj.prev = temp;

            obj.last = false;
            obj.pos--;


            if (obj.pos == 0){
                obj.first = true;
                obj.startEvents();
            }else{
                obj.change = obj.prev; // Objeto que será trocado (salvo aqui para ser pego pelo outro script)
                obj.load(obj.pos - 1);
            }
            
        });



    }

    // ====================================================
    // TROCA DIV PARA PROXIMO
    // ====================================================
    this.changeNext = function(){
        
        // EFEITO DE JANELA
        $("#" + this.div + " #" + this.curr).hide("slide", { direction: "left" }, 1000);
        $("#" + this.div + " #" + this.next).show("slide", { direction: "right" }, 1000, function(){

            var temp = obj.prev;
            obj.prev = obj.curr;  //Proxima passa a ser atual
            obj.curr = obj.next;  //Atual passa a ser a anterior
            obj.next = temp;

            obj.first = false;
            obj.pos++;

            obj.change = obj.next; // Objeto que será carregado
            obj.load(obj.pos + 1);            
            
        });

    }

    // ====================================================
    // LOAD
    // ====================================================
    this.load = function(pos){        

        // Obtem tabela inicial
		$.ajaxSetup({async: true, cache: false});
		var arg = this.extra;
		arg.action = 'get';
        arg.pos = pos;

		$.post(this.ajax, arg, obj.load2);

    }

    this.load2 = function(xml){

        //alert(xml);

        var objxml = new Parsing(xml);

		var erro = objxml.GetAttr("erro");
		var erroDesc = objxml.GetAttr("erro_desc");


        if (erro){
            alert(erroDesc);
            return false;
        }

        
        $('#' + obj.div + ' #' + obj.change).html(objxml.GetAttr("div"));        

        if (objxml.GetAttr("empty")){

            if (obj.change == obj.prev) obj.prev = true;
            if (obj.change == obj.next) obj.last = true;
            
        }
        

        obj.startEvents();
    }

    // ====================================================
    // TROCA DE FOTO (EXECUTADO EXTERNAMENTE)
    // ====================================================
    this.troca = function(){
        if (!this.busy){
           if (obj.direction){
               if (!obj.last){
                    obj.stopEvents();
                    obj.changeNext();                    
               }else{
                   obj.direction = !obj.direction;
               }
           }else{
               if (!obj.first){
                    obj.stopEvents();
                    obj.changePrev();
               }else{
                   obj.direction = !obj.direction;                   
               }               
           }
        }
    }
    // ====================================================
    // EVENTOS
    // ====================================================

    this.startEvents = function(){

        this.busy = false;
		// -------------------------------------------------------------
		// ----------- DIV ANTERIOR -----------------------
		// -------------------------------------------------------------
		$("#" + this.div + " #btn-prev").bind('click', function(){
           if (!obj.first){
                obj.stopEvents();
                obj.changePrev();
           }


		});

		// -------------------------------------------------------------
		// ----------- DIV POSTERIOR -----------------------
		// -------------------------------------------------------------
		$("#" + this.div + " #btn-next").bind('click', function(){
            if (!obj.last){
                obj.stopEvents();
                obj.changeNext();
            }

		});
        
    }

    this.stopEvents = function(){
        this.busy = true;
        //alert('parando');
        $("#" + this.div + " #btn-prev").unbind();
        $("#" + this.div + " #btn-next").unbind();

    }
}