// JavaScript Document

//Browser.Plugins.Flash.version
//Browser.Plugins.Flash.build

var correctFlashInstalled = false
if(Browser.Plugins.Flash.version >= 8){
	correctFlashInstalled = true
}

var initIphoneOnly = function(){
	if(!Browser.Platform.ipod){
		var objs = $$('div.iphoneonly')
		
		objs.each(function(item, index){
			item.setStyles({visibility: 'hidden', display: 'none'});
		});
	}
}

var initFlashHeader = function(){
	
	if(correctFlashInstalled){
		
		var headerImages = [];
		
		var objs = $$('div#headertopimage img')
		
		objs.each(function(item, index){
			headerImages.push(item.getProperty('src'))
			//alert(index + " = " + item);
		});
		
		var headerObj = new Swiff('/assets/flash/headershow.swf', {
			id: 'headerShow',
			width: 829,
			height: 217,
			container: 'headertopimage',
			params: {
				wmode: 'transparent',
				salign: 'LT',
				scale: 'noscale',
				bgcolor: '#fff',
				menu: 'false'
			},
			vars: {
				images: headerImages.join(','),
				slideTime: 3000
			},
			callBacks: {
				//load: myOnloadFunc
			}
		});
		
	}	
}

var scaleDelay
var navDelay

var initNavigation = function(){
	
	var objs = $$('#headernavigation ul li')
	for (var i=0; i<objs.length; i++){
		
		var mc = objs[i]
		
		mc.getChildren('a').setProperties({
			//onmouseover: 'javascript:rolloverNavigation(this);',
			//onmouseout: 'javascript:rolloutNavigation(this);'
		});
		
	}
}
window.addEvent('domready', function() {
									 
	favicon.change("/favicon.ico");
	
	/*
	var bgimages = Array('liz.jpg','shirt.jpg'); //'chaps.jpg','liz.jpg','shirt.jpg'
	
	$('bgimage').setProperty('src','/assets/images/backgrounds/'+bgimages[Math.floor(Math.random()*bgimages.length)]);
	*/
	
	var pagename = String(window.location).split('/')[3];
	
	if(pagename == null || pagename == ""){
		pagename = 'home';
	}
	
	$('bgimage').setProperty('src','/assets/images/backgrounds/'+pagename+'.jpg');
	
	$('bgimage').setProperty('style','');
	
	$('bgimage').fade('hide');
	
	if(Browser.Platform.ipod){
		$('bgimage').fade('show');
	}else{
	
		var delaybgimage = setTimeout(function(){
			$('bgimage').fade('in');
		},125);
	
	}
	
	var delaybgimagealt = setTimeout(function(){
		$('bgimage').setProperty('alt','Punk Soho Background Art');
	},1000);
	
	
	initNavigation();
	initIphoneOnly();
	//initFlashHeader();
	
	window.fireEvent('resize');
	
});

var resizeFunction = function(){
	
	var ratio = (999/999);
	
	var docsize = document.getScrollSize();
	var imgsize = $('bgimage').getSize();
	
	var docratio = (docsize.x/docsize.y);
	
	if(ratio > docratio){
		var newWidth = docsize.y*ratio;
		var newHeight = docsize.y;
	}else{
		var newWidth = docsize.x;
		var newHeight = docsize.x/ratio;
	}
	
	if(Browser.Platform.ipod){
	
		$('bgimage').setProperty('width',newWidth);
		$('bgimage').setProperty('height',newHeight);
		
		$('bgimagecontainer').setStyles({width: '100%', height: docsize.y});
		
	}else{
		
		var myEffect = new Fx.Morph('bgimage', {duration: 75, transition: Fx.Transitions.Sine.easeOut});
		
		clearTimeout(scaleDelay);
		
		scaleDelay = setTimeout(function(){
			
			myEffect.start({
				'width': [imgsize.x, newWidth],
				'height': [imgsize.y, newHeight]
			});
			
			$('bgimagecontainer').setStyles({width: '100%', height: docsize.y});
		
		},125);
	}
}

window.addEvent('resize', function() {
									 
	resizeFunction();
	
});

window.addEvent('onresize', function() {
									 
	resizeFunction();
	
});

window.addEvent('scroll', function() {
									 
	resizeFunction();
	
});

window.addEvent('load', function() {
	resizeFunction();						 
});

var rolloverNavigation = function(element){
	
	clearTimeout(navDelay)
	
	$('headernavigation').setStyles({
		'background-position': 'right top'
	});
	
}

var rolloutNavigation = function(element){
	
	
	clearTimeout(navDelay)
	navDelay = setTimeout(function(){
		$('headernavigation').setStyles({
			'background-position': 'right 30px'
		});
	},500);
	
}

var addcalendarentry = function(id){
	//alert("Added:\n"+id)
	window.location = '/lib/calendarentry.php?article='+id;
}
var downloadfile = function(file){
	window.location = file;
}