// JavaScript Document
var obj = null;
var mainOn = false;
var subOn = false;
var x = 0;
var contentJSON = '';

function callback(hash) {
  // do stuff that loads page content based on hash variable
	// alert(hash);
	if(hash == '' || hash == undefined) {
		$("#welcome").fadeIn("slow");
	}
	else{
		$("#welcome").hide();
	}
	var url = document.location.href;
	//alert(url + url.indexOf('page'));
	var mode="work";
	
	if (url.indexOf('page-') > 0) {
			mode="page";
	}	
	LoadPage(".rail-column",hash,mode);
}
  
$(document).ready(function(){
	var currHash = window.location.hash.substr(1);
	callback(currHash);
	//$.history.init(callback);			
	//$("a[@rel='history']").click(function(){
  //$.history.load(this.href.replace(/^.*#/, ''));
  //        return false;
  //  });
	
	//setInterval("checkClose()", 600);
	//".rail-column").hide();
	$('.drop-down-list').addClass('list-off');
  
	$('.nav-drop-down').click(function(){ //mouse in
	  var id = $(this).parent().attr("id");
    
		if($(this).parent().find('.drop-down-list').hasClass('list-off')) {
			if (id == id.replace("-open", "")) {
				id = id + "-open";
			}
			closeDropDowns();
			$(this).parent().attr("id", id).find('.drop-down-list').removeClass('list-off');
		}
		else {
			id = id.replace("-open", "");
			$(this).parent().attr("id", id).removeClass('drop-down-open').attr("id", id);
      
			closeDropDowns();
		}		
	});
	
	$('#container').click(function(){
		closeDropDowns();
	});
	
	$('.drop-down-list').hover(function(){ //mouse in
		subOn = true;
	},
	function(){ //mouse out
		subOn = false;	
	});
	
	$('.project-link-home').click(function(){
		$('.rail-column').fadeOut("slow");
		$("#welcome").fadeIn("slow");
		closeDropDowns();
		$('#page-drop-down').html("DAVID MASHBURN");
		$('#work-drop-down').html("DESIGN");
		$('#nav-work-open').attr("id","nav-work");
		$('#nav-info-open').attr("id","nav-info");
	});
	
	$('.project-link').click(function(){
		//load sample work
		$("#welcome").fadeOut("fast");
		closeDropDowns();
		$('#work-drop-down').html($(this).html());
		$('#nav-work-open').attr("id","nav-work");
		var hash = ($(this).attr("href")).substr(1);
    LoadPage('.rail-column', hash, "work");//skiphere
    //$.history.load(this.href.replace(/^.*#/, ''));
		$('#page-drop-down').html("DAVID MASHBURN");
    
    return false;		
	});
  
	$('.page-link').click(function(){
		$("#welcome").fadeOut("fast");
		closeDropDowns();
		$('#nav-info-open').attr("id","nav-info");
		var hash = ($(this).attr("href")).substr(1);
    LoadPage('.rail-column', hash, "page");//skiphere
		$('#page-drop-down').html($(this).html());
		$('#work-drop-down').html("DESIGN");
		 
    return false;
	});
});

function LoadPage(target, id, mode) {
	//alert(id);
	if (mode=="work" || mode == null) {
		ListWorkItems(target,id);
	}
	else if (mode=="page") {
		var json = { "page": id.replace('page-','') };
		$(target).html('<img src="img/loading.gif" />');
		
    $.post("includes/php/content.php?mode=getpage",
           json,
           function(results){
             contentJSON = eval('(' + results + ')');
             $(target).html(contentJSON.data[0].content);
           });
	}
}

function ListWorkItems(target, id) {
	id = id.replace(/_/g, " "); //put spaces back into client name
	var json = { "client": id };
	$(target).html('<img src="img/loading.gif" />');
  
	$.post("includes/php/content.php?mode=listwork", json, 
    function(results){
      contentJSON = eval('(' + results + ')');
      var output = '';
		
      for(i=0; i<contentJSON.data.length; i++){
        output += '<div class="rail-object" id="' + i + '"><img src="img/portfolio/' + contentJSON.data[i].imgname + '" alt="' + contentJSON.data[i].title + '" /></div>\n' ;
      }
      
      $(target).html(output);		
      $(target).fadeIn("slow");	  
      
      //set rail-tip action after populated
      setRailListener('.rail-object');
      
      $('.rail-object').hover(function(){
        loadRailTip($(this).attr("id")); 
      },	
      function(){
       
      });//end workspace rollover
      
    });//end response mechanism	
}

function loadRailTip(index) {
	$('.rail-tip').html('<img src="img/loading.gif" />');
  	
  var output = '<div class="info">' + 
                 '<span class="rail-top"></span>' + 
                 '<div class="rail-middle">' + 
                  '<h2>' + contentJSON.data[index].client_name + '</h2>' + 
                  '<div class="hr"></div>' +
                  '<div class="info-text">' +	contentJSON.data[index].client_description + '</div>' +
                  '<div class="hr"></div>' + 
                  '<h2 class="selected">' + contentJSON.data[index].title + '</h2>' +
                  '<div class="hr"></div>';  
  if (contentJSON.data[index].description != '') {
    output +=     '<div class="info-text"><p>' + contentJSON.data[index].description + '</p></div>' + 
                  '<div class="hr"></div>';
  }
  output +=      '</div>' + 
                 '<span class="rail-bottom"></span>' + 
                '</div>';
                
	$('.rail-tip').html(output);	
}

function checkClose() {
	if (!subOn && !mainOn){
		closeDropDowns();
	}
}

function closeDropDowns() {
	$('.drop-down-list').addClass('list-off');
}