/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

jQuery(document).ready(function($) {
    
    var loginresult = false;

    //When you click on a link with class of poplight and the href starts with a # 
    $('a.poplight[href^=#]').click(function() {
        var popID = $(this).attr('rel'); //Get Popup Name
        var popURL = $(this).attr('href'); //Get Popup href to define size

        //Pull Query & Variables from href URL
        var query= popURL.split('?');
        var dim= query[1].split('&');
        var popWidth = dim[0].split('=')[1]; //Gets the first query string value

        //Fade in the Popup and add close button
        $('#' + popID).fadeIn().css({
            'width': Number( popWidth )
        });
        //.prepend('<a href="#" class="close"><img src="close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');

        //Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and border width defined in the css
        var popMargTop = ($('#' + popID).height() + 80) / 2;
        var popMargLeft = ($('#' + popID).width() + 80) / 2;

        //Apply Margin to Popup
        $('#' + popID).css({
            'margin-top' : -popMargTop,
            'margin-left' : -popMargLeft
        });

        //Fade in Background
        $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
        $('#fade').css({
            'filter' : 'alpha(opacity=80)'
        }).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies 

        return false;
    });

    //Close Popups and Fade Layer
    $('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
        $('#fade , .popup_block').fadeOut(function() {
            //  $('#fade, a.close').remove();  //fade them both out
            });
        return false;
    });


    

    

    $('#register_form').validationEngine();
 
    var remember = false;
    $("#signin_ffffffform").click(function(event){
        //$.post($(this).attr('/login/'), $(this).serialize(), function (data) {
        sendRequest();
        // alert(loginresult);
        if (loginresult == 'true') {
            //   window.location.href = '';
            //  alert(loginresult);
            $("#signin_form").submit();

            return true;
        }else{
            event.preventDefault();
            //   alert('error');
            return false;
        }
  
    });

    $("#signin_fffffform").submit(function(){
    
            
        return true;
    });
    $("#signin_form").submit(function(event){
          return sendRequest();  
      //  var answer = sendRequest();
        //	var answer = confirm("Login?")
                var answer = returnAnswer();
        if (answer){
            alert(answer);
            //  $(this).attr('action','/updates/');
            return true;
        }
        else{
            event.preventDefault();
        }

            
    });         
   
   function returnAnswer(){
       return sendRequest();
   }
    function sendRequest(){
        $.ajax({
            url: '/login/',
            type: 'POST',
            dataType: 'json',
            data: {
                'password': $(".pwd").val(),
                'username': $(".uname").val(), 
                'user_login': 'login', 
                'remember_me' : 'remember'
            },

            beforeSend: function(  ) {
                $("#status").html('');
                $("#status").html(login.authorising).fadeIn("slow");
            // return false;
            },
            success: function( data ) {
                
               

                // alert(data);
                // console.log( 'Sample of result:', data );
                if(data == 1){
                    //   alert(data);
                    pageTracker._trackPageview('/userLogin');
                     $("#status").html(login.success).fadeIn("slow");
                    window.location.href = '/updates/';
                    return true;
                }else{
                    $("#status").html(login.mismatch).fadeIn("slow");
                    return false;

                }
            }
           
        });
        
    }


});

