if(Scraplr == undefined){
  var Scraplr = {};
} 

Scraplr.LoginObj ={
  postLoginData: function (){
    var strUserIdValue = $('login_username').value;
    var strUserPasswordValue = $('login_password').value;
    
    if((strUserIdValue != '')&&(strUserPasswordValue != '')){
      try{
        $('login_username').removeClassName('loginInputBoxInvalid');
        $('login_password').removeClassName('loginInputBoxInvalid');
      }catch(ex){
        //
      }finally{
         $('spinnerLogin').removeClassName('hidden');
         $('formLogin').submit();
         
         
        /*var strUrl = '/signin/';
        Scraplr.Utilities.caramelizeScrap(strUrl, false, true, 'formLogin', function (){
          $('spinnerLogin').removeClassName('hidden');
        }, function (){
          var jsonObj = Scraplr.Utilities.objCaramelizedJson;
          //{"state":"caramelized","username":"owner"}
          var strRequestState = jsonObj.state;
          var strUserName = jsonObj.username ; 
          if((strRequestState=='caramelized')&&(strUserName!='')){
            Scraplr.Utilities.redirect('/'+strUserName+'/');
          }        
        });*/
      }
    }else if(strUserIdValue == ''){
      $('login_username').addClassName('loginInputBoxInvalid');
      $('login_username').focus();
    }else if((strUserIdValue != '')&&(strUserPasswordValue == '')){
      $('login_username').removeClassName('loginInputBoxInvalid');
      $('login_password').addClassName('loginInputBoxInvalid');
      $('login_password').focus();
    }else if(strUserPasswordValue == ''){
      $('login_password').addClassName('loginInputBoxInvalid');
      $('login_password').focus();
    } else if((strUserPasswordValue != '')&&(strUserIdValue == '')){
      $('login_password').removeClassName('loginInputBoxInvalid');
      $('login_username').addClassName('loginInputBoxInvalid');
      $('login_username').focus();
    }
  }
};

Scraplr.Observers = {
  domLoaded: function() {
    
    var parent = document.getElementsByTagName("html")[0];
    var strBrowser = Scraplr.UADetector.Browser.toLowerCase();  
    
    var strOS = Scraplr.UADetector.OS.toLowerCase();
    var strVersion = Scraplr.UADetector.Version;
    if((strBrowser == 'firefox')&&(strVersion =='3.5')){
       strVersion = '35';
       parent.className = strBrowser+strVersion +' '+strOS;
    }else{
      parent.className = strBrowser +' '+strOS+' '+strVersion;
    }

    if ((strBrowser == 'ie')||(strBrowser == 'opera')){
      $('browsersBox').show();
      $('loginBox').hide();
    }
    
    if (typeof window.innerWidth != 'undefined') {
      Scraplr.UI.viewportwidth = window.innerWidth;
      Scraplr.UI.viewportheight = window.innerHeight;
    }
    else if (typeof document.documentElement !== 'undefined' && typeof document.documentElement.clientWidth !== 'undefined' && document.documentElement.clientWidth !== 0) {
      Scraplr.UI.viewportwidth = document.documentElement.clientWidth;
      Scraplr.UI.viewportheight = document.documentElement.clientHeight;
    }
    else {
      Scraplr.UI.viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
      Scraplr.UI.viewportheight = document.getElementsByTagName('body')[0].clientHeight;
    }
    
    $('userDialog').setStyle('left:'+  Math.floor((Scraplr.UI.viewportwidth/2) - (440/2 )-10) + "px;");
    
    Scraplr.Utilities.disableRightClick(); 
    var strBuzzedCity = '', strBuzzedCountry = ''; 
    geofill.find(
      {
        callback:function(o){
          strBuzzedCity = o.city;
          strBuzzedCountry = o.country;
        }
      }
    );
    
    var parent = document.getElementsByTagName("html")[0];
    var strBrowser = Scraplr.UADetector.Browser.toLowerCase();
    var strOS = Scraplr.UADetector.OS.toLowerCase();
    var strVersion = Scraplr.UADetector.Version;
    if((strBrowser == 'firefox')&&(strVersion =='3.5')){
       strVersion = '35';
       parent.className = strBrowser+strVersion +' '+strOS;
    }else{
      parent.className = strBrowser +' '+strOS+' '+strVersion;
    }
    $('loginButton').observe('click',function(event){
      Scraplr.LoginObj.postLoginData();      
    // end observe click
    }); 
    $('requestCodeButton').observe('click',function(event){
      var strEmail = $('requestMail').value;
      var emailIsValid = Scraplr.Utilities.isValidEmail(strEmail);    
      if(strEmail!=''){
        try{
          $('requestMail').removeClassName('loginInputBoxInvalid'); 
        }catch(ex){
          //
        }finally{
          if(emailIsValid){      	
            var strUrl = 'http://scraplr.com/buzz.php?&strBuzzedCity='+strBuzzedCity+'&strBuzzedCountry='+strBuzzedCountry;
            new Ajax.Request(strUrl, {
              method: 'get',
              onCreate: function() {
                $('spinnerRequest').removeClassName('hidden');
              },
              onComplete: function(response) {
                $('spinnerRequest').addClassName('hidden');
                Scraplr.UI.showDialog ('alert', 'Thank you for your interest in scraplr','We will send you invite soon as we made it available',function (){
                }, function (){
                },true);
              }
            });     
          }else{
            Scraplr.UI.showDialog ('alert', 'Email address invalid','email should be in following format : name@domain.com',function (){
            }, function (){ 
              $('requestMail').focus();
            },true);
          }
        }  
      }else if(strEmail==''){	 
        $('requestMail').addClassName('loginInputBoxInvalid');
        $('requestMail').focus();
      }
    // end observe click
    });  
    
    new HotKey('enter',function(event){
      Scraplr.LoginObj.postLoginData();
  	});
       
    new Scraplr.InlineLabel('#formLogin');
    new Scraplr.InlineLabel('#formRequest');
    
    // do anything when DOM is loaded
  }
};        

document.observe('dom:loaded', Scraplr.Observers.domLoaded.bind(Scraplr.Observers)); 

