//main variables
$login="<h1>Welcome</h1><p>Please enter your user name and press enter to access your personalized samples. Thank you.</p><form><label>User Name</label><br /><input id='username' type='text'></input><br /><input value='Enter' type='submit' id='enterbutton' /></form><br /><br /><small>If you have lost your username, please email us at info@jakgraphicdesign.com.</small>";

//***********************************************
//
//jQuery functions
//
//***********************************************

$(document).ready(function(){
	
	//initialize
	$('#mainContent').empty();
	$('#mainContent').append($login);

	$('#enterbutton').click(function(){
		loadImageThumbnails($('#username').val());
		return false;
		});
	
	});//end document.ready

//***********************************************
//
// loadImageThumbnails(folder)
//
// reads in the XML sheet and loads the custom thumbnails from the user-inputted value
//
//***********************************************
function loadImageThumbnails(folder){
	
	$('#mainContent').slideUp('slow', function(){	
		
		//Step 1 Initialize
		var $toLoad="";
		var $qty=0;
		var $ext="";
		
		if($.browser.msie && $.browser.version < 7){
			$ext="gif";
			}//end if
		
		else{
			$ext="png";
			}
		
		$('#mainContent').empty();
		$('#mainContent').append("<img src='http://www.jakgraphicdesign.com/lib/src/facebox/loading.gif' height='25' width='25' style='vertical-align:middle;'/>&nbsp;&nbsp;<strong>Loading...</strong>");
		$('#mainContent').slideDown('normal',function(){
		
		//Step 2 Load images
		$.ajax({
			type: "GET",
			url: "images/"+folder+"/contentdata.xml",
			dataType: "xml",
			
			success: function(xml){
				
				//Error State
				if(xml.length<2){
					alert("Sorry, user not found.");
					logOut();
					}
				
				else{
					
					$('section',xml).each(function(i){
						
						//Load Heading
						if($(this).find('heading').attr('imgreplacement')){
							$toLoad=$toLoad+"<h1><img src='images/"+folder+"/file/"+$(this).find('heading').attr('imgreplacement')+"' alt='"+$(this).find('heading').text()+"'/></h1>";
							}//end if
						
						else{
							$toLoad=$toLoad+"<h1>"+$(this).find('heading').text()+"</h1>";
							}//end else
						
						//Load Paragraphs
						$(this).find('paragraph').each(function(i){
							$toLoad=$toLoad+"<p>"+$(this).text()+"</p>";
							});
						
						//Load Thumbnails
						$toLoad=$toLoad+"<div class='controlbar'><a class='logoutlink' href=''>Log Out</a></div>";
						
						$(this).find('imagefile').each(function(){
							
							$image=$(this).text();
							
							$toLoad=$toLoad+"<div class='thumbnail'><a";
							
							if($(this).attr('download') && $(this).attr('download').length>0){
								$title='<a class="fancyboxlink" href="images/'+folder+'/file/download/'+$(this).attr('download')+'" target="_blank">Download PDF</a>';
								$toLoad=$toLoad+" title='"+$title+"'";
								}//end if
							
							$toLoad=$toLoad+" rel='"+folder+"' href='images/"+folder+"/file/full/"+$image+"'>"+
							"<img src='images/layout/thumb_foreground."+$ext+"' alt='[x]' width='160' height='160' border='0' style='background:url(images/"+
							folder+"/file/thumb/"+$image+") center no-repeat;'/>"+"</a></div>";
							});
					
						});//end of section loop
					
					$toLoad=$toLoad+"<div class='controlbar'><a class='logoutlink' href=''>Log Out</a></div>";
					
					$('#mainContent').slideUp('slow',function(){
						$('#mainContent').empty();
						$('#mainContent').append($toLoad);
						$('#mainContent').slideDown('normal');
						$('.thumbnail a').fancybox({
							'hideOnContentClick': false
							});
						$('.logoutlink').click(function(){logOut();return false;});
						});
					
					}//end else
			
				},//end success Function
			
			error:function(x){
				alert("Sorry, user not found.");
				logOut();
				}
			
			});});
				
		});
	
	}//end loadImageThumbnails

//***********************************************
//
// logOut()
//
// resets everything
//
//***********************************************
	
function logOut(){
	$('#mainContent').slideUp('slow',function(){
		$('#mainContent').empty();
		$('#mainContent').append($login);
		$('#enterbutton').click(function(){
			loadImageThumbnails($('#username').val());
			return false;
			});
		$('#mainContent').slideDown('normal');
		});
	}//end logOut