//	issue with opera can't click on idtabs styles don't load until a link is clicked
// ie removes the ticker

// some variables to save
var currentPosition;
var currentVolume;
var currentItem;
var currentState = 2;
var DEBUG = false;

// these functions are caught by the JavascriptView object of the player.
function sendEvent(typ,prm) { thisMovie('b295HdrObj').sendEvent(typ,prm); };
function getUpdate(typ,pr1,pr2,pid) {
	if(typ == "time") { currentPosition = pr1; }
	else if(typ == "volume") { currentVolume = pr1; }
	else if(typ == "item") { currentItem = pr1; setTimeout("getItemData(currentItem)",100); }
	else if(typ == "state") { currentState = pr1;  /* console.info(typ + ' ' + pr1);*/ }
	var id = document.getElementById(typ);
	// id.innerHTML = typ+ ": "+Math.round(pr1);
	// pr2 == undefined ? null: id.innerHTML += ", "+Math.round(pr2);
	if(pid != "null") {
		// console.debug("received from the player with id: "+pid);
	}
};

function oc(a)	{
// snook.ca occurence in array return turn
  var o = {};
  for(var i=0;i<a.length;i++)
  {
    o[a[i]]='';
  }
  return o;
}

// These functions are caught by the feeder object of the player.
function loadFile(obj) { thisMovie(b295Hdr.attributes.id).loadFile(obj); };
function addItem(obj,idx) { thisMovie(b295Hdr.attributes.id).addItem(obj,idx); }
function removeItem(idx) { thisMovie(b295Hdr.attributes.id).removeItem(idx); }
function getItemData(idx) {
	var obj = thisMovie(b295Hdr.attributes.id).itemData(idx);
	var nodes = "";
	for(var i in obj) { 
		//	console.debug("mediaplyr event "+i+": "+obj[i]+""); 
	}
};

// This is a javascript handler for the player and is always needed.
function thisMovie(movieName) {
	if(navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
};

// wrapper for 
function mp3LinkCall(href, title, rel)	{
				thisMovie(b295Hdr.attributes.id).controller.getEvent("playpause");
}
function makePaused()	{
	// console.info("currentState: " + currentState);
			if(currentState)
				sendEvent('playpause');
}

function consoleWrite(type, msg)	{
				if (DEBUG)	{
					if (type == 'info')	{
						console.info(msg);
					}	else if (type == 'warning')	{
						console.warn(msg);
					}	else if (type == 'debug')	{
						console.debug(msg);
					}
				}
}

// globals
var bodyClass = '';

// jLink Call
function jLinkCall(url, rel)	{

	consoleWrite('debug', url);
	consoleWrite('debug', rel);

	//	add var:value for json format "format:json";
	
	// determin if pretty urls are enabled
	var useInternalRewrite = false;
	// break dow the url & set up required vars.  contruct dataURL and params object then call via json
	var regex = ((useInternalRewrite === false) ? '\/v6\/(home|events|history|biogs|media|charts|contact|membership|photos|videos)(.*)' : '\/v6\/index\.php\/(home|events|history|biogs|media|charts|contact|membership|photos|videos)(.*)');
	var viewURL = url.toString(), dataURL = "", params = {}, re = new RegExp(regex, "g"), addOn = '';
	var PageClassName = $('body').attr('class');
	var shortcut = re.exec(viewURL);
	var relation = rel;
	
	// BlockUI
	$('#b295Bdy').block( '<p>Loading Please Wait</p><img src="' + siteCfg.path + '/img/default-ajax_loader.gif" width="25" height="25" border="0" alt="" title="" />' ).pngFix();
	regex = '\/(index|display|view)([\/]*)([0-9a-zA-Z]*)';
	re = new RegExp(regex, "g");
	addOn = re.exec(shortcut[2]);

	dataURL = ((!useInternalRewrite) ? siteCfg.path : siteCfg.path.replace('/webroot', '/index.php')) + "/" + shortcut[1] + ((addOn !== null) ? '/' + addOn[1] + addOn[2] + addOn[3] : '/index') + "/format:json/";

	// remove swfs
	$(swfObjs).each(function(i){
	   swfobject.removeSWF(swfObjs[i]);
	   // array is now emtpy
	   swfObjs = [];
	});



	$.getJSON(
		dataURL, 
		function (json)	{
			var htmlContent = json.contentStr;
			bodyClass = json.classStr;
			var subClass = json.subclassStr;
			var htmlTitle = json.titleStr;
			var htmlNavigation = json.navigation;
			var targetDiv = 'b295' +  bodyClass.substr(0, 1).toUpperCase() + bodyClass.substr(1);

			document.title = "Back to 95 " + htmlTitle;

			if (PageClassName === bodyClass)	{
				$('#' + targetDiv).slideUp({
					'duration': 250, 
					'easing': 'easeOutBounce',
					'complete': function ()	{
						$(this).html($(htmlContent).html());
					}
				}).slideDown({
					'duration': 250, 
					'easing': 'easeOutBounce',
					'complete': function ()	{
						jLinkComplete(bodyClass);
						$('.scroll-pane').jScrollPane();
					}
				});
			} else {
				$("#navi").html(htmlNavigation);
				$('body').removeClass(PageClassName).addClass(bodyClass);
				$("#b295Bdy").append(htmlContent);
				var oldContent = $('#b295' + (PageClassName.substr(0, 1).toUpperCase() + PageClassName.substr(1)));
				$('#b295' + (PageClassName.substr(0, 1).toUpperCase() + PageClassName.substr(1))).fadeOut({
					'duration': 250, 
					'easing': 'easeOutBounce', 
					'complete': function()	{
						oldContent.remove();
						$('#' + targetDiv).fadeIn({
							'duration': 250, 
							'easing': 'easeInBounce', 
							'complete': function()	{
								consoleWrite('debug', 'About to complete');
								jLinkComplete(bodyClass);
								$('.scroll-pane').jScrollPane();
							}
						});
					}
				});
			}
		}
	); // /getJSON
}

var runOnce = false;

function init()	{
			var PageClassName = $('body').attr('class');

			if (jQuery.browser.msie === true)		{
				$('.scroll-pane').jScrollPane();
			}

			if (runOnce === false)	{
				themeInit();
				runOnce = true;
			}
				b295SetContentByClass(PageClassName);

				$('.jLink, .jlink, .header, .nav	a, a[@href$="mp3"], a.lightbox')
						.unbind('click');

				// mp3 - make mp3 link to mp3player if flash installed
				$('a[@href$="mp3"]')
					.bind(
						'click',
						{'href': this.href,'rel': this.rel,'title':this.title},
						function(event)	{
							event.stopPropagation();
							event.preventDefault();
							mp3LinkCall(this.href, this.title, this.rel);
							$(this).flash(
								{ 'src': siteCfg.path + '/flash/mp3player.swf', height: 20, width: 100 },
								{ 'version': 8 },
								function(htmlOptions) {
									$this = $(this);
									htmlOptions.flashvars.file = $this.attr('href');
									$this.before($.fn.flash.transform(htmlOptions));
								}
							);
						}
					);

				$(".jLink, .jlink, .header, .nav	a")
						.bind(
							'click',
							{'href': this.href,'rel': this.rel},
							function(event)
							{
								event.stopPropagation ();
								event.preventDefault();
								jLinkCall(this.href, this.rel); 
							}
						);

				$('a.lightbox').lightBox(lightBoxCfg);
				$(document).pngFix();
}

function jLinkComplete(updateClass)	{
	// setFullDisplay('b295' + (updateClass.substr(0, 1).toUpperCase() + updateClass.substr(1)));
				init();

				$('#b295Bdy').unblock();
				
				$('#b295Bdy').animate(
					{'height':$('#b295' + (updateClass.substr(0, 1).toUpperCase() + updateClass.substr(1))).height()},
					'fast', 
					'linear',
					function ()	{
						$('.idTabs').idTabs();
					}
				);
}

// update after json etc
function b295SetContentByClass(bodyClass)	{
				switch(bodyClass)	{
					case 'home':		b295_home(); break;
					case 'events':		b295_events(); break;
					case 'history':		b295_history(); break;
					case 'biogs':		b295_biogs(); break;
					case 'media':		b295_media(); break;
					case 'charts':		b295_charts(); break;
					case 'contact':		b295_contact(); break;
					case 'membership':	b295_membership(); break;
					case 'photos':		b295_photos(); break;
					case 'videos':		b295_videos(); break;
				}
}
function setFullDisplay(newID)	{
			var contHeight = 0;
			var innerHeight = {
				'body':			$('body').innerHeight(),
				'innerWrap':	$('#innerWrap').innerHeight(),
				'outterWrap':	$('#outterWrap').innerHeight(),
				'base':			$('#base').innerHeight(),
				'inBase':		$('#inBase').innerHeight(),
				'introFObj':	$('#introFObj').innerHeight()
			};
			var Height = {
				'body':			$('body').height(),
				'innerWrap':	$('#innerWrap').height(),
				'outterWrap':	$('#outterWrap').height(),
				'base':			$('#base').height(),
				'inBase':		$('#inBase').height(),
				'introFObj':	$('#introFObj').height()
			};

				contHeight = $('#b295Hdr').height() + contHeight;
				contHeight = $('#b295Bar').height() + contHeight;
				contHeight = $('#' + newID).height() + contHeight;
				contHeight = $('#b295Ftr').height() + contHeight;

				$('#innerWrap').height(contHeight);
				$('#outterWrap').height(contHeight);
				$('#base').height(contHeight);
				$('#inBase').height(contHeight);
				$('#introFObj').height(contHeight);
				$('body').height(contHeight);
				// console.info(contHeight);
}

function	b295_home()	{
	// console.info('home 10');

		// home extra box
		var att = { data: siteCfg.path + "/flash/" + b295ExtraBox.swfFile, width: b295ExtraBox.width, height:b295ExtraBox.height, id:"b295HistoryObj" };
		var par = {
			menu: false,
			wmode: "transparent",
			scaleMode: "noScale",
			base: siteCfg.path,
			flashvars: "xmlFile=" + b295ExtraBox.flashvars.xmlFile + "&showGallery=" + b295ExtraBox.flashvars.showGallery + "&contentScale=" + b295ExtraBox.flashvars.contentScale
		};
		if(!('b295Extra' in oc(swfObjs)))	{
			swfobject.createSWF(att, par, "b295Extra");
			swfObjs[swfObjs.length] = 'b295Extra';
			consoleWrite('	extra createSWF created	');
		}
		if(b295HomeEvents)	{
			var att = { data: siteCfg.path + "/flash/" + b295HomeEvents.swfFile, width: b295HomeEvents.width, height:b295HomeEvents.height, id:"b295HistoryObj" };
			var par = {
				menu: false,
				wmode: "transparent",
				scaleMode: "noScale",
				base: siteCfg.path,
				flashvars: "xmlFile=" + b295HomeEvents.flashvars.xmlFile + "&showGallery=" + b295HomeEvents.flashvars.showGallery + "&contentScale=" + b295HomeEvents.flashvars.contentScale
			};
		}
		if(!('b295HomeEvents' in oc(swfObjs)))	{
			if(b295HomeEvents)	{
				swfobject.createSWF(att, par, "b295HomeEvents");
			}
			swfObjs[swfObjs.length] = 'b295HomeEvents';
			consoleWrite('	extra createSWF created	');
		}

		// console.info(swfObj);
}

function	b295_events()	{
}

function	b295_history()	{
		consoleWrite('debug', 'b295_history');
		// history
		var att = { data: siteCfg.path + "/flash/" + b295History.swfFile, width: b295History.width, height:b295History.height, id:"b295HistoryObj" };
		var par = {
			menu: false,
			wmode: "transparent",
			scaleMode: "noScale",
			base: siteCfg.path,
			flashvars: "xmlFile=" + b295History.flashvars.xmlFile + "&showGallery=" + b295History.flashvars.showGallery + "&contentScale=" + b295History.flashvars.contentScale
		};
		swfobject.createSWF(att, par, "b295HistoryFObj");
		swfObjs[swfObjs.length] = 'b295HistoryFObj';
}

function	b295_biogs()	{
	/* $('#biogNavList').Accordion({ 
			event: 'mouseover', 
			active: '.selected', 
			selectedClass: 'active', 
			animated: "bounceslide",
			header: '.head',
			navigation: true
		}).change(function(event, newHeader, oldHeader, newContent, oldContent) { 
			$('<div>' + oldHeader.text() + ' hidden, ' + newHeader.text() + ' shown</div>').appendTo('#log'); 
		}); */

}

function	b295_media()	{
}

function	b295_photos()	{
					// photos
					var att = { data: siteCfg.path + "/flash/" + b295slideshowPlyr.swfFile, width: b295slideshowPlyr.width, height:b295slideshowPlyr.height, id:"b295PhotoGalleryObj" };
					var par = {
						menu: false,
						wmode: "transparent",
						scaleMode: "noScale",
						base: siteCfg.path,
						flashvars: "xmlFile=" + b295slideshowPlyr.flashvars.xmlFile + "&contentScale=" + b295slideshowPlyr.flashvars.contentScale
					};
					swfobject.createSWF(att, par, "b295PhotoGalleryFObj");
					swfObjs[swfObjs.length] = 'b295PhotoGalleryFObj';
}

function	b295_videos()	{
	
					// videos
					var att = { data: siteCfg.path + "/flash/" + b295flvPlyr.swfFile, width: b295flvPlyr.width, height:b295flvPlyr.height, id:"b295flvPlyrObj" };
					var par = {
						menu: false,
						wmode: "opaque",
						scaleMode: "noScale",
						base: siteCfg.path,
						allowscriptaccess: b295flvPlyr.params.allowscriptaccess,
						allowfullscreen: b295flvPlyr.params.allowfullscreen,
						flashvars: "file=" + b295flvPlyr.flashvars.file + 
							"&displayheight=" + b295flvPlyr.flashvars.displayheight + 
							"&height=" + b295flvPlyr.flashvars.height + 
							"&displaywidth=" + b295flvPlyr.flashvars.displaywidth + 
							"&width=" + b295flvPlyr.flashvars.width + 
							"&autoscroll=" + b295flvPlyr.flashvars.autoscroll + 
							"&overstretch=" + b295flvPlyr.flashvars.overstretch + 
							"&shuffle=" + b295flvPlyr.flashvars.shuffle + 
							"&thumbsinplaylist=" + b295flvPlyr.flashvars.thumbsinplaylist
					};
					swfobject.createSWF(att, par, "b295VideoPlayerFObj");
					swfObjs[swfObjs.length] = 'b295VideoPlayerFObj';
					makePaused();
	}

function	b295_charts()	{
	consoleWrite('debug', 'b295_charts');
}

function	b295_contact()	{
									consoleWrite('debug', 'FN b295_contact');
				$('#format').val('json');
				$('#ContactSubmissionForm').submit(function()	{
									consoleWrite('debug', '		contact submit');
					$(this).parent().block('<p>Submitting message</p>');
					$(this).slideUp({
								'duration': 250, 
								'easing': 'easeOutBounce',
								'complete': function ()	{
									$(this).empty();
								}
							});

					// submit the form 
					$(this).ajaxSubmit({ 
						// dataType identifies the expected content type of the server response 
						'dataType': 'json', 
						// success identifies the function to invoke when the server response 
						// has been received 
						'success': b295_ContactSub 
					});
					return false; 
				});
}

function	b295_ContactSub(data)	{
				// 'data' is the json object returned from the server 
				var msgText = '';
					
									consoleWrite('debug', 'FN b295_ContactSub - data:');
									consoleWrite('debug', data);
									if (data.success === true)
									{
										msgText = '<p>Thank You ' + data.name + '</p><p>' + data.message + '</p><p>The a response will be sent to ' + data.email + ' shortly.</p>'
									}	else	{
										msgText = '<p>' + data.name + ' we were unable to send your message. </p><p>' + data.message + '</p>'
									}
					$('#ContactSubmissionForm').html(msgText).slideDown({
								'duration': 250, 
								'easing': 'easeOutBounce',
								'complete': function ()	{
									consoleWrite('debug', 'unblock');
									$('#ContactSubmissionForm').parent().unblock();
								}
							});

					data = false;
}

function	b295_membership()	{
		
}

function blank()	{
				alert('blank');
}

function debug(obj)	{
				var s = "";
				var i = 0;

				for (prop in obj)
				{
					s += prop + ":" + obj[prop] + "\n";
					if (++i % 10 === 0) {
						consol.debug(s);
						s = "";
					}
				};

};

jQuery.clean.convert = function(html) {
				var n = document.createElement('div');
				n.innerHTML = html;
				return n;
};


if(thisPage == 'welcome')	{
	swfobject.embedSWF(siteCfg.path+"/flash/"+b295Intro.swfFile, "introFObj", "100%", "100%", "8.0.0", siteCfg.path+"/flash/expressInstall.swf", b295Intro.flashvars, b295Intro.params, b295Intro.attributes);
} else {
	
	// b295 header
	swfobject.embedSWF(siteCfg.path + "/flash/" + b295Hdr.swfFile, "b295HdrFObj", b295Hdr.width, b295Hdr.height, "8.0.0", false, b295Hdr.flashvars, b295Hdr.params, b295Hdr.attributes);
	if(thisPage == 'home')	{
		swfobject.embedSWF(siteCfg.path+"/flash/"+b295ExtraBox.swfFile,"b295Extra",b295ExtraBox.width,b295ExtraBox.height,"8.0.0",false,b295ExtraBox.flashvars,b295ExtraBox.params,b295ExtraBox.attributes);
		swfObjs[swfObjs.length] = 'b295Extra'; 
		//swfobject.embedSWF(siteCfg.path+"/flash/"+b295HomeEvents.swfFile,"b295HomeEvents",b295HomeEvents.width,b295HomeEvents.height,"8.0.0",false,b295HomeEvents.flashvars,b295HomeEvents.params,b295HomeEvents.attributes);
		//swfObjs[swfObjs.length] = 'b295AlbumAd';
	}
	swfobject.embedSWF(siteCfg.path+"/flash/"+b295Ticker.swfFile,"b295TickerFObj",b295Ticker.width,b295Ticker.height,"8.0.0",false,b295Ticker.flashvars,b295Ticker.params,b295Ticker.attributes);
}	
//	file=/upload/mediaplayer.xml&enablejs=true&javascriptid=mpl&displayheight=240

	//	jQuery docu ready
	$(document).ready(function()	{
		init();
		$('.idTabs').idTabs();
		$('.scroll-pane').jScrollPane({ showArrows : true, scrollbarWidth : 7 });

		if(DEBUG)	{
			$('#b295Ftr').append('<a href="#" id="ieOverflowTest">ie8 test</a>');
			$('#ieOverflowTest').bind('click', function()	{
				alert('ie beta testing');	 
				alert($('.jScrollPaneContainer').css('overflow'));
				$('.jScrollPaneContainer').css('overflow', 'hidden');
			})
		}
	});