var image_path = 'http://www.tkrg.org/upload/';
//var image_path = 'http://www.tkrgdev.com/upload/'; // uncomment for dev use

function sDebug(content)
{
	if (typeof(console) != 'undefined') { console.log(content); }
}

function fadeImage(type,img,url,location) // type can be sub, side, or vert
{
	sDebug(img + ' - ' + location + '/' + url);
	$('main_image').fade('hide');
	$('main_image').setStyle('background-image','url(' + image_path + location + '/' + url + ')'); /*Image for the page*/
	(function(){$('main_image').fade('in');}).delay(250);
}

function changeHeader(type,img)
{
	if (img == 'undefined' || img == null) {img = window.page_name;}
	if (type=='sub'){
		$(type + '_image').src = 'images/headers/bb_header_' + img + '.gif';
	}else{
		(function(){$(type + '_image').src = 'images/headers/bb_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 loadPage(type, page)
{
	var limit = '';
	restaurant = 3;
	menuStory('N');
	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: "services/getContent.php",
		onSuccess: function(response){
			if (response.content)
			{
				if (response.content.page_image)
				{
					fadeImage(type,page,response.content.page_image,response.location);
				}
				$(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;
				}

				ScrollReload(type + '_box_content');
			}
		}
	}).get({
		'action': 'page_content',
		'page': page,
		'location': restaurant,
		'limit': limit
	});
	changeHeader(type,page);
}

function getStaff()
{
	var jsonRequest = new Request.JSON({
		url: "services/getContent.php",
		onSuccess: function(response){
			$('staff_submenu').innerHTML = response.content;
		}
	}).get({
		'action': 'staff_sub',
		'location': window.loc
	});
}

function getStaffDetail(staff_id)
{
	var jsonRequest = new Request.JSON({
		url: "services/getContent.php",
		onSuccess: function(response){
			if (response.content.page_image)
			{
				fadeImage('sub','staff',response.content.page_image,response.location);
			}
			toggleSubHeader(response.content.name.toUpperCase());
			$('sub_box_inner').innerHTML = response.content.content;
			ScrollReload();
		}
	}).get({
		'action': 'staff_detail',
		'staff_id': staff_id,
		'location': window.loc
	});
}

function getCuisine()
{
	var jsonRequest = new Request.JSON({
		url: "services/getContent.php",
		onSuccess: function(response){
			$('cuisine_submenu').innerHTML = response.content;
		}
	}).get({
		'action': 'cuisine_sub',
		'location': window.loc
	});
}

function getCuisineItemDetail(cuisine_id)
{
	var jsonRequest = new Request.JSON({
		url: "services/getContent.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;
			ScrollReload();
			if (response.content.story.enabled == 'Y')
			{
				menuStory('Y');
				$('story_box_inner').innerHTML = response.content.story.story;
				$('story_header').innerHTML = response.content.story.story_name;
			}
			else
			{
				menuStory('N');
			}
		}
	}).get({
		'action': 'cuisine_detail',
		'cuisine_id': cuisine_id,
		'location': window.loc
	});
}

function menuStory(response)
{
	if (response == 'Y')
	{
		$('story_link').setStyle('display','block');
		$('story_link').tween('width','225px');
		(function(){$('story_link').setStyle('font-size','11px')}).delay(350);
	}
	else
	{
		closeModal('story_box');
		$('story_link').tween('width','0');
		(function(){$('story_link').setStyle('font-size','0')}).delay(350);
		(function(){$('story_link').setStyle('display','none')}).delay(500);
	}
}

function getWineList()
{
	var jsonRequest = new Request.JSON({
		url: "services/getContent.php",
		onSuccess: function(response){
			if (response.content.page_image)
			{
				fadeImage('sub','cuisine',response.content.page_image,response.location);
			}
			toggleSubHeader('Wines');
			$('sub_box_inner').innerHTML = response.content.content;
			ScrollReload();
		}
	}).get({
		'action': 'wines',
		'location': window.loc
	});
}

function getDining()
{
	var jsonRequest = new Request.JSON({
		url: "services/getContent.php",
		onSuccess: function(response){
			$('private_dining_submenu').innerHTML = response.content;
		}
	}).get({
		'action': 'dining_sub',
		'location': window.loc
	});
}

function getReservations()
{
	var jsonRequest = new Request.JSON({
		url: "services/getContent.php",
		onSuccess: function(response){
			$('reservations_submenu').innerHTML = response.content;
		}
	}).get({
		'action': 'reservations_sub',
		'location': window.loc
	});
}

function getMap()
{
	var jsonRequest = new Request.JSON({
		url: "services/getContent.php",
		onSuccess: function(response){
			if (response.page_image)
			{
				fadeImage('sub','maps',response.page_image,response.location);
			}
			$('sub_box_inner').innerHTML = response.content;
			// set map image src
			$('map_image').src = image_path + response.location + '/' + response.map.image;
			ScrollReload();
		}
	}).get({
		'action': 'maps',
		'limit': 1,
		'location': window.loc
	});
}

function getGiftCertificates()
{
	var jsonRequest = new Request.JSON({
		url: "services/getContent.php",
		onSuccess: function(response){
			if (response.page_image)
			{
				fadeImage('sub','gift_certificates',response.page_image,response.location);
			}
			$('sub_box_inner').innerHTML = response.content;
			ScrollReload();
		}
	}).get({
		'action': 'gifts',
		'location': window.loc
	});
}

function getImageGallery()
{
	var jsonRequest = new Request.JSON({
		url: "services/getContent.php",
		onSuccess: function(response){
			ScrollReload();
			if (response.page_image)
			{
				fadeImage('sub','image_gallery',response.page_image,response.location);
			}
			$('sub_box_inner').innerHTML = '<div style="position:relative;left:-20px;top:-20px;"><img id="gallery_image" alt="" src="" /></div>';
			if (response.images && response.location)
			{
				TKRG_gallery = new ImageGallery('gallery_image', response.images, 'TKRG_gallery', image_path);
			}
		}
	}).get({
		'action': 'image_gallery',
		'location': window.loc
	});
}

function getInfoRequest()
{
	switch (window.loc)
	{
		case 4:
		calClass = 'blue';
		break;
		default:
		calClass = 'red';
	}

	var jsonRequest = new Request.JSON({
		url: "services/getContent.php",
		onSuccess: function(response){
			if (response.page_image)
			{
				fadeImage('sub','info_request',response.page_image,response.location);
			}
			$('sub_box_inner').innerHTML = response.content;
			myCal = new Calendar({ 'contact_start_date:date': 'm/d/Y', 'contact_end_date:date': 'm/d/Y' }, { classes: [calClass + '_calendar']
		                                                                                                           	, direction: .5, pad: 0});
			ScrollReload();
		}
	}).get({
		'action': 'info_request',
		'location': window.loc
	});
}

function getAccolades()
{
	// default location is window.loc, otherwise use the param passed in
	var loc = window.loc;
	if (arguments.length == 1) {
		loc = arguments[0];
	}
	
//	var jsonRequest = new Request.JSON({
//		url: "services/getContent.php",
//		onSuccess: function(response){
//			$('accolades_box_inner').innerHTML = response.content;
//			accoladespageFlyout('show');
//			ScrollReload('accolades_box_content');
//		}
//	}).get({
//		'action': 'get_accolades',
//		'location': window.loc
//	});
	
	var jsonRequest = new Request.JSON({
		url: "services/getContent.php",
		onSuccess: function(response){
			$('accolades_box_inner').innerHTML = response.content;
			accoladespageFlyout('show');
			ScrollReload('accolades_box_content');
		}
	}).get({
		'action': 'get_accolades',
		'location': loc
	});
}

function getPR(loc)
{
	var jsonRequest = new Request.JSON({
		url: "services/getContent.php",
		onSuccess: function(response){
			$('press_box_inner').innerHTML = response.content;
			presspageFlyout('show');
			ScrollReload('press_box_content');
		}
	}).get({
		'action': 'get_pr',
		'location': loc
	});
}

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');
	});
	anchor.addClass('sub_nav_anchored');
*/
}

window.addEvent('domready', function() {
	var TKRG_gallery;
	getStaff();
	getCuisine();
	getDining();
	getReservations();

	ScrollReload('vert_box_inner');
});

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);
	}
}

/* Example:
 *	TKRG_gallery = new ImageGallery('gallery_image', response.images, response.location, 'TKRG_gallery');
 *
 *  note how last value matches the js variable name assigned to the class instance, until i find a way to
 *	grab this from within the class (if its even possible) it must be passed in to work properly.
 */
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 : 'http://www.tkrg.org/upload/';
		this.name = this.name ? this.name : 'TKRG_gallery';
		this.offset = 0;
		this.delay = 5000;
		this.loop = true;
		this.timeout;

		var control_div = new Element('div', {
			'id': 'control_div',
			'styles': {
				'width': '310px'
			}
		});

		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 + '\');';
			}
		});
	}
});