﻿if(typeof parkside == "undefined") var parkside = new Object();

// Constructor
parkside.paging = function(p_cfc_component, p_settings, p_num_items) {
	this.cfc_component = p_cfc_component;
	this.settings = p_settings;
	this.num_items = p_num_items;
	
	var that = this;
	this.listener = function(ev) { that.handleDeepLinkChange(ev); };

	SWFAddress.addEventListener( SWFAddressEvent.CHANGE, this.listener );
}

/// Static variables
//parkside.paging.first_run = true;

/// Static methods
// parkside.paging.method = function() {}

/// Class members
parkside.paging.prototype = {
	/// class variables
	cfc_component: ""
	, num_items: 5
	, last_link: null
	, settings: null
	, last_page: 0
	, init : false

	/// class methods
	
	/// sets deep link
	, loadPage: function( page, force, callback) { 
		if(force === undefined)
			force = false;
		/// force reload when clicked or called
		this.last_page = 0;
						
		/// set deep link
		if (SWFAddress && SWFAddress.setValue && !force) {
			SWFAddress.setValue( "" + page ); // this throws SWFAddressEvent.CHANGE
		} else {
			if(force)
				SWFAddress.setValue( "" + page );
			this.loadPageRequest ('', page, callback);
		}
	}
	/// handles changes in deep link
	, handleDeepLinkChange: function (event) { 
		var path_exploded = event.path.split("/");
		if(event.path.indexOf("/") > -1 ) {  
			if ( path_exploded[path_exploded.length-1] != "" && !isNaN( path_exploded[path_exploded.length-1] ) ) {
				this.loadPageRequest (path_exploded[0], Math.max(1, path_exploded[path_exploded.length-1]));
			}
		} else {
			this.loadPageRequest (path_exploded[0], 1);
		}
		
/*		if (event.path.length > 1 && !isNaN(event.path.replace("/", "") && this.last_page != event.path.replace("/", "")) ){
			this.loadPageRequest ( event.path.replace("/", "") );
		} else if (event.path == "/" &&  this.last_page != 1 ) {
			this.loadPageRequest ( 1 );
		}*/
	}
	/// performs actual request
	, loadPageRequest: function(anchor, page, callback) {
		this.last_link = $(".item_list_paging .active");
		
		var params = new Object(),
			$item_list_wrapper = $("." + this.cfc_component + "_item_list_wrapper"),
			$item_list_content = $item_list_wrapper.find(".item_list_content");
		params.cfc_component_name = this.cfc_component;
		params.current_page = page;
		params.num_items =  this.num_items;
		params.settings = $.toJSON(this.settings);

		/// transfer query string to the cfc
		var qa = window.location.search.substring(1).split("&");
		for (i=0;i<qa.length;i++) {
			kv = qa[i].split("=");
			params[kv[0]] = kv[1];
		}
		
		this.last_page = page;
		
		anchor = "#" + anchor;
		if(anchor == "#")
			anchor = "";

		this.anchor = anchor;

		/// stupid scope problems 
		var that = this;
	
		this.settings.query_string = this.settings.query_string.replace(/&amp;/gi, "&");
				
		if(this.settings.callback === "undefined")
			this.settings.callback = {};
			
		if(this.settings.callback.before === "undefined")
			this.settings.callback.before = null;
				
		if(this.settings.callback.after === "undefined")
			this.settings.callback.after = null;
		
		if(typeof this.settings.callback.before === "function") this.settings.callback.before();
		$item_list_content.after('<div class="' + this.cfc_component + '_item_list_loading' + '"></div>');
		$item_list_content.wrapInner("<div></div>").children().css({opacity : 0, width : "100%"}).show();
		$item_list_wrapper.css({height : $item_list_wrapper.find(".item_list").outerHeight(true)});
		$(window).trigger("resize");
		$.post("custom/modules/list/paging.cfc?method=loadPage&javascript=true&" + this.settings.query_string, params, function(cfc_result, status) {if(typeof that.settings.callback.after === "function") that.settings.callback.after(); that.loadPageResponse(cfc_result); if(typeof callback === "function") callback(); $(window).trigger("ajaxLoad", $item_list_wrapper); }, "json");
		
	}
	/// process the response
	, loadPageResponse: function ( cfc_result ) {
		var component = this.cfc_component.split(".");
		component = component[component.length - 1];
		//SWFAddress.removeEventListener( SWFAddressEvent.CHANGE, this.listener );
		if (/*cfc_result.HTML.length != 0 && this.last_link != null*/true) { 
			var $item_list_wrapper = $("." + component + "_item_list_wrapper");
			$item_list_wrapper.html(cfc_result.HTML );
			
			var $item_list_content = $item_list_wrapper.find(".item_list_content");
			if(!this.init)
			{
				$item_list_content.wrapInner("<div></div>").children().css({opacity : 0, width : "100%"}).show().animate({opacity : 1}, 500, function() { $(window).trigger("resize"); });
				$item_list_wrapper.animate({height : $item_list_wrapper.find(".item_list").outerHeight(true)}, 500, function() { $item_list_wrapper.height(""); $(window).trigger("resize"); }); 
			}
			$item_list_content.removeClass(this.cfc_component + "_item_list_loading");
		}
		
		if(this.init)
			this.init = false;

		if(this.anchor == "")
		{ 
			//$(document).scrollTop(($item_list_wrapper.offset().top));
		}
		else
		{
			var $anchor = $(this.anchor); 
			if($anchor.length)
			{
				$(document).scrollTop($anchor.offset().top);
				$anchor.addClass("anchor");
			}
		}
	}
}
