/**************************************************************************
* video functions
**************************************************************************/
function writeVideo(width, height, file, image, playerid, containerid, start) {
	wv(width, height, "/assets/media/" + file + ".flv", image, playerid, containerid, start);
}

function wv(width, height, file, image, playerid, containerid, start) {
    var as = "false";
    if (start) {
        as = "true";
    } 
	var flashvars = {
		backcolor: "0xffffff"
		, frontcolor: "0x000000"
		, width: width
		, height: height
		, file: file
		, image: image
		, javascriptid: playerid
		, enablejs: "true"
		, autostart: as};
	var flashparams = {
		wmode: "transparent"
		, allowfullscreen: "true"
		, allowscriptaccess: "always"};
		
	swfobject.embedSWF("scripts/player.swf", containerid, width, height, "8.0.0", false, flashvars, flashparams, {});
        
	swfobject.addDomLoadEvent(function() {
	    if (document.forms[0]) {
		    window[containerid] = document.forms[0][containerid];
		} else {
		    window[containerid] = document.getElementById(containerid);
		}
	});
	//$(containerid).writeAttribute('name', containerid);
}

function switchVideos(video) {
	$$('.video.selected').each(function(s) {
		s.removeClassName('selected');
	});
	$(video).addClassName('selected');
	$('current-video').down('h2').innerHTML = $(video).down('.title').innerHTML;
	$('current-video').down('.video-list-content').innerHTML = $(video).down('.content').innerHTML;
	
	
	$('flash_container').sendEvent('load', {file: $(video).down('.video-file').innerHTML, image: $(video).down('.preview').src});
}

function thisObject(swf) {
	if(navigator.appName.indexOf("Microsoft") != -1) {
		return window[swf];
	} else {
		return document[swf];
	}
}
/**************************************************************************
* popup images (on product pages)
**************************************************************************/
var PI = {
	_hide: false
	, _delay: 50
	, showPopImg: function(img) {
		PI._hide = false;
		if (!$('pop-img')) {
			$('container').insert({before: '<img id="pop-img" src="'+img+'" alt="" style="position:absolute;z-index:1001;left:500px;top:383px;" />'});
			document.onmousemove = PI._movePopImg;
			$('pop-img').onmouseover = PI.showPopImg;
			$('pop-img').onmouseout = PI.hidePopImg;
			
		}
	
	}, hidePopImg: function() {
		PI._hide = true;
		window.setTimeout('PI._hidePop()', PI._delay);
	}, _movePopImg: function(e) {
		var x,y = 0;
		
		if ($('pop-img')) {
			var width = document.viewport.getDimensions().width + document.viewport.getScrollOffsets().left;
			width = width - $('pop-img').width;
			
			var height = document.viewport.getDimensions().height + document.viewport.getScrollOffsets().top;
			height = height - $('pop-img').height;
		
			if (e) { y = Event.pointerY(e); x = Event.pointerX(e); } 
			else { y = Event.pointerY(event); x = Event.pointerX(event); }
			
			x = Math.min(x + 10, width);
			y = Math.min(y + 10, height);
			
			$('pop-img').setStyle('left: '+ x +'px; top: '+ y +'px;');
		}
	}, _hidePop: function() {
		if (PI._hide) {
			$('pop-img').remove();
		}
	}
};
/**************************************************************************
* poster switcher
**************************************************************************/
var PS = {
	posterWidth: 210 			// includes padding/margins
	, wrap: true 				// when we get to the end of one side, wrap posters from the other side
	, visiblePosters: 4			// number of posters visible at a time
	, slideBy: 4 				// number of posters to slide in on each slide
	, speed: .3					// time (in seconds) for each item scrolled
	, _moving: false
	, _curLeft: 0
	, _posterCount: 0
	, _containerWidth: 0
	, _pc: false
	, _sl: false
	, _sr: false
	, init: function(postersContainer, slideLeft, slideRight, visible, slide, width) {
		// check poster numbers - if greater than visiblePosters, add functionality to both arrows, otherwise hide arrows
		PS._pc = postersContainer;
		PS._sl = slideLeft;
		PS._sr = slideRight;
		PS.visiblePosters = visible;
		PS.slideBy = slide;
		PS.posterWidth = width;
		
		if (PS._pc && PS._sl && PS._sr) {			
			PS._posterCount = PS._pc.childElements().length;
			
			if (PS._posterCount > PS.visiblePosters) {
				PS._sl.onclick = PS.slide_left;
				PS._sr.onclick = PS.slide_right;
				
				PS._containerWidth = PS._posterCount * PS.posterWidth;
				PS._pc.setStyle('width:' + PS._containerWidth + 'px;');
			} else {
				PS._sl.setStyle('visibility:hidden;');
				PS._sr.setStyle('visibility:hidden;');
			}
		} else { 
			// fail
			alert('postersContainer, slideLeft, and slideRight must all exist!');
		}
	}, slide_right: function() {
		if (!PS._moving) {
			// update curLeft, moving
					
			// how many posters are there hidden on the right?
			var buffer = PS._posterCount - PS.visiblePosters - PS._curLeft;
			
			// how many do we want to slide by?
			sb = Math.min(PS._posterCount - PS.visiblePosters, PS.slideBy);
			
			PS._moving = true;
			if (buffer < sb) {
				// no room to slide, unless we're wrapping
				if (PS.wrap) {
					// need to move some posters from the beginning to the end
					
					// ok, move sb posters to the beginning, and instantly shift our list
					for (var i = 0, j = sb - buffer; i < j; ++i) {
						PS._pc.insert({bottom:PS._pc.childElements()[0]});
					}
					PS._curLeft -= (sb - buffer);
					PS._pc.setStyle('left: -' + ((PS._curLeft) * PS.posterWidth) + 'px;'); // we can put this in the loop if it looks fucked up					
				} else {
					PS._moving = false;
					return; // can't slide - we're done
				}
			}
			PS._curLeft += sb;
			PS._pc.visualEffect('move', {x: (-1 * (sb * PS.posterWidth)), y:0, mode:'relative', duration:PS.speed * sb, afterFinish: function() { PS._moving = false; } });			
		}
	}, slide_left: function() {
		if (!PS._moving) {
			// update curLeft, moving
					
			// how many posters are there hidden on the left?
			var buffer = PS._curLeft;
			
			// how many do we want to slide by?
			sb = Math.min(PS._posterCount - PS.visiblePosters, PS.slideBy);
			
			PS._moving = true;
			if (buffer < sb) {
				// no room to slide, unless we're wrapping
				if (PS.wrap) {
					// need to move some posters from the beginning to the end
					
					// ok, move sb posters to the end, and instantly shift our list
					for (var i = 0, j = sb - buffer; i < j; ++i) {
						PS._pc.insert({top:PS._pc.childElements()[PS._posterCount - 1]});
					}
					PS._curLeft += (sb - buffer);
					PS._pc.setStyle('left: -' + ((PS._curLeft) * PS.posterWidth) + 'px;'); // we can put this in the loop if it looks fucked up					
				} else {
					PS._moving = false;
					return; // can't slide - we're done
				}
			}
			PS._curLeft -= sb;
			PS._pc.visualEffect('move', {x: (sb * PS.posterWidth), y:0, mode:'relative', duration:PS.speed * sb, afterFinish: function() { PS._moving = false; } });			
		}
	}
};

/**************************************************************************
* front page video / image switcher
**************************************************************************/
var FeatureSwitcher = {
	_auto_switch_timer: 6000 // time between switches in ms
	, _fade_timer: 1 // total time for a switch (fade from one to another) in seconds
	, _max_top_features: 4
	, _hidden: ''
	, _shown: ''
	, _cur_feature: 0
	, _features: [] 
	, _feature_cycle: -1
	, _feature_timeout: -1
	, _moving: 0
	, init: function(features, hidden, shown) {
		FeatureSwitcher._hidden = hidden;
		FeatureSwitcher._shown = shown;
		FeatureSwitcher._features = features;
		
		var j = FeatureSwitcher._features.length
		// set up the switching numbers on the right side
		for (var i = 0; i < j; ++i) {
			// preload images
			FeatureSwitcher._hidden.down('img').src = FeatureSwitcher._features[i].img;
			
			$('banner').down('ul.tabs').insert({bottom:
				'<li><a href="#" onclick="return FeatureSwitcher.manualChange('+i+');">'+(i+1)+'</a></li>'});
		}
		
		$('banner').down('ul.tabs').insert({bottom:
			'<li><a href="#" onclick="return FeatureSwitcher.manualChange('+j+');">'+(j+1)+'</a></li>'});

		// set up the first feature - set the image with no effects
		$('banner').down('ul.tabs').down().addClassName('selected');
		FeatureSwitcher._shown.down('img').src = FeatureSwitcher._features[FeatureSwitcher._cur_feature].img;
		FeatureSwitcher._shown.down('a').href = FeatureSwitcher._features[FeatureSwitcher._cur_feature].href;
		FeatureSwitcher._shown.show();
		
		// start the switcher
		if (FeatureSwitcher._auto_switch_timer > (FeatureSwitcher._fade_timer * 1000)) {
			if (FeatureSwitcher._features.length > 1) {
				FeatureSwitcher._hidden.down('img').src = FeatureSwitcher._features[1].img;
				FeatureSwitcher._hidden.down('a').href = FeatureSwitcher._features[1].href;
				FeatureSwitcher._feature_cycle = window.setInterval(FeatureSwitcher.change, FeatureSwitcher._auto_switch_timer);	
			} else {
				FeatureSwitcher._hidden.remove();
			}
		}
	}, change: function() {
		// increment / switch
		FeatureSwitcher._cur_feature += 1;
		var s = FeatureSwitcher._shown;
		FeatureSwitcher._shown = FeatureSwitcher._hidden;
		FeatureSwitcher._hidden = s;
		
		// buttons
		$('banner').down('ul.tabs .selected').removeClassName('selected');
		$('banner').down('ul.tabs').childElements()[FeatureSwitcher._cur_feature].addClassName('selected');
		
		if (FeatureSwitcher._cur_feature < FeatureSwitcher._features.length) {
			// instead of changing the attributes of existing DOM items, we're replacing them all together - this fixes a strange "feature" of firefox, which doesn't update these attributes right away (flash of an old image before the fade of the real image starts)
			FeatureSwitcher._shown.update('<a href="'+FeatureSwitcher._features[FeatureSwitcher._cur_feature].href+'"><img src="'+FeatureSwitcher._features[FeatureSwitcher._cur_feature].img+'" alt="Featured Item" /></a>');
			//FeatureSwitcher._shown.down('img').src = FeatureSwitcher._features[FeatureSwitcher._cur_feature].img;
			//FeatureSwitcher._shown.down('a').href = FeatureSwitcher._features[FeatureSwitcher._cur_feature].href;			
			FeatureSwitcher._shown.appear({duration:FeatureSwitcher._fade_timer
				, beforeStart: function() { FeatureSwitcher._moving += 1; }
				, afterFinish: function() { FeatureSwitcher._moving -= 1; }
			});
		} else {
			// stop when we get to the movie
			$('video').appear({duration:FeatureSwitcher._fade_timer
				, beforeStart: function() { FeatureSwitcher._moving += 1; }
				, afterFinish: function() { FeatureSwitcher._moving -= 1; }
			});
			window.clearTimeout(FeatureSwitcher._feature_timeout);
			window.clearInterval(FeatureSwitcher._feature_cycle);
		}
		
		if (FeatureSwitcher._shown.hasClassName('already-hidden')) {
			$('video').fade({duration:FeatureSwitcher._fade_timer
				, beforeStart: function() { FeatureSwitcher._moving += 1; }
				, afterFinish: function() { FeatureSwitcher._moving -= 1; }
			});
			FeatureSwitcher._shown.removeClassName('already-hidden');
		} else {
			FeatureSwitcher._hidden.fade({duration:FeatureSwitcher._fade_timer
				, beforeStart: function() { FeatureSwitcher._moving += 1; }
				, afterFinish: function() { FeatureSwitcher._moving -= 1; }
			});
		}
	}, manualChange: function(index) {
		// stop the cycle and switch to the given project, if we aren't in the middle of a switch
		if (FeatureSwitcher._moving == 0) {		
			window.clearTimeout(FeatureSwitcher._feature_timeout);
			window.clearInterval(FeatureSwitcher._feature_cycle);	
			if (FeatureSwitcher._cur_feature == FeatureSwitcher._features.length) {
				FeatureSwitcher._hidden.addClassName('already-hidden');
			}			
			FeatureSwitcher._cur_feature = index - 1; // this gets incremented
			FeatureSwitcher.change();
			/*if (FeatureSwitcher._cur_feature == FeatureSwitcher._features.length) {
				FeatureSwitcher._feature_timeout = window.setTimeout('FeatureSwitcher._feature_cycle = window.setInterval(FeatureSwitcher.change, ' + FeatureSwitcher._auto_switch_timer + ');', FeatureSwitcher._auto_switch_timer * 2);
			}*/
		}
		return false;
	}
};