/*
 * Copyright (c) 2011 Sell Stage. All Right Reserved, http://sellstage.com
 * This source is subject to the Sell Stage terms of use.
 * All other rights reserved.
 */
(function(){

   var rv = -1; 
   if (navigator.appName == 'Microsoft Internet Explorer'){
      var ua = navigator.userAgent;
      var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
      if (re.exec(ua) != null){
         rv = parseFloat( RegExp.$1 );
      }
   }
   if (rv > 0){
      if (rv <=6){
         //we dont support IE before v7 yet..
         return;
      }
      else if (rv <=8 && ua.indexOf('Trident/5') == -1){
         //actually in IE7 or IE8..
         document.attachEvent("onreadystatechange", function(){
            if ( document.readyState === "complete" ) {
               document.detachEvent( "onreadystatechange", arguments.callee );
               loadScript();
            }
         });
      }
      else{
         loadScript();
      }
   }
   else{
      loadScript();
   }

   function loadScript(){
   
      var page_location;
      if (window.location.href.indexOf('sellstage.com/demo') > -1 || window.location.href.indexOf('localhost:8080/demo') > -1){ //in demo mode
         page_location = document.createElement('a');
         page_location.href = window.location.href.split('site=')[1];
      }
      else { //not in demo mode
         page_location = window.location;   
      }
      
      var merchant_script_host = "http://www.sellstage.com/"; //by default
      if (window.location.href.indexOf('sell_stage_enviro=') != -1) {
         sell_stage_enviro = window.location.href.split('sell_stage_enviro=')[1].split('.')[0].split('&')[0];
         if (sell_stage_enviro == 'local') { 
            merchant_script_host = "http://localhost:8080/";
         } else if (sell_stage_enviro == 'new'){
            merchant_script_host = "http://sellstage.appspot.com/";
         } else {
            merchant_script_host = "http://" + sell_stage_enviro + ".sellstage.com/";
         }
      }
      
      var all_scripts = document.getElementsByTagName('script');
      var add_sell_stage_script_source = '';
      
      for (var i=all_scripts.length-1; i >=0; i--){
         
         if (all_scripts[i].src.indexOf('/st/add_sell_stage.js') != -1){
         
            add_sell_stage_script_source = all_scripts[i].src;
            break;
            
         }
      }
         
      var script   = document.createElement("script");
      script.type  = "text/javascript";
      script.src   = merchant_script_host + 'merchant_script?host_name=' + page_location.hostname;
      
      if (window.location.href.indexOf('sell_stage_approval=true') != -1) {
         script.src += ('&sell_stage_approval=true');
      }
      
      if(add_sell_stage_script_source.indexOf('no_cache=') != -1){
         script.src += ('&no_cache='+ Math.floor(Math.random()*100000000));
      }
      
      document.getElementsByTagName("head")[0].appendChild(script);
   }
})()

