var isIE6 = Browser.Engine.trident && Browser.Engine.version==4;

// fade in from 0 to 'opacity'
function objFadeIn (el, opacity, speed, frames) {
	if( !$defined(el) ) return false;
	if( !$chk(opacity) ) opacity = 1;
	if( !$chk(speed) ) speed = 500;
	if( !$chk(frames) ) frames = 30;
	
	$(el).setStyle('display','block');
	var fade = new Fx.Tween(el, {property:'opacity', duration:speed, fps:frames});
	fade.start('0', opacity);
}
// fade out from 'opacity' to 0
function objFadeOut (el, opacity, speed, frames) {
	if( !$defined(el) ) return false;
	if( !$chk(opacity) ) opacity = 1;
	if( !$chk(speed) ) speed = 500;
	if( !$chk(frames) ) frames = 30;
	
	var fade = new Fx.Tween(el, {property:'opacity', duration:speed, fps:frames});
	fade.start(opacity, '0');
	setTimeout( function(){ $(el).setStyle('display','none'); }, speed );
}

function loadClip(id, fileName, firstFrameName, width, height) {
	if( !$defined(id) || !$(id) ) return false;
	if( !$chk(width) ) width = 512;
	if( !$chk(height) ) height = 384;
	var str = '';
		str += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553544400" width="'+width+'" height="'+height+'" id="fplayer" ';
		str += 'codebase="http://download.macromedia.com/pub/shockwave/cabs/content/swflash.cab#version=8,0,0,0">';
		str += '<param name="allowFullScreen" value="true" />';
		str += '<param name="movie" value="content/fplayer.swf" />';
		str += '<param name="FlashVars" value="moviefile=clips/' + fileName + '&clipframe=content/clips/' + firstFrameName + '" />';
		str += '<param name="quality" value="high" />';
		str += '<param name="wmode" value="transparent" />';
		str += '<param name="bgcolor" value="#ffffff" />';
		str += '<param name="scale" value="exactFit" />';
		str += '<embed src="content/fplayer.swf" ';
		str += 'FlashVars="moviefile=clips/' + fileName + '&clipframe=content/clips/' + firstFrameName + '" ';
		str += 'quality="high" scale="exactFit" wmode="transparent" ';
		str += 'bgcolor="#ffffff" width="'+width+'" height="'+height+'" name="fplayer" ';
		str += 'allowFullScreen="true" type="application/x-shockwave-flash" ';
		str += 'pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
	$(id).set('html',str);
}

function setupScrollbox( boxId, leftArrowId, rightArrowId, boxWidth, scrollSpeed, scrollLimit, fadeArrows )
{
	// grab a handle on all objects and make sure they exist
	var box = $(boxId);
	var leftArrow = $(leftArrowId);
	var rightArrow = $(rightArrowId);
	if (!box || !leftArrow || !rightArrow) return false;
	
	// if they all exist proceed with setting up the scroll box
	var scrollEffect = new Fx.Scroll(box, {
		wait:false,
		duration:scrollSpeed,
		transition:Fx.Transitions.Quad.easeInOut,
		wheelStops:false,
		fps:30
	});
	
	// and setup the arrows
	var scrollcounter = 0;
	leftArrow.addEvent('click', function(e) {
		if (fadeArrows && scrollcounter==scrollLimit) objFadeIn(rightArrow);
		// decrement the counter and scroll to position of the image
		if (scrollcounter > 0) {
			scrollcounter = scrollcounter - 1;
			scrollEffect.start( boxWidth*scrollcounter, 0);
			if (fadeArrows && scrollcounter==0) setTimeout( function(){ objFadeOut(leftArrow) }, scrollSpeed/2 );
		}
		return false;
	});
	
	rightArrow.addEvent('click', function(e) {
		if (fadeArrows && scrollcounter==0) objFadeIn(leftArrow);
		if (scrollcounter < scrollLimit) {
			scrollcounter = scrollcounter + 1;
			scrollEffect.start( boxWidth*scrollcounter, 0 );
			if (fadeArrows && scrollcounter==scrollLimit) setTimeout( function(){ objFadeOut(rightArrow) }, scrollSpeed/2 );
		}
		return false;
	});
	return true;
}

function setCharAt(string, index, character) {
	if (index > string.length-1) return string;
	return string.substr(0,index) + character + string.substr(index+1);
}

function getRatedState(rating_el, cookie) {
	if (!$defined(rating_el) || isIE6) return false;
	return Cookie.read(cookie).charAt( rating_el.charAt(6).toInt()-1 ) == 1;
}

function setRatedState(rating_el, cookie) {
	if (!$defined(rating_el) || isIE6) return false;
	
	var rateRecord = Cookie.read(cookie);
	if( !rateRecord ) return false;
	
	Cookie.dispose(cookie);	// clear the cookie
	// change character to indicate set was rated
	rateRecord = setCharAt( rateRecord, rating_el.charAt(6).toInt()-1, '1' );
	// put the updated value into cookie
	Cookie.write(cookie, rateRecord, {path: "/", duration:365});
}
