window.addEvent('load', function(){
	MochaUI.Modal = new MochaUI.Modal();

	MochaUI.aboutWindow = function(){
		new MochaUI.Window({
			id: 'about_window',
			title: 'OnlineMoviez.com',
			content: $('about-onlinemoviez').innerHTML,
			type: 'modal1',
			width: 500,
			height: 265,
			contentBgColor: '#ffffff',
			padding: {top: 10, right: 12, bottom: 10, left: 12},
			scrollbars:  false
		});
	}
	if ($('about-onlinemoviez-link')) {
		$('about-onlinemoviez-link').addEvent('click', function(e){
			new Event(e).stop();
			MochaUI.aboutWindow();
		});
	}
 
	MochaUI.footerWindow = function(){
		new MochaUI.Window({
			id: 'footer_window',
			title: 'OnlineMoviez.com',
			content: $('footer1').innerHTML,
			type: 'modal1',
			width: 300,
			height: 75,
			contentBgColor: '#ffffff',
			padding: {top: 10, right: 12, bottom: 10, left: 12},
			scrollbars:  false
		});
	}
	if ($('footer-link')) {
		$('footer-link').addEvent('click', function(e){
			new Event(e).stop();
			MochaUI.footerWindow();
		});
	}

	MochaUI.infoWindow = function(){
		new MochaUI.Window({
			id: 'info_window',
			title: 'Oops...',
			content: $('info_message').innerHTML,
			type: 'modal1',
			width: 300,
			height: 125,
			contentBgColor: '#ffffff',
			contentFgColor: '#000000',
			padding: {top: 10, right: 12, bottom: 10, left: 12},
			scrollbars:  false
		});
	}
});

function changeCategory(str, cf) {

	if (str=="") {
		return;
	} 
	if (window.XMLHttpRequest) {
		// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else {
		// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			ajax_response = xmlhttp.responseText;
			$("item_container").innerHTML = ajax_response;

			AddItems(cf);
			RemoveItems(cf);
			$('ajax-loader').setStyle('display', 'none');
			refreshGlobalCaption(cf);
			setTimeout("cf.resize()",2000);
		}
	}
	$('ajax-loader').setStyle('display', 'inline');
	xmlhttp.open("GET","changecategory.php?q="+str,true);
	xmlhttp.send();
}

function RemoveItems(cf) {
	//console.debug("Removing " + numOldItems + " Old Items");	
	for (var i = 0; i < numOldItems; i++) {
		//console.debug(" - Removing " + cf.items[i].caption.innerHTML);
		if(cf.items[0] != undefined && cf.items[0] != null) {
			//console.debug(" - Removing " + cf.items[0].caption.innerHTML);
			cf.rmItem(0);
		}
	}
}

function AddItems(cf) {

	numOldItems = cf.getNumberOfItems();

	// Get the new items from the container
	// Note: The childElementCount property is only supported in Firefox from version 3.5 and 
	// Internet Explorer from version 9.  For now use Mootools for IE engine detection (trident)
	var ic = $('item_container');
	var numItems = Browser.Engine.name == 'trident' ? ic.childNodes.length : ic.childElementCount;
	//alert(numItems);
	var newItems = new Array();

	for (i = 0; i < numItems; i++) {
		newItems[i] = ic.children[i];
	}

	//console.debug("Adding " + numItems + " New Items");
	for (i = 0; i < numItems; i++) {
		//console.debug(" + Adding item");
		cf.addItem(newItems[i], 'last');
	}

	return numItems;
}

function refreshGlobalCaption(cf) {

	// Set the global caption
	cf.setGlobalCaption();
}

function searchForMovie(cf) {
	// Get the search string from the form
	var str = document.movie_search.search_box.value;

	// Return if there is no search string
	if (str=="") {
		return;
	}
	
	// Character substitutions
	str = str.replace("&", " and ");
	str = str.replace("+", " and ");

	if (window.XMLHttpRequest) {
		// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else {
		// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			ajax_response = xmlhttp.responseText;
			if(ajax_response.indexOf("Could not find any") != -1) {
				$('info_message_text').innerHTML = ajax_response;
				MochaUI.infoWindow();
			}
			else {
				$('item_container').innerHTML = ajax_response;
				//$("movie_carousels").innerHTML = ajax_response;

				numNewItems = AddItems(cf);
				RemoveItems(cf);
				$('ajax-loader').setStyle('display', 'none');
				refreshGlobalCaption(cf);
				//setTimeout("cf.moveTo('next')",2000);
				setTimeout("cf.resize()",2000);
			}
			$('ajax-loader').setStyle('display', 'none');
			// Don't start on a padded item
//			if(numNewItems == 9 || numNewItems == 7 || numNewItems == 5 || numNewItems == 3) {
//				cf.moveTo('next');
//			}
		}
	}
	$('ajax-loader').setStyle('display', 'inline');
	xmlhttp.open("GET","searchformovie.php?q="+str,true);
	xmlhttp.send();
}
