var caroShows = 4;
var caroSlotWidth = 110;
var caroImgs = null;

function switchVisibility(sectToHide, sectToShow) {
	$(sectToHide).hide();
	$(sectToShow).show();
}

/**
* Moves the image carousel. If caroDir is negative, it moves to the left.
* If positive, it moved right. The size of caroDir controls how many steps to
* move for each invocation (1 moves carousel one step to the right).
*/
function moveCaro(caroDir) {
	var vp = $("viewPort");
	if( vp.running == true ) {
		return;
	}
	vp.running = true;
	
	if( caroImgs == null ) {
		// find all elements that has the carouselImage class
		caroImgs = $$(".carouselImage");
	}
	
	if( caroDir == -1 ) {
		// scrolling left
		var elem = caroImgs.shift();
		caroImgs.push(elem);
		elem.setStyle({left: (caroImgs.size()*caroSlotWidth)+"px"});
	} else {
		// scrolling right
		var elem = caroImgs.pop();
		caroImgs.unshift(elem);
		elem.setStyle({left: (-2*caroSlotWidth)+"px"});
	}
	
	for( var imgIdx = 0, len = caroImgs.length; imgIdx < len; ++imgIdx ) {
		var newLeft = (imgIdx-1)*caroSlotWidth;
		if( imgIdx > caroShows +1 ) {
			caroImgs[imgIdx].hide();
		} else {
			caroImgs[imgIdx].show();
		}
		new Effect.Move(caroImgs[imgIdx], {x: newLeft, y:0, mode: 'absolute'});
	}
	window.setTimeout(function(){
		$("viewPort").running=false;					   
		  }, 1100);
}

function postForm(form) {
	$(form).submit();
}

function showMenuArrow(arrowId) {
	$(arrowId).show();
}


function hideMenuArrow(arrowId) {
	$(arrowId).hide();
}

function showInlineTrailer(movieUrl, instant, image) {
	var inlineTrailer = $("inlineTrailer");
	var trailerDiv = $("trailerBlock");
	if( trailerDiv.showing != "true" ) {
		$(image).hide();
		trailerDiv.showing = "true";
		var delay = 0;
		if( instant == true ) {
			inlineTrailer.show();
		} else {
			new Effect.BlindDown(inlineTrailer, {duration: 1.0});
			delay = 1;
		}
		
		if( trailerDiv.hasObject != "true" ) {
			trailerDiv.hasObject = "true";
			var inner = '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" width="640" height="450" bgcolor="#000000" codebase="http://www.apple.com/qtactivex/qtplugin.cab">'+
			'<param name="src" value="'+movieUrl+'">'+
			'<param name="bgcolor" value="#000000">'+
			'<param name="pluginspage" value="http://www.apple.com/quicktime/download/">'+
			'<param name="controller" value="true">'+
			'<param name="target" value="myself">'+
			'<param name="href" value="'+movieUrl+'">'+
			'	<embed src="'+movieUrl+'" width="640" height="450" bgcolor="#000000" controller="true" target="myself" pluginspage="http://www.apple.com/quicktime/download/">'+
			'	</embed>'+
			'</object>';

			//objElem.insert(embedElem);
			Element.insert.delay( delay, 'trailerBlock', inner);
			
			
		}
	} 
}

function prepareCarousel() {
	caroImgs = $$(".carouselImage");
	if(caroImgs == null || caroImgs.length < 5) {
		return;
	}
	caroImgs.unshift(caroImgs.pop());
	for(var x = 5; x < caroImgs.length; x++ ) {
		caroImgs[x].hide();
	}
	
	
}

function hideInlineTrailer() {
	var trailerDiv = $("trailerBlock");
	var inlineTrailer = $("inlineTrailer");
	if( trailerDiv.showing == "true" && trailerDiv.hasObject == "true") {
		trailerDiv.showing = "false";
		trailerDiv.innerHTML ="";
		trailerDiv.hasObject = "false";
		new Effect.BlindUp(inlineTrailer, {duration: 1.0});
		$('artTitleButton').show();
	}
}

function changeProgramSort(newSorting) {
	var form = $("daySelector");
	form['sortBy'].value = newSorting;
	postForm(form);
}

function purchase(url) {
	window.open(url, "_blank", "width=717,height=750,resizable=yes");
}

function HM_preparePage() {
		
		var urlElem = $("hiddenTrailerUrl");
		if(urlElem != null ) {
			if(window.location.href.indexOf("showTrailer=true") != -1) {
				showInlineTrailer(urlElem.innerHTML, true, "artTitleButton");
			}
			caroSlotWidth = 189;
		}
		prepareCarousel();
		
}
