﻿
/// ZOOMER CLASS ///
//© 2008 SeatQuest LLC.

function debug_trace(trace){
    alert(trace);
   //console.log("debug_trace: ", trace); 
}


//BOGDAN constructor
function ZOOMER(args)
{
    var _this=this;
    if(args){this.args=args;}
    
    //ELI override parameters that are not set with default parameters.
    this.set_default_args();
    
    //ELI wait for the zoomer div to load.
    framework.observe(this.args.id,'onload',function(){_this.load();});

};

//ELI default parameters
ZOOMER.prototype.args={
    //BOGDAN seat_selected is called when seat selected on the map.
    seat_selected:null,
    section_selected:null,
    debug:null,
    id:null,
    swf_url:null,
    //BOGDAN flash zoomer parameters
    map_xml:null,
    mg:0,
    header:0,
    prog:1,
    msg:0,
    dom:1,
    theme:null,
    mouseover_mg:1,
    stmap:null,
    usetotalprice:0,
    profile:1,
    help:1,
    zoom_ratio:1.5,
    sl:1,
	callback:null,
	enable_section_highlights:false
}

//BOGDAN variables 
//ZOOMER.prototype.swfObject=null;
ZOOMER.prototype.map=null;
ZOOMER.prototype.gmap_controls=[];
ZOOMER.prototype.geocoder=null;
ZOOMER.prototype.swf=null;
ZOOMER.prototype.init_timer=null;
ZOOMER.prototype.flash_finished_loading=false;
ZOOMER.prototype.ready=false;
ZOOMER.prototype.queue={
    load_tickets:null,
    highlight_sections:[]
};
ZOOMER.prototype.zoom_level = 1;
ZOOMER.prototype.actual_sections = [];
ZOOMER.prototype.section_logic = null;



ZOOMER.prototype.set_default_args=function()
{       
    if(!this.args.mg){this.args.mg=0;}
    if(!this.args.header) {this.args.header=0; }
    if(!this.args.prog){this.args.prog=1;}
    if(!this.args.msg){this.args.msg=0;}
    if(!this.args.dom){this.args.dom=1;}
    if(!this.args.mouseover_mg){this.args.mouseover_mg=1;}
    if(!this.args.usetotalprice){this.args.usetotalprice=0;}
    if(!this.args.profile){this.args.profile=1;}
    if((!this.args.help) && (this.args.help!=0) && (this.args.help!=false)){this.args.help=1;}
    if(!this.args.zoom_ratio){this.args.zoom_ratio=1.5;}
    if((!this.args.sl) && (this.args.sl!=0) && (this.args.sl!=false)){this.args.sl=1;}
    if(!this.args.enable_section_highlights){this.args.enable_section_highlights=false;}
};

ZOOMER.prototype.load_complete = function()
{
	framework.zoomer.ready = true;
	console.log("framework.zoomer.ready ", framework.zoomer.ready);
}

ZOOMER.prototype.load=function()
{
    _this = this;
    //TODO finish enable_section_highlights == false
    if(this.args.enable_section_highlights){
        this.actual_sections = this.args.html_sections();
        if(!this.actual_sections){
            setTimeout(function() {_this.load()}, 500);
            return;
        }
    }
    try
    {
        var w = document.getElementById(this.args.id).offsetWidth;
        var h = document.getElementById(this.args.id).offsetHeight;

        var swfObject = new SWFObject(this.args.swf_url, "zoomer", "100%", "100%", "7", "#FFFFFF");
		
        swfObject.addVariable("xml", escape(this.args.map_xml));
        swfObject.addVariable("mg", this.args.mg);
        swfObject.addVariable("header", this.args.header);
        swfObject.addVariable("prog", this.args.prog);
        swfObject.addVariable("msg", this.args.msg);
        swfObject.addVariable("zoomer_width", w);
        swfObject.addVariable("zoomer_height", h);
        swfObject.addVariable("dom", this.args.dom);
        swfObject.addVariable("theme", this.args.theme);
        swfObject.addVariable("mouseover_mg", this.args.mouseover_mg);
        swfObject.addVariable("stmap", escape(this.args.stmap));
        swfObject.addVariable("usetotalprice", this.args.usetotalprice);
        swfObject.addVariable("profile", this.args.profile);
        swfObject.addVariable("help", this.args.help);
        swfObject.addVariable("sl", this.args.sl);
        swfObject.addVariable("browser", framework.browser);
        
        if(this.args.enable_section_highlights) {
            swfObject.addVariable("sectionSelectionMode", this.args.enable_section_highlights);
            swfObject.addVariable("actual_sections", this.actual_sections.join('$,'));
	    }
	    
        swfObject.addParam("allowScriptAccess", "always");
        swfObject.addParam("wmode", "transparent");
        swfObject.write(this.args.id);
        if(framework.browser == 'explorer') {
            if(this.args.callback) this.args.callback();
        }
        this.swf = document.getElementById("zoomer");
        this.swf_loaded_timer = setInterval(function() {_this.swf_loaded()},500);    
    }catch(err){
        this.debug.error("ZOOMER->load",err);
    }       
}

ZOOMER.prototype.set_section_logic = function(section_logic){
    this.section_logic = section_logic;
}


//--------------------------------------------------------------------------------------------------------------------------------------------
//BOGDAN flash callbacks
//--------------------------------------------------------------------------------------------------------------------------------------------
//ELI fires when the zoomer finished loading.
ZOOMER.swf_loaded_timer = null;
ZOOMER.prototype.swf_loaded=function()
{	
	try
	{
		this.swf.noop();
		
		clearInterval(this.swf_loaded_timer);
		this.flash_finished_loading = true;
	    if(this.queue.load_tickets && typeof(this.queue.load_tickets)=='function') 
	    {
	        var _this=this;
	        setTimeout(function(){_this.queue.load_tickets();},1);
	    }
	}	
	catch(err)
	{}
}

//ELI fires when a seat was selected in the zoomer.
ZOOMER.prototype.seat_selected=function(id)
{
    var _this=this;
    
    //ELI call the filter.apply to filter GA tickets when GA is selected.
    if(id.toLowerCase()=='ga')
    {
        setTimeout(function(){framework.filter.apply({params:{ga:1}});},1);
    }
    
    //ELI calls the user seat_selected function.
    this.args.seat_selected(id,false);
    //TODO: Debug why trap doesn't work here.
    //if(this.args.seat_selected){this.debug.trap("ZOOMER->seat_selected",function(){_this.args.seat_selected(id,false);});}
 
}

//--------------------------------------------------------------------------------------------------------------------------------------------
//BOGDAN functions to control flash
//--------------------------------------------------------------------------------------------------------------------------------------------
ZOOMER.prototype.show_seats=function(seats_to_show)
{
    if(this.swf && this.swf.show_seats) 
    {
        this.swf.show_seats(seats_to_show); 
        
        //ELI determine wheather or not to hide the bubble
        var tid=this.last_highlighted_ticket;
        if(tid && seats_to_show.indexOf(tid + '$,')<=0)
        {
            this.reset_highlights();
        };

        return true;
    }else{
        return false;
    }
}

//--------------------------------------------------------------------------------------------------------------------------------------------
ZOOMER.prototype.load_tickets=function(dom)
{
    var _this=this;
    if(this.flash_finished_loading)
    {     
        this.load_tickets_after_flash_loaded(dom);
    } else  {
        this.queue.load_tickets = function(){_this.load_tickets(dom)};
    }
}

ZOOMER.prototype.load_tickets_after_flash_loaded=function(dom){
    if(this.args.enable_section_highlights) {
        this.swf.dom_complete_string("10000$;10000$;10$;10$;10$;10$;10$;10$;10$;10$;10$;10$;10$;$;"); 
        return;
    }else{ 
        var s = this.create_dom_complete_arg(dom);
        this.swf.dom_complete_string(s);
        if(this.args.debug!=null) this.args.debug.time("Zoomer Bridge",true);
        this.unqueue_highlighted_sections_calls();
    }
}

ZOOMER.prototype.unqueue_highlighted_sections_calls=function(){
    for(var i = 0; i < this.queue.highlight_sections.length; i+=2) {
        this.highlight_sections(this.queue.highlight_sections[i], this.queue.highlight_sections[i+1]);
    }
}

ZOOMER.prototype.create_dom_complete_arg=function(dom){
    if(this.args.debug!=null) this.args.debug.time("Array 1st");
    var rows=dom.getElementsByTagName('row');
    if(!rows){return null;}
    var ra={x:[],y:[],e:[],g:[],p:[],op:[],u:[],s:[],r:[],st:[],a:[],d:[],b:[],tb:[], sp:[], bn:[]};
    var reg_ex=new RegExp(';','g');
    //alert(reg_ex);
    for(i=0;i<rows.length;i++)
    {
        var row=rows[i];
        var coords=null;
        coords=row.getAttribute('sq_coords');
        if(coords)
        {
            var c=coords.split(';');
            
            var seats=row.getAttribute('sq_seats').split(';');
            var e=row.getAttribute('sq_exact');
            var p=row.getAttribute('TicketPrice');
            var op=row.getAttribute('OriginalTicketPrice');
            var u=row.getAttribute('TicketID');
            
            if(!this.args.enable_section_highlights) {
                var s=row.getAttribute('SeatSection');
                var r=row.getAttribute('SeatRow');                
            } else {
                var s=row.getAttribute('sq_sec');
                var r=row.getAttribute('sq_row');
            }
            var a=row.getAttribute('Available');
            var d=row.getAttribute('SeatDescription');
            var tb=row.getAttribute('TB');
			var sp=row.getAttribute('ValidSplits');
			var bn=row.getAttribute('BrokerName'); //ELI broker name
			if(sp != null) sp = sp.substring(0, sp.length - 1);
			else sp = '';

            for(var j=0;j<seats.length-1;j++)
            {
                var m=j*2;
                var x=c[m];
                var y=c[m+1];
                if(!this.args.enable_section_highlights) {
                    ra.x.push(x);
                    ra.y.push(y);
                } else {
                    ra.x.push(0); 
                    ra.y.push(0);
                }
                ra.st.push(seats[j]);
                ra.e.push(e);
                ra.g.push(i);
                ra.p.push(p);
                ra.op.push(op);
                ra.u.push(u);
                ra.s.push(s);
                ra.r.push(r);
                ra.a.push(a);
                ra.d.push(d);
                ra.tb.push(tb);
				ra.sp.push(sp);
				ra.bn.push(bn);
            }
        }
    }
    if(this.args.debug!=null) this.args.debug.time("Array 1st",true);
    
    if(this.args.debug!=null) this.args.debug.time("Array 2nd");
    var params = [];
    params.push(ra.x.join("$,"));
    params.push(ra.y.join("$,"));
    params.push(ra.e.join("$,"));
    params.push(ra.g.join("$,"));
    params.push(ra.p.join("$,"));
    params.push(ra.op.join("$,"));
    params.push(ra.u.join("$,"));
    params.push(ra.s.join("$,"));
    params.push(ra.r.join("$,"));
    params.push(ra.st.join("$,"));
    params.push(ra.a.join("$,"));
    params.push(ra.d.join("$,"));
    params.push(ra.tb.join("$,"));
	params.push(ra.sp.join("$,"));
    params.push(ra.bn.join("$,"));
    if(this.args.debug!=null) this.args.debug.time("Array 2nd",true);
    
    if(this.args.debug!=null) this.args.debug.time("Zoomer Bridge");
    return params.join("$;");
}


//--------------------------------------------------------------------------------------------------------------------------------------------
ZOOMER.prototype.enable_magnifying_glass=function(){
    if(this.swf && this.swf.enable_magnifying_glass )  {this.swf.enable_magnifying_glass();return true;}
    else return false;
}
//--------------------------------------------------------------------------------------------------------------------------------------------
ZOOMER.prototype.disable_magnifying_glass=function(){
    if(this.swf && this.swf.disable_magnifying_glass)  {this.swf.disable_magnifying_glass();return true;}
    else return false;
}
//--------------------------------------------------------------------------------------------------------------------------------------------
ZOOMER.prototype.reset_highlights=function()
{
    if(this.swf && this.swf.reset_highlights) {
        this.swf.reset_highlights();
        return true;
    }
    else return false;
}
//--------------------------------------------------------------------------------------------------------------------------------------------
ZOOMER.prototype.show_section_labels=function(enabled)
{
    if(this.swf && this.swf.show_section_labels) 
    {
        if(enabled)
        {
            this.swf.show_section_labels(1);
        }
        else
        {
            this.swf.show_section_labels(0);
        }
        return true;
    }
    else return false;
}
//--------------------------------------------------------------------------------------------------------------------------------------------
ZOOMER.prototype.toggle_price_mode=function(enabled)
{
    if(this.swf && this.swf.toggle_price_mode )
    {
        if(enabled)
        {
            this.swf.toggle_price_mode(1);
        }
        else
        {
            this.swf.toggle_price_mode(0);
        }
        return true;
    }
    else return false;
}
//--------------------------------------------------------------------------------------------------------------------------------------------
ZOOMER.prototype.show_help=function(with_fees)
{
    var _this=this;
    
    if(this.swf && this.swf.show_help ) 
    {
        setTimeout(function() {_this.swf.show_help(with_fees);},0);
        return true;
    } else{ return false;}
}

//--------------------------------------------------------------------------------------------------------------------------------------------
//BOGDAN commented out last_zoom and started using zoom_level, so zoom_out function will work after zoom( )  call
//ZOOMER.prototype.last_zoom=null;
ZOOMER.prototype.zoom_htimer=null;
ZOOMER.prototype.zoom=function(level,delay,timer_done)
{

    var _this=this;

    //ELI kill the timer 
    if(this.zoom_htimer){clearTimeout(this.zoom_htimer);}

    //ELI fire this function with a delay.
    if(!delay){delay=0;}
    if(delay>0 && !timer_done){
        var d = function() {_this.zoom(level,delay,true);};
        this.zoom_htimer=setTimeout(d,delay);
        return;
    }
    
    //ELI execute a zoom.
	if(this.swf && this.swf.zoom_ratio ) 
	{
        if(this.zoom_level!=level ){this.zoom_level=level;}else{return;}
        var f = function() {_this.swf.zoom_ratio(level,false);};
        setTimeout(f,delay);
	    return true;
	}else{ 
	    return false;
	}
}

//--------------------------------------------------------------------------------------------------------------------------------------------
ZOOMER.prototype.zoom_in=function()
{
    if(this.zoom_level >= 4) return;
    var new_level = this.zoom_level * this.args.zoom_ratio;
    if(new_level > 4) new_level = 4;
    this.zoom(new_level);
}
//--------------------------------------------------------------------------------------------------------------------------------------------
ZOOMER.prototype.zoom_out=function()
{
    if(this.zoom_level <=1) return;
    var new_level = this.zoom_level / this.args.zoom_ratio;
    if(new_level < 1) new_level = 1;
    this.zoom(new_level);
}

//--------------------------------------------------------------------------------------------------------------------------------------------
ZOOMER.prototype.highlight_ticket=function(tid) 
{
    var _this=this;
    if(this.swf && this.swf.highlight_ticket)
    { 
        _this.swf.highlight_ticket(tid);
        //setTimeout(function() {_this.swf.highlight_ticket(tid)}, 0);
        this.last_highlighted_ticket=tid;
        return true;
    }
    else return false;
}
//--------------------------------------------------------------------------------------------------------------------------------------------
ZOOMER.prototype.highlight_sections=function(sections_list, color) 
{
   var _this=this;     
   if(this.ready){
    //if(this.swf && this.swf.highlight_sections)
    //{ 
        if(this.args.debug!=null) this.args.debug.time("highlight_sections");
        //setTimeout(function() {_this.swf.highlight_sections(sections_list, color)}, 0);
        _this.swf.highlight_sections(sections_list.join("$,"), color);
        if(this.args.debug!=null) this.args.debug.time("highlight_sections", true);
        return true;
    //}
    //else return false;
    } else {
        this.queue.highlight_sections.push(sections_list);
        this.queue.highlight_sections.push(color);
    }
    
}
//--------------------------------------------------------------------------------------------------------------------------------------------
ZOOMER.prototype.highlight_other_sections=function(sections_list, color) 
{
   var _this=this;     
   if(this.ready){
    //if(this.swf && this.swf.highlight_sections)
    //{ 
        if(this.args.debug!=null) this.args.debug.time("highlight_other_sections");
        //setTimeout(function() {_this.swf.highlight_sections(sections_list, color)}, 0);
        _this.swf.highlight_other_sections(sections_list.join("$,"), color);
        if(this.args.debug!=null) this.args.debug.time("highlight_other_sections", true);
        return true;
    //}
    //else return false;
    } else {
        this.queue.highlight_other_sections.push(sections_list);
        this.queue.highlight_other_sections.push(color);
    }  
}
//--------------------------------------------------------------------------------------------------------------------------------------------
ZOOMER.prototype.highlight_section=function(section_name, color) 
{
    
    var _this=this;
    if(this.swf && this.swf.highlight_section)
    { 
        setTimeout(function() {_this.swf.highlight_section(section_name, color)}, 0);
        return true;
    }
    else return false;
}


ZOOMER.prototype.toggle_ga=function(flag){
    var _this=this;
    if(this.swf && this.swf.toggle_ga)
    { 
        _this.swf.toggle_ga(flag);
        return true;
    }
    else return false;
} 

//--------------------------------------------------------------------------------------------------------------------------------------------
//ZOOMER.prototype.reset_section_highlights=function() 
//{
//    var _this=this;
//    if(this.swf && this.swf.reset_section_highlights)
//    { 
//        if(this.args.debug!=null) this.args.debug.time("reset_section_highlights");
//        //setTimeout(function() {_this.swf.reset_highlights()}, 0);
//        _this.swf.reset_highlights();
//        //_this.swf.reset_highlights(-1,false);
//        if(this.args.debug!=null) this.args.debug.time("reset_section_highlights", true);
//        return true;
//    }
//    else return false;
//}


ZOOMER.prototype.section_selected=function(section_name)
{
    this.section_logic.zoomer_section_selected(section_name);
}


ZOOMER.prototype.show_section_bubble=function(section_name) 
{
    var _this=this;
    if(this.swf && this.swf.show_section_bubble)
    { 
        _this.swf.show_section_bubble(section_name);
        return true;
    }
    else return false;
}

ZOOMER.prototype.reset_section_highlight=function(section_name) 
{
    var _this=this;
    if(this.swf && this.swf.reset_section_highlight)
    { 
        _this.swf.reset_section_highlight(section_name);
        return true;
    }
    else return false;
}


ZOOMER.prototype.get_section_translations = function(){
    var result = this.swf.get_section_translations();
    return result;
}



