function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}


$(document).ready( function() {
	var pressed = 0;
	var contextmenu = $('#contextBox');
	var postinfoblock = $('#post-info-block-wrap');
	var postmainblock = $('#post-main-block');
	var postinfoinfo = $('#post-info-info');
	var posttitleblock = $('#post-title-block');
	var content = '';
	var infohidden = 1;
	var contextBox = $('#contextBox');
	var contextBoxWidth = contextBox.width();
	var hashsection = '';
	var embedcode = '';
	jQuery.fn.loadembed = function() {
		postmainblock.empty().html('<div class="embed">'+embedcode+'</div>');
	}
	//////////////////////////

	jQuery.fn.loadcontextmenu = function(url, menulocation, loadswitch, pagetype, post) {	
	if (pressed == 0) {
		// swapcontent(url, "notimage");
		$('#gigs').fadeOut();
		
		if (post == null) {posthash = ''} else { posthash = '&p='+post };
		if (pagetype == null) {pagetype = 'page'};		
		window.location.hash = '#!section='+url+posthash;
		hashsection = url;
		
		mainmenuitem = $(this);
		mainmenuli = mainmenuitem.parent();
		mainmenuul = mainmenuli.parent();
		mainmenuitem.mainmenuselect();
		contextmenu.hide("slide", { direction: "left" }, 300);
		var loadswitch = loadswitch;
		
		if (infohidden == 0){
			postinfoblock.hide("slide", { direction: "right" }, 300);
			infohidden = 1;
		}
		
			// postmainblock.hide("slide", { direction: "right" }, 300);
		postmainblock.fadeOut(300);
		posttitleblock.fadeOut(300);
		
			// postinfoblock.animate( {"left":-1000}, 300);
		 if (pagetype == 'page') {
			pressed = 1; 
			$.ajax({
				url: 'ajax/getpage.php',
				data: "pid=" + url + "&sec=image",
				dataType: 'json',
				contentType : "text/json",
				success: function(data) {
					posttitleblock.empty();
					if (data['type'] == "image"){
						imagesrc = data['src'];
						
						postmainblock.empty();
						
						$( function () {
							  
							  var img = new Image();
							  
							  $(img)
								
								.load(function () {
								  //$(this).hide();
								  postmainblock
									.removeClass('loading')
									.append(this);
								
								  postmainblock.fadeIn();
								})
								.error(function () {
								})
								.attr('src', imagesrc);
						});	
					}
					if (data['type'] == "slideshow"){ 
						postmainblock.empty();
						postmainblock.slideshow(data['src']);
						postmainblock.fadeIn();
					}
					$(this).loadinfoblockpage(url, 'description');	
						// postmainblock.show("slide", { direction: "right" }, 300);
				}
			});	
		 } else if (pagetype == 'post') {
			 $(this).loadcontent(post, loadswitch);
			// $(this).loadinfoblock(url, 'description');
			// $(this).loadimage(post, 'blogpost');
			// $(this).swapcontent(post, loadswitch);
		 }
			$.ajax({
				url: 'ajax/getmenu.php',
				data: "loc=" + menulocation,
				dataType: 'json',
				contentType : "text/json",
				success: function(rtn) {
					setTimeout( function(){
						if (rtn != null) {
							for (var o = 1; o < 20 ; o++)  {
								thisbox = $('#contextBoxInner .context-menu-box-wrap:nth-child('+o+')');
								if (thisbox == null) {
									alert(o);	
								}
								if (rtn[o-1] == undefined){
									// alert ('empty' + o);
									thisbox.find('.title').empty();
									thisbox.css({ display:'none' });
								}else{ 
									thisbox.css({ display:'table', 'line-height': '30px' });
									var title = rtn[o-1]['title'];
									date = rtn[o-1]['time'];
										if (date == "") { thisbox.css({ 'line-height': '30px' }) } else {
											thisbox.css({  'line-height': '16px' })
										}
									theurl = rtn[o-1]['id'];
									thisbox.find('.title').empty().html( title );
									thisbox.find('small').empty().html( date );
									if (loadswitch == 'blogpost') {
										thisbox.find('a').unbind().bind('click', {theurl: theurl}, function(e) {
										swapcontent(e.data.theurl, "blogpost");
										return false;
										});
									 } else {
										thisbox.find('a').unbind().bind('click', {theurl: theurl}, function(e) {
										swapcontent(e.data.theurl, "description");
										return false;
										});
									}
								}
							}
						}
						contextmenu.show("slide", { direction: "left" }, 300);	
					}, 100 );
					return false;		
				}
			});
		}
	}
/*	$('#top-menu a').click( function(url, sec){ 
		$(this).loadcontextmenu(url, sec);
	})
*/	
	/////////////////////
	jQuery.fn.loadcontent = function(url, sec) { 
		 hashpost = url;
			
		fragment = '#!section='+hashsection+'&p='+hashpost;
		window.location.hash = fragment;
		if (pressed == 0) {
			pressed = 1;
			
			if (sec == 'blogpost'){
				
				// $(this).loadblog(url, sec, content);
				$(this).loadblog(url, 'blogpost');
				
			} else {
			
					
				$(this).loadimage(url, sec);
				
				
			}
			
			
		};
		
		
		
		fragment = escape(fragment);
		fburl = "http://www.facebook.com/plugins/like.php?href=sparklestreet.net/2.5/"+fragment+"&amp;layout=button_count&amp;show_faces=false&amp;width=450&amp;action=recommend&amp;font=arial&amp;colorscheme=light&amp;height=21";
		
		// loadiframe(fburl);
		
		
		
		
	}
/*	loadiframe = function(fburl) {
	fbiframe = document.getElementById('fbiframe');
	fbiframe.src=fburl;
		return false;
		
	}
*/	
	
	/////////////////////
	
	jQuery.fn.loadimage = function(url, sec) {
		
			
			if (infohidden == 0) {
			infohidden = 1;
			postinfoblock.hide("slide", { direction: "right" }, 300);
			
				}
			
			postmainblock.fadeOut(300);
			posttitleblock.fadeOut(300);
			$('#gigs').fadeOut();
			
			$.ajax({
				url: 'ajax/getpost.php',
				data: "pid=" + url + "&sec=image",
				dataType: 'json',
				contentType : "text/json",
				success: function(html) {
					
					// postmainblock.html(html);
					postmainblock.empty();
					posttitleblock.empty();
					imagesrc = html['src'];
					thetitle = html['title'];
					
					// postmainblock.fadeIn(300);
					setTimeout( function(){
										 
							$( function () {
								  
								  var img = new Image();
								  
								  $(img)
									
									.load(function () {
										 
									  postmainblock
										.removeClass('loading')
										.append(this)
										.append('<div class="post-title">'+thetitle+'</div>');
									  
									  postmainblock.fadeIn();
									})
									
									.error(function () {
									})
									.attr('src', imagesrc);
							});
																	 
						
					}, 300 );
					
					
					
						// postmainblock.show("slide", { direction: "right" }, 300);
						
					$(this).loadinfoblock(url, sec);
				}
			});	

		
	}
	
	/////////////////////
	jQuery.fn.loadblog = function(url, sec) {
		
			
		if (infohidden == 0) {
			infohidden = 1;
			postinfoblock.hide("slide", { direction: "right" }, 300);
			
		}
			
			postmainblock.fadeOut(300);
			posttitleblock.fadeOut(300);
			
			
			// postinfoblock.animate( {"left":-1000}, 300);
			
			$.ajax({
				url: 'ajax/getpost.php',
				data: "pid=" + url + "&sec=blogpost",
				dataType: 'json',
				contentType : "text/json",
				success: function(html) {
					
					
					setTimeout( function(){
						postmainblock.empty();
						postmainblock.html(html['body']);
				 		posttitle = html['title'];
						posttitleblock.empty().append(posttitle);
						posttitleblock.fadeIn();
						postmainblock.fadeIn();
						pressed = 0;
					}, 300 );	
					return false;
					
				}
				
			});	
			return false;

		
	}
	/////////////////////
	
	jQuery.fn.loadinfoblock = function(url, sec) {	
		

		$.ajax({
			url: 'ajax/getpost.php',
			data: "pid=" + url + "&sec=" + sec,
			dataType: 'json',
			contentType : "text/json",

			
			success: function(rtn) {
				
				embedcode = unescape(rtn['embed']);
				
				
				posttitle = rtn['title'];
				$('#post-title-block').empty().append(posttitle);
				gigs = rtn['gigs'];
				
				
				
				setTimeout( function(){
						  
							
							
							postinfoinfo.html(rtn['body']);
							
							infohidden = 0;
							postinfoblock.show("slide", { direction: "right" }, 300);
							// postinfoblock.animate( {"left":0}, 300);
							if (gigs!=null){
							$('#gigs').empty().append(gigs).fadeIn();
							}
					pressed = 0;
					
					
				}, 400 );
				return false;		
			}
		});
		return false;
		
	}
	/////////////////////
	
		jQuery.fn.loadinfoblockpage = function(url, sec) {		
		$.ajax({
			url: 'ajax/getpage.php',
			data: "pid=" + url + "&sec=" + sec,
			dataType: 'json',
			contentType : "text/json",
			
			success: function(rtn) {
				
				setTimeout( function(){
					
						
					postinfoinfo.html(rtn['body']);
					
					if(rtn['body']!=""){
							
					
					
					infohidden = 0;
					postinfoblock.show("slide", { direction: "right" }, 300);
					
					// postinfoblock.animate( {"left":0}, 300);
					
					}
					pressed = 0;
					
				}, 400 );
				return false;		
			}
		});
		return false;
		
	}
/////////////////////

	var startcontextmenu = function(sec) { 
	
		contextmenu.css({'left':-10});
		var thisbox = 0;
		var boxwidth = 0;
		var options = '';
		
		contextmenu.find(".context-menu-box-wrap").each( function(){

			thisbox = $(this);
					
			boxwidth = thisbox.width();
			thisbox.show("slide", { direction: "left" }, 300);
			thisbox.hover(function() {
				$(this).stop().animate({ left: 10 }, 100);
			  }, function() {
				$(this).stop().animate({ left: 0 }, 100);
			});
		})
	};
/////////////////////

	jQuery.fn.mainmenuselect = function(){
		// menuitem = $(this);
		mainmenuul.find('.selected').removeClass('selected');
		mainmenuitem.addClass('selected');
		mainmenuli.addClass('selected');
		
		
	}

/////////////////////
	
	var swapcontent = function(url, sec) {
		
		// alert ('loaded');
		$(this).loadcontent(url, sec);
		
	}
	
///////////////////// SHARE BUTTONZ
	var sharewrap = $('#share-wrap');
	var sharebtn = $('#share-button');
	var sharebox = $('#share-box');
	var shareclose;
	sharewrap.hover( function() {
		 clearTimeout(shareclose);
		 sharebox.stop().animate({ height: 300 }, 300);
		 
	}, function() {
		shareclose = setTimeout( function() {
			sharebox.stop().animate({ height: 70 }, 200);
			
		}, 1000)
		
	});


	jQuery.fn.sharebox = function() {
		
	}

/////////////////////

	$(window).load(function(){
							
		locations = unescape(self.document.location.hash.substring(2));
		
		var section; 
		var post;
		var loadswitch;
		var keyValuePair,
			hashString = locations;
			// .hash.substring(2),
			nvPairs = hashString.split("&"),
			nvPair = {}; 
		
		for (var i = 0; i < nvPairs.length; ++i){ 
			keyValuePair = nvPairs[i].split("="); 
			if (keyValuePair[0] in nvPair)
			  nvPair[keyValuePair[0]].push(keyValuePair[1]);
			else
			  nvPair[keyValuePair[0]] = [keyValuePair[1]];
		} 
		// alert(dump(nvPair));
		if ('section' in nvPair) {section = nvPair['section'][0];} else {section = 'hq';};
		if ('p' in nvPair) {post = nvPair['p'][0]; pagetype = 'post';} else {post = null; pagetype = 'page'; };
		
		$("#top-menu li."+section).addClass('selected').find('a').addClass('selected');
		// alert(pagetype);
		startcontextmenu(section);
		if (section == 'hq') { loadswitch = 'blogpost'; } else { loadswitch = 'description'; };
		$(this).loadcontextmenu(section,section,loadswitch, pagetype, post ); return false;
		
	});
	
});

