buildPage = function (caller)
{

temp = {};

temp.linkId = $(caller).closest('ul').attr('class');	
temp.info = $(caller).attr('title'),
temp.action = $(caller).attr('rel'),
temp.name = $(caller).text();
	
temp.construct = function()
{
	$('.stock ul li a').addClass('sort');
	$(caller).removeClass('sort');
	$('#header').append('<div class="load"><img src="' + $baseUrl + 'utility/ajax-loader.gif" /></div>');
	$('.inner-scroll').children().remove();
	
	$('#stores h2').remove();//change back to #stores
	$('.navi').hide();
	$('#stores').css({'background-color':'transparent', 'opacity': 0});
	
	if (temp.linkId !== 'state_list'){
		$('.first-navi').hide();
		$('#two-level h2').remove();
		$('ul#stock-nav li a').siblings().remove();
		$('.section').css({'background-color':'transparent', 'opacity': 0});
		$('.empty').children().remove();
		$(caller).parent().append('<img src="' + $baseUrl + 'utility/barre_activite_plein.jpg"/>');

	}

	
}

temp.levelTwo = function(data)
{	
	
		if(temp.linkId == 'state_list'){
			
				var selector = $('#stores'),
					title = "<h2>Countries</h2>";
		}
		
		if(temp.linkId !== 'state_list'){

				var selector = $('#one-level'),
					title = "<h2>Countries</h2>";
		}
		
			
			selector.css({'background-color': 'white'});
		
			selector.animate({'opacity': 0.9});
						
			selector.prepend(title);
			
			if (data.length > 13) {
				
				$('.navi').show();
					
				
			}
	
			$.each(data, function(index){
		
			if (temp.linkId == 'state_list'){
				
		
			if(index <= 12){

			var link = "<li class='first'>" + this.post_title + "</li>";
			
			}
		
			if (index > 12 ){
				
		
		
				var link = "<li class='next'>" + this.post_title + "</li>";
				
			}
			
				if(this == '[object Object]'){
				$('.inner-scroll').append(link);
 			}
			
			}
			
			if (temp.linkId !== 'state_list') {
				
				
				var link = "<li><a href='http://" + this.meta_value + "' target='_blank'>" + this.post_title + "</a></li>";

					if(this == '[object Object]'){
					selector.append(link);
				}
				
				
			}
			
			});
			
			
			
			if (temp.linkId == 'state_list'){
				
					
							$(function() {

							$('.scrollable').livequery(function(){

							$(this).scrollable();

							});

						});
				
					selector.find('li.first').wrapAll('<ul id="store-list_1" class="store_list">');
					selector.find('li.next').wrapAll('<ul id="store-list_2" class="store_list">');	
			}
			
			if (temp.linkId !== 'state_list'){
				
					selector.find('li').wrapAll('<ul>');
				
			}
			
}


temp.levelOne = function(data)
{
	
	
			
	
			var selector = $('#two-level');
			
			var title = "<h2 class='" + temp.name + "'>" + temp.name + "</h2>";
		
			selector.css({'background-color': 'white'}).animate({'opacity': 0.9});
			
							
							if (data.length > 20) {

								$('.first-navi').show();


							}
			
							$.each(data, function(index) {
								
								
								if (index <= 19 ) {


								var link = "<li class='state-first'><a class='sort' rel='one-level' title='" + this.term_id + "' name='" + this.name +"'>" + this.name + "</a></li>";

								if(this == '[object Object]'){

										$('.first-inner-scroll').append(link);
								}
								
								}
								
								if (index > 19 ) {


								var link = "<li class='state-next'><a class='sort' rel='one-level' title='" + this.term_id + "' name='" + this.name +"'>" + this.name + "</a></li>";

								if(this == '[object Object]'){

										$('.first-inner-scroll').append(link);
								}
								
								}

							});
							
								$(function() {

								$('.first-scrollable').livequery(function(){

								$(this).scrollable({
									
									next: '.first-next',
									prev: '.first-prev'
									
								});

								});

							});
							
							selector.find('li.state-first').wrapAll('<ul id="state-list_1" class="state_list">');
							selector.find('li.state-next').wrapAll('<ul id="state-list_2" class="state_list">');

							//selector.find('li').wrapAll('<ul id="state">');

							selector.prepend(title);
				
		
}

temp.shoot = function(fn){

setTimeout(function(){
$('.load').hide();	
	fn;
}, 600);		

};

temp.init = function()
{
		
		temp.construct();
	
		var passed = 'param=' + temp.info;

		$.ajax({
		  url: '../model/carpet.php?action=' + temp.action,
		  dataType: 'json',
		  data: passed,
		  success: function(data){
			
				if (temp.action == 'one-level'){
					
					temp.shoot(temp.levelTwo(data));
				}
			
				if (temp.action == 'two-level'){
					
					temp.shoot(temp.levelOne(data));
				}
	
		}
		});
	
}

return temp;

}

$(document).ready(function(){
	
	
		$('a.sort').livequery('lastclick',function(event, clicks){
			
			var namespace = $(this).text(),
				namespace = buildPage(this); 
	
				namespace.init();
					
		});
						

});

