/*
 * Copper Conferencing Travel Widget
 * Developed with the goal to make you realize how much money you waste in travel!
 * Developed by a guy who develops things.
 *
 */

var copper_conferencing = {};

copper_conferencing.templates = {
   url_css:                'widget/css/cc_travel_widget.css',
   url_forms:              'widget/forms.php?method=',
   
   small_widget_heading:   'Travel Calculator',
   small_widget_button:    '<h3>Calculate Your Savings</h3>',
   small_widget_id:        'cc_travel_widget_small',
   
   big_widget_heading:     "Travel Calculator",
   big_widget_description: "We've made it easier then ever to calculate how much money you will save if you book your next meeting through Copper Conferencing!",
   
   big_widget_disclaimer: "*Calculations based on current IRS mileage reimbursement rate of $.585 per mile; average airfare of $400 per round trip; average per night hotel rate of $250; and an average hourly rate of $100.  Calculations for web conferencing and phone conferencing are based on current competitive rates.  Actual calculations and savings may vary.",
   travel_methods:         ['Select an Option','Plane','Vehicle'],
   iframe_heights:         {Plane:'400px',Vehicle:'500px'},
   method_question:        'How are you planning on traveling: '
};

copper_conferencing.populate_template = function(text, arr) {
   for(var prop in arr) {
      text = text.replace('{{'+prop+'}}',arr[prop]);
   }
   return text;
}

copper_conferencing.init = function() {
    var t          = copper_conferencing.templates;

    document.write('<link rel="stylesheet" type="text/css" href="'+ t.url_css +'" /> ');
    document.write('<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.5.2/build/container/assets/skins/sam/container.css">');
  // document.write('<script type="text/javascript" src="http://yui.yahooapis.com/combo?2.5.2/build/yahoo-dom-event/yahoo-dom-event.js&2.5.2/build/animation/animation-min.js&2.5.2/build/container/container-min.js&2.5.2/build/selector/selector-beta-min.js"></script>');
    document.write('<script type="text/javascript" src="http://yui.yahooapis.com/combo?2.6.0/build/yahoo-dom-event/yahoo-dom-event.js&2.6.0/build/animation/animation-min.js&2.6.0/build/connection/connection-min.js&2.6.0/build/container/container-min.js&2.6.0/build/selector/selector-beta-min.js"></script>');
    
}

copper_conferencing.Travel_widget = function(container_id) {
	
    this.small_widget_container = YAHOO.util.Dom.get(container_id);
    this.big_widget = false;
    
    this.init = function() {
        var t          = copper_conferencing.templates;
        var populate   = copper_conferencing.populate_template;
        
        /* SMALL WIDGET */
        //make sure tyles are set properly
        YAHOO.util.Dom.addClass(this.small_widget_container,'cc_small_widget');
        
        //insert some required elements
       // this.insert_element(this.small_widget_container, 'h1', t.small_widget_heading);
        var btn_div = this.insert_element(this.small_widget_container,'div',t.small_widget_button);
        YAHOO.util.Dom.addClass(btn_div,'cc_button_small');
        
        //event listener yo
        YAHOO.util.Event.addListener(btn_div,'click',function(){
           this.show_big_widget();
        },this,true);
        
        this.big_widget_container = this.insert_element(document.body,'div','');
        YAHOO.util.Dom.addClass(this.big_widget_container,'yui-skin-sam');
    }
   
   this.show_big_widget = function() {
      if(!copper_conferencing.big_widget) {
            copper_conferencing.big_widget = new YAHOO.widget.Panel('cc_big_widget',{
               modal:          true,
               fixedCenter:    false,
               width:          '498px',
               draggable:      true,
               iframe:	        true,
               strings:          { close: ''},
               effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.25}
            });
		 
         
            var t = copper_conferencing.templates;
            copper_conferencing.big_widget.setHeader('');
            
            //drop in body header
            var container = this.insert_element(false,'div','');
            container.id	= 'cc_big_widget_container';
            YAHOO.util.Dom.addClass(container,'cc_body_container');
            
            var hd = this.insert_element(container,'div','');
            YAHOO.util.Dom.addClass(hd,'cc_header');
            
            var heading = this.insert_element(hd,'div',t.big_widget_heading);
            YAHOO.util.Dom.addClass(heading,'cc_heading');
            
            //add description
            YAHOO.util.Dom.addClass(this.insert_element(container,'div',t.big_widget_description),'cc_description');
            
            //add dropdown
            var method_div       = this.insert_element(container, 'div',t.method_question);
            YAHOO.util.Dom.addClass(method_div,'cc_method');
            copper_conferencing.method_dropdown  = this.insert_element(method_div, 'select','');
           
            //add methods to dropdown
            for(var c=0;c<t.travel_methods.length;c++) {
               var option     = this.insert_element(copper_conferencing.method_dropdown,'option',t.travel_methods[c]);
               option.value   = t.travel_methods[c].toLowerCase();
            }
            
            //event handler for when they select a method
            YAHOO.util.Event.addListener(copper_conferencing.method_dropdown,'change',this.load_form,this,true);
           
            //add container iframe
            var iframe = this.insert_element(container,'iframe','');
            iframe.setAttribute('frameBorder','0');
            iframe.setAttribute('border','0');
            iframe.setAttribute('hspace','0');
            iframe.setAttribute('vspace','0');
            
            iframe.setAttribute('allowTransparency','true');
            
            YAHOO.util.Dom.setStyle(iframe,'border','none');
            YAHOO.util.Dom.setStyle(iframe,'visibility','hidden');
                      
             //listen for when it's done lading so we can resize it
             YAHOO.util.Event.on(iframe,'load',function(e) {

                 var iframe = YAHOO.util.Event.getTarget(e);
                 
                 copper_conferencing.method_dropdown.blur();
                 
                 if(iframe.src.length > 0) {
                    YAHOO.util.Dom.setStyle(iframe,'visibility','');
                 } else {
                    YAHOO.util.Dom.setStyle(iframe,'visibility','hidden');
                 }
                 
                 var content= iframe.contentWindow || iframe;
                 var height = (content.body_height) ? content.body_height() : 25;
                 
                 
                 YAHOO.util.Dom.setStyle(iframe,'height',height+'px');
                 
                 setTimeout(function() {
                    var height = (content.body_height) ? content.body_height() : 10;
                    YAHOO.util.Dom.setStyle(iframe,'height',height+'px');
                },250);
                 
                 copper_conferencing.big_widget.center();
                 copper_conferencing.big_widget.sizeMask();
             }, this, true);
                     
             copper_conferencing.big_widget.my_iframe = iframe;
           
            //container for services menu
            var services_container = this.insert_element(container,'div','');
            //populate it from the forms page
            YAHOO.util.Connect.asyncRequest('GET',t.url_forms+'services',{
                success :function(r) {
                    services_container.innerHTML = r.responseText;
                }
            });
            
             var ft = this.insert_element(container,'div','');
             YAHOO.util.Dom.addClass(ft,'cc_footer');
             
             //lets setup the container that holds the price
             copper_conferencing.big_widget.total_container = this.insert_element(ft,'div','$0.00');
             YAHOO.util.Dom.addClass(copper_conferencing.big_widget.total_container,'cc_total');
             
             //insert the disclaimer to show at the bottom of everything
             var disclaimer = this.insert_element(container,'div',t.big_widget_disclaimer);
             YAHOO.util.Dom.addClass(disclaimer,'cc_disclaimer');
             
             copper_conferencing.big_widget.setBody(container);
             copper_conferencing.big_widget.setFooter('');
             
             copper_conferencing.big_widget.render(this.big_widget_container);
        }
              
    copper_conferencing.big_widget.show();
    copper_conferencing.big_widget.center();

    setTimeout(function() {
        copper_conferencing.big_widget.center();
    },200);

   }
   
   this.load_form = function() {
        
      YAHOO.util.Dom.setStyle(copper_conferencing.big_widget.iframe,'display','block');

      var method    = copper_conferencing.method_dropdown.value;
      var t         = copper_conferencing.templates;
      var url       = t.url_forms + method;
      
      copper_conferencing.big_widget.my_iframe.src = url;
   }
   
   this.insert_element = function(container, type, contents) {
      var el = document.createElement(type);
	  //alert('insert '+type+' into ' + container.id);
	  if(contents) {
      	el.innerHTML = contents;
	  }
      if(container) {
         container.appendChild(el);
      }
      return el;
   }
   
   this.init();
}

copper_conferencing.init();
copper_conferencing.refresh_total = function() {
    copper_conferencing.set_total(copper_conferencing.last_total,copper_conferencing.attendees,copper_conferencing.meeting_duration);
}

copper_conferencing.last_total          = 0;
copper_conferencing.attendees           = 0;
copper_conferencing.meeting_duration    = 0;

copper_conferencing.set_total = function(total, attendees, meeting_duration) {

    copper_conferencing.last_total          = total;
    copper_conferencing.attendees           = attendees;
    copper_conferencing.meeting_duration    = meeting_duration;

    var d = YAHOO.util.Dom;
    copper_conferencing.big_widget.total_container.innerHTML = '...';
    
    setTimeout(function() {
        //find cost of copper stuff
        var cc_cost =  0
        if(d.get('cc_widget_both').checked) {
            cc_cost = d.get('cc_widget_both').value;
        } else if(d.get('cc_widget_web').checked) {
            cc_cost = d.get('cc_widget_web').value;
        } else if(d.get('cc_widget_audio').checked) {
            cc_cost = d.get('cc_widget_audio').value;
        }
        
        //console.log(cc_cost);
        //calc copper cost by multiply by attendees and duration
        cc_cost = (cc_cost * 60) * attendees * meeting_duration;
    
        //format currencty
        var c = copper_conferencing.formatCurrency;
        
        //populate cost areas
        d.get('cc_host_cost_container').innerHTML = c(total);
        d.get('cc_copper_cost_container').innerHTML = c(cc_cost);
    
        var savings = total - cc_cost;
        if(savings < 0) {
            savings = 0;
        }
        copper_conferencing.big_widget.total_container.innerHTML = c(savings);
    },250);
}

copper_conferencing.formatCurrency = function(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}
