$(document).ready(function(){
	$("#tiles ul li").hide();
	var globalHTTP = $('[name="global"]').val();
	var session = globalHTTP+'/session.php';
	$('#twocol').lionbars();
	
	$('.show').each(function (i) {					  
		var whichShow = '#show'+i;
		var delay = i * 300;
		$(whichShow).delay(delay).fadeIn('slow', function() {			
      	});
	});
	$(".menutab").hover(
	  function () {
		$(this).css("height","150");
		$(".menutabnav").show();
	  },
	  function () {
		$(".menutabnav").hide();
	  }
	);
	function toggle(position) {
		//alert(position);
		$.ajax({
			type: "POST",
			url: session,
			data: '&toggle='+position, 
			cache: false,
			async: false,
			success: function(data) {
				//alert(data);
			},
			error: function(data) {
				//alert("some error occured, please try again later");
			}
		});	
	}
	var state = $('[name="toggle"]').val();
	$('#off').click(function() {
		var state = $('[name="toggle"]').val();
		if (state == "on") {
			$('#mylightbox').hide();
			$('#off').text('on');
			toggle("off");
			$('[name="toggle"]').val("off");
		} else {
			$('#mylightbox').show();
			$('#off').text('off');
			toggle("on");
			$('[name="toggle"]').val("on");
		}
	});
	if (state == "off") {
		$('#mylightbox').hide();
		$('#off').text('on');
	} else {
		$('#mylightbox').show();
		$('#off').text('off');
		
	}
});
$(window).load(function () {
	var count = $('[name="theCount"]').val(); 
	var state = $('[name="toggle"]').val(); 
	var globalHTTP = $('[name="global"]').val();
	var session = globalHTTP+'/session.php';
	function positionTabs() {
		var newcount = count - 5;
		var position = newcount * 197; // This is the width of the average 'item' which will be used to calculate the position of the tab so that the most recent item is shown on a reload.	
		var newposition = "-"+position+"px";
		$('#lightbox').css({"margin-left": newposition});
	}
	positionTabs();
	
	function appendItem(filename, count, media_type) {
		if (media_type == "video") { // Checks to see if it's Video
			var lightbox = '<li><div style="position: absolute; z-index:100;"><img src="../images/btn_video.png" /></div><img src="'+globalHTTP+'media/thumb/'+filename+'" /></li>';
		} else {
			var lightbox = 	'<li><img src="'+globalHTTP+'media/thumb/'+filename+'" /></li>';	
		}

		$('#lightbox > ul').append(lightbox);

		if (count > 5) {
			$('#lightbox').animate({"margin-left": "-=197px"}, "slow", 
					function() { });
		}
	}

	$(function() {
		$("#sortgrid").sortable({ opacity: 0.6, cursor: 'move', update: function() {
			var page_id = $("input[name=media_id]").val();
			var order = $(this).sortable("serialize") + '&action=updateRecordsListings&page_id=' + page_id;
			$.post(session, order, function(theResponse){	
				$("input[name=theResponse]").val(theResponse);
				
			}); 															 
		}
	});

	$(".addtolightbox").click(function() {		
								   
		// On clicking one of the add buttons this creates all the variables and strings to be sent to the session and update the visual shopping cart
		var whichID = $(this).attr('id').substr(1); // This is the item number
		var filename = $('[name="filename_'+whichID+'"]').val();
		var media_type = $('[name="media_type_'+whichID+'"]').val();
		// This bundles and sends the information to the Session array
		$.ajax({
			type: "POST",
			url: session,
			data: '&update=session&image='+whichID+'&filename='+filename, 
			cache: false,
			async: false,
			success: function(data) {
				var count = data;				
				appendItem(filename, count, media_type);
				$('#count').css({color:'#FF0000'});
				$('#count').animate({color:'#BFBFBF'}, 1000);
				$('#count').text(count);
				
			},
			error: function(data) {
				alert("some error occured, please try again later");
			}
		});		
	});
	
	$(".remove").live({ // Live binds future additions to the cart so that they can be removed.
		click: function() {	
			var whichID = $(this).attr('id').substr(7); // This is the array postion reference
			var remove = '#remove_'+whichID;
			$.ajax({
				type: "POST",
				url: "session.php",
				data: '&removeitem=session&theItem='+whichID, 
				cache: false,
				async: false,
				success: function(data) {
					$("#count").text(data);
					$('[name="thecount"]').val(data);
					$(remove).fadeOut(200, function () {
						$(remove).remove();
					});
				},
				error: function(data) {
					alert("some error occured, please try again later");
				}
			});
		}
	});
	
	// These are only necessary for testing, you can remove this before launch
	$("#destroy").click(function() {
		$.ajax({
				type: "POST",
				url: "session.php",
				data: '&destroysession=destroy',
				cache: false,
				async: false,
				success: function(data) {
					//alert("success: "+data);
					location.reload();
				},
				error: function(data) {
					alert("some error occured, please try again later");
				}
			});
	});
	
	$("#viewsession").click(function() {
		$.ajax({
				type: "POST",
				url: "session.php",
				data: '&viewsession=view',
				cache: false,
				async: false,
				success: function(data) {
					alert("success: "+data);
				},
				error: function(data) {
					alert("some error occured, please try again later");
				}
			});
	});	
});
});
