	
var image_path = "sj_files/upload/";
function sDebug(content)
{
	if (typeof(console) != 'undefined') { console.log(content); }
}


function changeHeader(type,img)
{
	if (img == 'undefined' || img == null) {img = window.page_name;}
	if (type=='sub'){
		$(type + '_image').src = 'images/headers/sj_header_' + img + '.gif';
	}else{
		(function(){$(type + '_image').src = 'images/headers/sj_header_' + img + '.gif';}).delay(1200);
	}
}

function toggleSubHeader(content)
{
	if (content == '' || typeof(content) == 'undefined')
	{
		$('sub_header_text').style.display = 'none';
	//	$('sub_header').style.display = 'block';
	}
	else
	{
		$('sub_header_text').innerHTML = content;
		$('sub_header').style.display = 'none';
		$('sub_header_text').style.display = 'block';
	}
}
function fadeImage(type,img,url) 
{

	sDebug(img + '/' + url);
	$('main_image').fade('hide');										
	$('main_image').setStyle('background-image','url(' + image_path + '/' + url + ')');
	(function(){$('main_image').fade('in');}).delay(250);
}



function getCuisine()
{
	var jsonRequest = new Request.JSON({
		url: "sj_files/Services/Content.php",
		onSuccess: function(response){
			$('cuisine_submenu').innerHTML = response.content;
		}
	}).get({
		'action': 'cuisine_sub'

	});
}


function getCuisineItemDetail(cuisine_id)
{
	var jsonRequest = new Request.JSON({
		url: "sj_files/Services/Content.php",
		onSuccess: function(response){
			if (response.content.page_image)
			{
				fadeImage('sub','cuisine',response.content.page_image); ///,response.location
			}
			$('sub_box_inner').innerHTML = response.content.content;
	//$('sub_box_title').innerHTML = response.content.header;
		}
	}).get({
		'action': 'cuisine_detail',
		'cuisine_id': cuisine_id

	});
}

function getDining()
{
	var jsonRequest = new Request.JSON({
		url: "sj_files/Services/Content.php",
		onSuccess: function(response){
			$('private_dining_submenu').innerHTML = response.content;
		}
	}).get({
		'action': 'dining_sub'

	});
}
function getStaff()
	
{
	var jsonRequest = new Request.JSON({
		url: "sj_files/Services/Content.php",
		onSuccess: function(response){
			$('staff_submenu').innerHTML = response.content.content;
		}
	})
	
	
	.get({
		'action': 'staff_sub'

	});
}



function getStaffDetail(staff_id)
{
	var jsonRequest = new Request.JSON({
		url: "sj_files/Services/Content.php",
		onSuccess: function(response){
			if (response.content.page_image)
			{
				fadeImage('sub','staff',response.content.page_image);
			}
			
			$('sub_box_inner').innerHTML = response.content.content;
			
		}
	}).get({
		'action': 'staff_detail',
		'staff_id': staff_id

	});
}


function getReservations()
{
	var jsonRequest = new Request.JSON({
		url: "sj_files/Services/Content.php",
		onSuccess: function(response){
			$('reservations_submenu').innerHTML = response.content.content;
		}
	}).get({
		'action': 'reservations_sub'
		
	});
}




function anchorSubNav(type,anchor)
{
	var container = $(type + '_submenu');
	var array = container.getElements('.nav_anchor');
	var target = 'nav_anchor_' + type + '_' + anchor;

	sDebug('looking in ' + container + ' through ' + array + ' for ' + target);

	array.each(function(el){
		if(el.hasClass(target))
		{
			el.addClass('sub_nav_anchored');
		}
		else
		{
			el.removeClass('sub_nav_anchored');
		}
	});

	var parent = $(anchor).getParent('div');
	parent.getElements('a').each(function(el) {
		el.removeClass('sub_nav_anchored');
	});
	//I think this is where the problem with no sub header exists
	anchor.addClass('sub_nav_anchored');

	}

window.addEvent('domready', function() {
	var image_gallery;
	getStaff();
	getCuisine();
	getDining();
	getReservations();


});

function ScrollReload(div)
{
	if (typeof(div) == 'undefined') { div = 'sub_box_content'; }
	if ($(div).scrollUpdate)
	{
		sDebug('updating [' + div + ']');
		(function(){
			$(div).scrollUpdate();
			$(div).contentScroll(0,0);
		}).delay(250);
	}
	else
	{
		sDebug('first run [' + div + ']');
		(function(){CSBfleXcroll(div);}).delay(500);
	}
}


function getImageGallery()
{
	var jsonRequest = new Request.JSON({
		url: "sj_files/services/Content.php",
		onSuccess: function(response){
			ScrollReload();
			if (response.page_image)
			{
				fadeImage('sub','image_gallery',response.page_image); //,response.location
			}
			$('sub_box_inner').innerHTML = '<h1>Sailor Jacks<br>Image Gallery<div style="width:50px;height:50px; padding-top:50px"><img id="gallery_image" alt="" src="" /></div>';
			if (response.images)// && response.location)
			{
				image_gallery = new ImageGallery('gallery_image', response.images, 'image_gallery', image_path);
			}
		}
	}).get({
		'action': 'image_gallery',
		//'location': window.loc
	});
}




function loadPage(type, page)
{
	var limit = '1';
	
	if (page != null) { window.page_name = page; }
	if (page == null) { page = window.page_name; }

    switch (type)
	{
		case 'sub':
		subpageFlyout();
		scroll_delay = 0;
		toggleSubHeader();
		restaurant = window.loc;
		break;
		case 'side':
		sidepageFlyout();
		scroll_delay = 1200;
		break;
		case 'vert':
		vertpageFlyout();
		scroll_delay = 1200;
		break;
	}

	switch(page)
	{
		case 'staff':
		getStaff();
		limit = 1;
		break;
		case 'cuisine':
		getCuisine();
		break;
		case 'reservations':
		getReservations();
		break;
		case 'private_dining':
		getDining();
		break;
	}

	var jsonRequest = new Request.JSON({
		url: "sj_files/Services/Content.php",
		onSuccess: function(response){
			if (response.content)
			{
				if (response.content.page_image)
				{			
						
					fadeImage(type,page,response.content.page_image);
				}
				$(type + '_box_inner').innerHTML = response.content.content;

				switch (page)
				{
					case 'staff':
					case 'cuisine':
					case 'reservations':
					case 'private_dining':
						$(type + '_box_inner').innerHTML += $(page + '_submenu').innerHTML
					break;
				}

				
			}
		}
	}).get({
		'action': 'page_content',
		'page': page
	});

}


var ImageGallery = new Class({
	initialize: function(element, image_array, name, image_path) {
		this.image = $(element);
		this.images = image_array ? image_array : null;
		this.path = image_path ? image_path : 'sj_files/upload/';
		this.name = this.name ? this.name : 'image_gallery';
		this.offset = 0;
		this.delay = 5000;
		this.loop = true;
		this.timeout;

		var control_div = new Element('div', {
			'id': 'control_div',
			'styles': {
				'width': '310px', 'font-size': '16px'
				
			}
		});

		var control_prev = new Element('div', {
			'id': 'control_prev',
			'styles': {
				'float': 'left'
			},
			'html': '<a class="gallery_toggle gallery_toggle_off" href="javascript:void(\'Prev\');" type="prev">Prev</a>'
		}).inject(control_div);

		var control_next = new Element('div', {
			'id': 'control_next',
			'styles': {
				'float': 'right'
			},
			'html': '<a class="gallery_toggle gallery_toggle_off" href="javascript:void(\'Next\');" type="next">Next</a>'
		}).inject(control_div);

		var control_playpause = new Element('div', {
			'id': 'control_playpause',
			'styles': {
				'width': '150px',
				'margin': 'auto'
			},
			'html': '<span class="gallery_bullet">&bull;</span> <a class="gallery_toggle gallery_toggle_off" id="gallery_play" href="javascript:void(\'Play\');" type="start">Play</a> | <a class="gallery_toggle gallery_toggle_on" id="gallery_stop" href="javascript:void(\'Stop\')" onclick="' + this.name + '.stop();" type="stop">Stop</a> <span class="gallery_bullet">&bull;</span>'
		}).inject(control_div);

		control_div.inject(this.image, 'after');
		this.next();
	},
	next: function() {
		this.reset();
		if((this.offset + 1) == this.images.length) { this.offset = 0; } else { this.offset++; }
		this.setImg();
		if (this.loop == true) { this.timeout = setTimeout(this.name + '.next()',this.delay); }
	},
	prev: function() {
		this.reset();
		if(this.offset == 0) { this.offset = (this.images.length - 1); } else { this.offset = (this.offset - 1); }
		this.setImg();
		if (this.loop == true) { this.timeout = setTimeout(this.name + '.next()',this.delay); }
	},
	reset: function() {
		if (this.timeout)
		{
			clearTimeout(this.timeout);
		}
	},
	setImg: function() {
		sDebug('[' + this.offset + '] - ' + this.images[this.offset]);
		this.image.src = this.path + this.images[this.offset];
	},
	start: function() {
		this.loop = true;
		this.playtoggle();
		this.next();
	},
	stop: function() {
		this.reset();
		this.loop = false;
		this.playtoggle();
	},
	playtoggle: function(bool) {
		var links = $$('#control_div a.gallery_toggle');
		var instance = this.name;
  		links.each(function(el){
			if (el.hasClass('gallery_toggle_off'))
			{
				el.removeClass('gallery_toggle_off');
				el.addClass('gallery_toggle_on');
				el.href = 'javascript:' + instance + '.' + el.type + '()';
			}
			else
			{
				el.removeClass('gallery_toggle_on');
				el.addClass('gallery_toggle_off');
				el.href = 'javascript:void(\'' + el.innerHTML + '\');';
			}
		});
	}
});



