﻿(function ($) {
gbbhInitialize();
jQuery.fn.exists = function(){return jQuery(this).length>0;}

		   var restfulApp = Backbone.Controller.extend({
			   restfulUrl: "http://api.openkeyval.org/", //This is a free service to store key pair values
				
			   //Routes tell the app what to do 
			   routes: {
				 //":controller/:view/:id":     "defaultRoute",
			   "*page":                    "defaultAction" //This simply matches any urls that weren't caught above and assigns it to "page"
			   },
				
			   defaultAction: function( pages ){
			   if(pages){
					var page = pages.split("#");
				   for(var i = 0; i < page.length; i++){
							//page = page.toLowerCase();
							var params = page[i].split("/");
							var controller = params[0] != null ? params[0] : "";
							var action = params[1] != null ? params[1] : "";
							var id = params[2] != null ? params[2] : "";                    
							var qs = params[3] != null ? params[3] : (page[i].split("?")[1] != null ? page[i].split("?")[1] : "");

							var el = "#"+controller.toLowerCase()+"Container";

							if(!$(el).exists())
								window.location = pages;
							if(qs != "" && qs.indexOf("scrollTo=") > -1)
								$.scrollTo("#" + qs.split("scrollTo=")[1].split("&")[0], 800);

							loadRestfulData(page[i], el);

//							window.setTimeout(function () {
//							try {
//                                clicky.log(window.location);   
//                                console.log("Logging:" + window.location); 
//                            }
//                            catch(err){
//                                clicky.log("/#" + pages);   
//                                console.log("Logging:" + "/#" + pages);  
//                                }                    
//                            }, 500);
                            
                                       
                                            
						}
					}
			   },               
			   defaultRoute: function( controller, view, id ){
					var url = controller != null ? "/" + controller + (view != null ? "/" + view + (id != null ? "/" + id : "") : "") : "";
			   }
 
		});
 
		   var app = new restfulApp;
		   //Initiate a new history and controller class
		   Backbone.emulateHTTP = true;
		   Backbone.emulateJSON = true
		   Backbone.history.start();
        
	   })(jQuery);


	   function loadRestfulData( page, contentPane ){
			   
			   jQContentPane = $(contentPane);
			   
			   if(!$(contentPane +" div:first").exists())
					jQContentPane.append("<div></div>");


				   $(contentPane +" div:first").fadeOut("fast", function(){ 
							jQContentPane.html("<h2 style='text-align:center; color:#669; display:none'><br><br>loading...</h2>").find("h2:first").fadeIn("fast");
							$.ajax({
									url: page,
									dataType: "html",
									success: function(data){
										//Once we receive the data, set it to the content pane.
										jQContentPane.find("h2:first").fadeOut("fast", function(){
											var tmp = $("<div class='hide'>"+data+"</div>");                                    
											jQContentPane.html(tmp);                                            
											jQContentPane.find("div[class='hide']:first").fadeIn(2000); 
											
											$.scrollTo(contentPane, 800);
											gbbhInitialize();                             
										});
									},
									error: function(r, status, errorMsg){                                        
										jQContentPane.html("<h2 style='text-align:center; color:#669;'><br><br>We're sorry, but an error has occured. Server returned the following response: "+ errorMsg +"</h2>")
									}
								}); 
						}); 
				
			  }
