
var GuruSite=new Class();


GuruSite.prototype.construct=function(gf_site_config)
{     
    if(gf_site_config==null)
    {
        var gf_site_config={
            path:"operations/",
            alert: function($data)
            {
                alert($data);
            },
            date: function(target)
            {
					 
            }
        }
    }
 
    this.path=gf_site_config.path;
	   
    this.alert_function=gf_site_config.alert;
	   
    this.areas={
        login: "rq_login.php",
        register: "rq_register.php",
        contact: "rq_contact.php",
        shop: "rq_shop.php",
        message: "rq_message.php",
        reset: "rq_reset.php"
    }
	   
    this.current_container="";
	   
    this.recaptcha=0;
    this.recaptcha_container="";
    this.recaptcha_theme="";
	 
    this.events="";
}
	 

//this method executes an alert/print message operation.
//The message is sent to a specific method...that implements the printout.(a foreign fw can be used)
GuruSite.prototype.alert=function($text)
{
    this.alert_function($text);
}

//this method implements a ajax driven dependeciy between two DOM areas.
//the target area is dependent to the trigger value wich is processed in the "processor"
// which is a custom script that does something an returns the message you want (in gf fw type)
// the success responce is dumped in the target area
GuruSite.prototype.dependent=function(trigger,target,processor)
{
    if(trigger==null || target==null || processor==null)
    {
        this.alert("There was a problem while configuring the dependecy");
        return false;
    }
			
    var $trigger_value=$("#"+trigger).val();
    var params="";
			
    if(processor.indexOf("?")!=-1){
        processor=processor.split("?");
        params=processor[1];
        processor=processor[0];
    }
				  
    this.current_container=target;
				  
    $.ajax({
        type: "POST",
        url: this.path+processor,
        data: params,
        success:   this.showResponse
    });
			
}
/*login transaction */
GuruSite.prototype.call=function ($call,$container,$trigger,extra,events)
{  
    var $area="";
    var $alt_trigger="";
	
    switch($call)
    {
        case "login" :
            $area=this.areas.login;
            break;
        case "register" :
            $area=this.areas.register;
            break;
        case "contact" :
            $area=this.areas.contact;
            break;
        case "shop" :
            $area=this.areas.shop;
            break;
        case "message" :
            $area=this.areas.message;
            break;
        case "reset" :
            $area=this.areas.reset;
            break;
    }
	
    if($area=="") {
        this.alert("No call path selected");
        return false;
    }
			  
    if(document.getElementById($container)==null) {
        this.alert("No call container selected");
        return false;
    }
	
    this.current_container=$container;
	
    this.events=events;
	
    if($trigger.indexOf("|")!=-1){
        $trigger=$trigger.split("|");
        $alt_trigger= $trigger[1];
        $trigger= $trigger[0];
    }
	   
    this.trigger=$trigger;

	
    if($trigger!=""){
        $("#"+$trigger).hide();
	 
        if($alt_trigger!="")
            $("<p id='alt_trigger'>"+$alt_trigger+"</p>").insertAfter("#"+$trigger);
    }
	
    $.ajax({
        type: "POST",
        url: this.path+$area,
        data: gf.findNodesUri($container)+extra,
        success:   this.showResponse
    });
	 
}
	 
/* handle the ajax responces */	 
GuruSite.prototype.showResponse=function ($responce)
{  
	$responce = $responce.replace(">","");
    $responce=$responce.split("(:)");
		
    //get custom events
    if(gfs.events!=null && gfs.events!="")
    {
        var  success_event=gfs.events.success;
        var  fail_event=gfs.events.fail;
			   
        if($responce[0].trim()=="SUCCESS")
        {
            if(typeof success_event=="function") success_event($responce[2]);
        }
        else
        {
            if(typeof fail_event=="function") fail_event($responce[2]);
        }
        $("#"+gfs.trigger).show();
        $("#alt_trigger").remove();
        return;
    }
		
    switch($responce[1].trim())
    {
				 
        case "LOGIN":
            if($responce[0].trim()=="SUCCESS")
            {
                document.location.href=$responce[2];
            }
            else
            {
                gfs.alert($responce[2]);
            }
            break;
        case "RESET":
            if($responce[0].trim()=="SUCCESS")
            {
                gfs.alert($responce[2]);
            }
            else
            {
                gfs.alert($responce[2]);
            }
            break;
									
        case "MESSAGE":
            if($responce[0].trim()=="SUCCESS")
            {
                $("#"+gfs.current_container).hide("fast");
                $("#"+gfs.current_container).html($responce[2]);
                $("#"+gfs.current_container).show("slow");

            }
            else
            {
                gfs.alert($responce[2]);
            }
            break;
									
        case "SHOP":
            if($responce[0].trim()=="SUCCESS")
            {
										
                document.location.href=$responce[2];
            }
            else
            {
                gfs.alert($responce[2]);
            }
            break;
        case "CONTACT":
            if($responce[0].trim()=="SUCCESS")
            {
                $("#"+gfs.current_container).hide("fast");
                $("#"+gfs.current_container).html("Your message has been sent succsessfully. Thank you.");
                $("#"+gfs.current_container).show("slow");
            }
            else
            {
                gfs.alert($responce[2]);
            }
            break;
        case "REGISTER":
            if($responce[0].trim()=="SUCCESS")
            {
                if($responce[2].indexOf("http://")!=-1 || $responce[2].indexOf("https://")!=-1)
                {
                    document.location.href=$responce[2];
                }
                else
                {
                    $("#"+gfs.current_container).hide("fast");
                    $("#"+gfs.current_container).html("A confirmation email has been sent to your email address.");
                    $("#"+gfs.current_container).show("slow");
                }
            }
            else
            {
                gfs.alert($responce[2]);
											
                if(gfs.recaptcha==1) gfs.showRecaptcha("recaptcha_container","","red");
            }
  
            break;
        case "DEPENDENT":
            if($responce[0].trim()=="SUCCESS")
            {
                switch(gf.getType(gfs.current_container))
                {
                    case "INPUT" :
                        $("#"+gfs.current_container).val($responce[2]);
                        break;
                    case "TEXTAREA" :
                        $("#"+gfs.current_container).val($responce[2]);
                        break;
                    default:
                        $("#"+gfs.current_container).html($responce[2]);
                        break;
                }
										  
            }
            else
            {
                gfs.alert($responce[2]);
            }
            break;
    }
		
    $("#"+gfs.trigger).show();
    $("#alt_trigger").remove();
}	 


/* this functions are for the re-captha Module. For this to work, we need the re-captha library.http://api.recaptcha.net/js/recaptcha_ajax.js
   this method creates the reCaptcha visual object.
   This can be called any number of times, reloading the securitu phrase
 */  
GuruSite.prototype.showRecaptcha=function (element, themeName) {
							
    this.recaptcha=1;
    this.recaptcha_container=element;
    this.recaptcha_theme=themeName;
							
    Recaptcha.create("6LeZ07oSAAAAAB2FUKWzBuhS1ZTbj7fzM8rQ14ok", element, {
        theme: themeName,
        tabindex: 0,
        callback: Recaptcha.focus_response_field
    });

}
									
/* this functions are for the re-captha Module. For this to work, we need the re-captha library
   this method creates destroys reCaptcha visual object.

 */ 									
GuruSite.prototype.destroyRecaptchaWidget=function () {
    Recaptcha.destroy();
}

//instantiate the ddc object
var gfs=new GuruSite(gf_site_config); 
