var BASE = '/edwin/';
var SITE_TITLE = 'Edwin Allan';
var AJAX_PATH = BASE+'ajax.php';
// Woah, IE doesn't even understand no fuckin constants. Shame on ye MS
var curSlide = 0;
var ourInterval;
$.jQTouch({

});

function updateHash(url,title){
	if(url != 'null/'){
		window.location.hash = '/'+url;
		document.title = SITE_TITLE+' - '+title;
	}
}

function doSomething()
{
	if($('img.slide').length == curSlide){
		curSlide = 0;
	}
	var curImage = $('img.slide:eq('+curSlide+')').attr('src');
	$('#main').css('background-image','url("'+curImage+'")');
	
	curSlide++;
}

$(document).ready(function(){
	 // Redirect to hash location
	 if(window.location.hash.length > 0){
		 var hash = window.location.hash.replace("#/",'');
		 window.location = BASE+hash;
	 }else{
		var loc = window.location.toString().replace(/^http:\/\/[a-zA-Z.]*\/[a-zA-Z.]*\//,'').split("/");
		loc = loc.length-1;
		var relpath = '';
		for(var i=0;i<loc;i++){
			relpath = relpath+'../';
		}
		$('#flash').flashembed(
		{
			 src: 'site/main.swf',
			 width: '100%',
			 height: '100%',
			 version: [9, 115]
		},
		{
			 menuname: $('.sitemap .current').attr('name'),
			 pageid: $('body').attr('id').replace('page_',''),
			 curPath: relpath
		});
	 }
	 
	 
	 $('a[href=#]').click(function(e){
		e.preventDefault();
	 });
	 
	 
	 $('#opensitemap .menu').click(function(){
		$('.pages').hide();
		$(this).children('.pages').show();
	 });
	 
	 $('#opensitemap .pages li').click(function(){
		$('.items').hide();
		$(this).children('.items').show();
	 });
	 
	 $('#delete,#autohome').click(function(){
		$(this).children().show();  
	 });
	 
	 
	// Hash example
	 $('#click').click(function(){
		 window.location.hash = '/yoyo';
		 document.title = SITE_TITLE+' - Yoyo';
	 });
	
	// iPhone Slideshow
	var agent=navigator.userAgent.toLowerCase();
	var is_iphone = (agent.indexOf('iphone')!=-1);
	if (is_iphone) {
		$('.sitemap li.current ul li:last-child').css('border-bottom','none');
		if($('img.slide').length >= curSlide){
			$('h1').after('<div id="tip">Rotate your iPhone to view slideshow</div>')
			doSomething();
			ourInterval = setInterval("doSomething()", 3000 );
			if($('body').hasClass('landscape')){
				$('#main').children().hide();
			}else{
				$('#main').children().show();
			}
		}else{
			$('body').addClass('noimages')
		}
		}
	
	$('body').bind('turn', function(event, info){
		if(info.orientation == 'landscape'){
			if($('img.slide').length >= curSlide){
				$('#main').children().hide();
			}
			setTimeout(function(){window.scrollTo(0, 1);}, 100);
		}else{
			$('#main').children().show();
			setTimeout(function(){window.scrollTo(0, 1);}, 100);
		}
	});
	
	// ADMIN
	$('img.alias').click(function(){
		  $.ajax({
			   url: AJAX_PATH,
			   type: "POST",
			   data: ({
				    formaction : 'aliasHome',
				    itemid : $(this).attr('title')
				    }),
			   dataType: "html",
			   success: function(msg){
			   }
			}
		    );
		   $(this).remove()
	});
	$('img.removeItem').click(function(){
		  $.ajax({
			   url: AJAX_PATH,
			   type: "POST",
			   data: ({
				    formaction : 'removeItem',
				    itemid : $(this).attr('title')
				    }),
			   dataType: "html",
			   success: function(msg){
			   }
			}
		    );
		 $(this).parent().remove()
	});
	
	 $("#sortable").sortable({
		  placeholder: 'ui-state-highlight',
		  update: function(e,ui){
			   var order = $("#sortable").sortable('toArray');
			   $.ajax({
				    url: AJAX_PATH,
				    type: "POST",
				    data: ({
					     formaction : 'sortRatings',
					     hash : order
					     }),
				    dataType: "html",
				    success: function(msg){
				    }
				 }
			     );
		  }
	 }).disableSelection();

	 $('#autohome a').click(function(){
		$.ajax({
			url: AJAX_PATH,
			type: "POST",
			data: ({
				formaction : 'autoHome'
				}),
			dataType: "html",
			success: function(msg){
				alert('Home updated !');
			}
		}
		);
	 });
	 
	 $('#users li.user:even').addClass('even');
	 $('#pages li:even').addClass('even');
	 
	var curpages = new Array();
	$('#validate').hide();
	$('#users .user').click(function(){
	$('#users .selected').removeClass('selected');
	$(this).addClass('selected');
		$.ajax({
			url: AJAX_PATH,
			type: "POST",
			data: ({
				formaction : 'showAccesPages',
				userid: $(this).attr('title')
				}),
			dataType: "html",
			success: function(msg){
				$('#pages').html(msg);
				$('#validate').show();
				$("#pages .ui-selected").each(function(){
					var index = $(this).attr('title');
					curpages.push(index);
				});
			// Multi-selection with jQuery UI
				$('#pages').selectable({
					filter: 'li.page',
					
					stop: function(){
					curpages = new Array();
						$(".ui-selected", this).each(function(){
							var index = $(this).attr('title');
							curpages.push(index);
						});
					}
				});
			}
		}
		);
		$.ajax({
			url: AJAX_PATH,
			type: "POST",
			data: ({
				formaction : 'showUserLog',
				userid: $(this).attr('title')
				}),
			dataType: "html",
			success: function(msg){
				$('#users samp').html(msg);
			}
		});
	});
	
	$('#validate').click(function(){
		$.ajax({
			url: AJAX_PATH,
			type: "POST",
			data: ({
				formaction : 'updateUserAccess',
				userid: $('#users li.selected').attr('title'),
				newaccess: curpages
			}),
			dataType: "html",
			success: function(msg){
				$('#validate').hide();
				$('#pages').html('User access successfuly updated !');
			}
		});
		
	});
	
	$('.userlog').click(function(){
		$.ajax({
			url: AJAX_PATH,
			type: "POST",
			data: ({
				formaction : 'clearUserLog',
				userid: $(this).attr('title')
			}),
			dataType: "html",
			success: function(msg){
				$(this).remove();
			}
		});
	});
	
});

$(window).load(function(){
	$('#main').css('visibility','visible');
	setTimeout(function(){window.scrollTo(0, 1);}, 100);
});

