/*********************************************************************************
 * CIPC project javascript
 *
 * @author  websupport@modulis.ca
 * @crdate  2009-08
 */

$(document).ready( function(){
    /**
     *  -- login form -- {{{
     **/
    $("#cipc_login").click( function(){
      var dialog = {
        bgiframe: true,
			  autoOpen: false,
        title: $("#cipc_loginform_title").text(),
			  height: 250,
        width:380,
        draggable:true,
        resizable:false,
			  modal: true
      };
      $("#cipc_loginform").dialog(dialog);
      $("#cipc_loginform").dialog('open');
      $("#cipc_loginform_loading,#cipc_loginform_error").hide();
      // reset dialog height
      $("#cipc_loginform").dialog('option', 'height', dialog.height);
      $("#cipc_loginform").height(dialog.height);
      // cancel button
      $("#cipc_loginform button.cancel").click(function(){
        $("#cipc_loginform").dialog('close');
      });
      // login button
      $("#cipc_loginform button.submit").click(function(){
        // reset dialog height
        $("#cipc_loginform").dialog('option', 'height', dialog.height);
        $("#cipc_loginform").height(dialog.height);

        $("#cipc_loginform_loading,#cipc_loginform_error").hide();

        var loading_h = 40;
        var loginfail_h = $("#cipc_loginform_error").height() + 25;
        // post info to ajax server
        var fields = $("#cipc_loginform :input").serializeArray();
        // disable buttons
        $("#cipc_loginform button.submit, #cipc_loginform button.cancel").attr({"disabled":"disabled"});
        // show loading message
        $("#cipc_loginform_loading").show();
        $("#cipc_loginform").dialog('option', 'height', dialog.height + loading_h);
        $("#cipc_loginform").height(dialog.height + loading_h);
        
        // send request 
        $.post("/members.ajax",fields,
          function(data,ajaxStatus){
              $("#cipc_loginform_loading").hide();
             // successful
             if( data.CODE == "SUCCESS" ){
              //reload page
                $("#cipc_loginform_loading").html(data.message);
                $("#cipc_loginform_loading").addClass("cipc-success");
                $("#cipc_loginform_loading").show('blind');
                //window.location = window.location.href;
		
		var url = location.href;
		var baseURL = url.substring(0, url.indexOf('/', 14));

		window.location = baseURL+'/?id=244&L='+lang;
		
             }else{ // failed login
                // set dialog height
                $("#cipc_loginform").dialog('option', 'height', dialog.height + loginfail_h);
                $("#cipc_loginform").height(dialog.height + loginfail_h);
                // enable buttons
                $("#cipc_loginform button.submit, #cipc_loginform button.cancel").removeAttr("disabled");
                // show message
                $("#cipc_loginform_error").show();
             }
          },
          "json"
        );
      });

      return false;
    }); //}}}
    /**
     * -- logout form -- {{{
     **/
    $("#cipc_logout").click( function(){
      var dialog = {
        bgiframe: true,
			  autoOpen: false,
        title: $("#cipc_logoutform_title").text(),
			  height: 150,
        draggable:true,
        resizable:false,
			  modal: true
      };

      $("#cipc_logoutform").dialog(dialog);
      $("#cipc_logoutform").dialog('open');
      // cancel button
      $("#cipc_logoutform button.cancel").click(function(){
        $("#cipc_logoutform").dialog('close');
      });
      // login button
      $("#cipc_logoutform button.submit").click(function(){
        // reset dialog height
        $("#cipc_logoutform").dialog('option', 'height', dialog.height);
        $("#cipc_logoutform").height(dialog.height);
        var loading_h = 20;
        // show loading message
        $("#cipc_logoutform_loading").show();
        $("#cipc_logoutform").dialog('option', 'height', dialog.height + loading_h);
        $("#cipc_logoutform").height(dialog.height + loading_h);
        
        // post info to ajax server
        var fields = $("#cipc_logoutform :input").serializeArray();
        // send request 
        $.post("/members.ajax",fields,
          function(data,ajaxStatus){
             if( data.CODE == "SUCCESS" ){
                window.location = window.location.href;
             }else{
                alert("ERROR: Ajax server response error.");
             }
          },
          "json"
        );
        
      });

      return false;
    }); //}}}
});


