var currentSlide = 0;
var dialogOpen = false;
var showStartYourJourney = true;

$(document).ready(function() {

  // setup dialog
  $('#jqmBox').jqm(
	 {modal:false,
		overlay:40,
		onShow:onDialogOpen,
		onHide:onDialogClose});

  $('#closeButton').click(CloseWindow);
  $('#previous').click(PrevSlide);
  $('#next').click(NextSlide);	

  // delay the opening of the dialog for 3 seconds while flash loads.  I don't want to miss the cool load.
  showContainer();

  // close the window when you click outside of it.
  //  $(document).bind('click', function(e) {
  //    var $clicked=$(e.target); // get the element clicked
  //    if(!$clicked.is('a') && !$clicked.is('span')) {
  //      CloseWindow();
  //    }
  //  });
});


function showContainer () {
  if($('#jqmContent .slide').length > 1){
    $('#previous').css('color', '#d6d6d6');
    $('#jqmContent').css('height', '468px');
    $('#nextPrevious').show();
    $('#jqmContent .horizontalScroller').css('width', $('#jqmContent .slide').length * 678);
  }
  else{
    $('#jqmContent').css('height', '496px');
    $('#nextPrevious').hide();
  }	
  dialogOpen = true;
  $('#jqmBox').jqmShow();
}

function PassExternal(__path){
  $.ajax({
    type: "GET",
    url: __path,
    success: function(content) {
      content = content.replace(/\<hr *\/\>/g, "</div><div class=\"slide\"><div class=\"pageBuffer\"></div>");
      $('div#jqmContent').html("<div class=\"horizontalContainer\"><div class=\"horizontalScroller\"><div class=\"slide\">" + content + "</div></div></div>");
      showContainer();
    }
  });

  try {
    var pageTracker = _gat._getTracker("UA-9174484-1");
    pageTracker._trackPageview(__path);
  } catch(err) {}

}

function PrevSlide(){
	currentSlide--;
	$('#next').css('color', '#000');
	if(currentSlide > 0){
		$('#previous').css('color', '#000');
	}
	else{
		currentSlide = 0;
		$('#previous').css('color', '#d6d6d6');
	}
	$('#jqmContent .horizontalScroller').animate({
		left:(currentSlide * -678) + 'px'
	}, 500);
}
function NextSlide(){
	currentSlide++;
	$('#previous').css('color', '#000');
	if(currentSlide < $('#jqmContent .slide').length - 1){
		$('#next').css('color', '#000');
	}
	else{
		currentSlide = $('#jqmContent .slide').length - 1;
		$('#next').css('color', '#d6d6d6');
	}
	$('#jqmContent .horizontalScroller').animate({
		left:(currentSlide * -678) + 'px'
	}, 500);
}

function PassVideo(__path, __title, __description){
  var videoFlashvars = {file:__path,
  											skin:"/swf/bekle.swf",
  											autostart:"true",
  											controlbar:"over",
  											name:"videoContainer",
  											id:"videoContainer"};
	var videoParams = {wmode:"transparent",
										allowfullscreen:"true",
										allowscriptaccess:"always"};
										
	var videoAttributes = {};
	$('#jqmContent').html("<div id=\"videoContainer\"><div id=\"video\"></div></div><div id=\"videoTitle\"></div><div id=\"videoDescription\"></div>");
  swfobject.embedSWF("/swf/player.swf", "video", "100%", "100%", "9.0.0", false, videoFlashvars, videoParams, videoAttributes);
  $('#videoTitle').html(__title);
  $('#videoDescription').html(__description);

  dialogOpen = true;
  $('#jqmBox').jqmShow();
}

function embedSWFcb(e) {
  console.dir(e);
  if (e.success) {
    alert("success");
  }
  else {
    alert("failed");
  }
}

function showHTMLSite() {
  $('#fileTreeDemo_2').fileTree({ 
			root: '/gospel/', 
			folderEvent: 'click', 
			expandSpeed: 750, 
			collapseSpeed: 750, 
			multiFolder: false }, 
			function(file) {
                                PassExternal(file);
			});
}

function startYourJourney() {
  if (showStartYourJourney) {

    if (htmlOnly) {
      showHTMLSite();
    }
    else {
      // load the radial navigation.
      swfobject.embedSWF("/swf/radial_nav.swf", "flashContainer", "700", "500", "9.0.0", false, false, {wmode:"transparent"}, false, function(e) {
        if (! e.success) {
          showHTMLSite();
        }
      });
    }

    setTimeout('$(\'#startYourJourney\').fadeIn(1000);', 1000);
    setTimeout('$(\'#startYourJourney\').fadeOut(1000);', 6000);
    showStartYourJourney = false;
  }
}

function CloseWindow(){
  if (dialogOpen) {
    $('#jqmBox').jqmHide();
    dialogOpen = false;
  }
}


function onDialogOpen(__hash){
	//__hash.w.show();
	__hash.w.fadeIn(500);
}

function onDialogClose(__hash){
	$('#videoContainer').html('');
	__hash.w.fadeOut(500);
        __hash.o.fadeOut(500);
startYourJourney();
}
