// jQuery extensions

function toggleTitleText(id) {
	var box = document.getElementById(id);
	
	if (box.value == box.alt) {
		box.value = "";
		box.style.color = "black";
	} else if (box.value == "") {
		box.style.color = "#ccc";
		box.value = box.alt;
	}
}

function filterAdminTitles(query) {
	
	jQuery.expr[':'].contains = function(a,i,m){
	    return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase())>=0;
	};
	
	if (query == "") {
		$("#admin-lib li").show();
		$("#admin-lib li").css("border-bottom", "0");
		$("#admin-lib li:last").css("border-bottom", "1px solid #ebebeb");
	} else {
		$("#admin-lib li").hide();
		$("#admin-lib li:first").show();
		$("#admin-lib li:contains('" + query + "')").show();
		$("#admin-lib li:contains('" + query + "'):last").css("border-bottom", "1px solid #ebebeb");
	}
}

function filterTitles(text) {
	jQuery.expr[':'].contains = function(a,i,m){
	    return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase())>=0;
	};
	
	$("div.scrollable").scrollable().seekTo(0);
	
	if (text == "") {
		$("div.filter-item").show();
	} else {
		$("div.filter-item").hide();
		$("div.filter-item:contains('" + text + "')").show();
	}
}

function searchTitles(query) {
	if (query == "") {
		chooseGenre('All');
		$("div.filter-filters").show();
	} else {
		$("div.filter-filters").hide();
	}
	
	filterTitles(query);	
}

function chooseGenre(genre) {
	
	var siteroot = "http://www.sunworldpictures.com/";
	
	$(".filter-filters a").css("background-image", "none");
	$(".filter-filters a").css("color", "#888");
	$(".filter-filters a:contains('" + genre + "')").css("background-image", "url(" + siteroot + "images/tag-active.png)");
	$(".filter-filters a:contains('" + genre + "')").css("color", "white");
	
	if (genre == "All") {
		filterTitles("");
	} else {
		filterTitles(genre);	
	}
}
			
function scrollList(amount) {
	var api = $("div.scrollable").scrollable();
	if (amount > 0) {
		api.nextPage();	
	} else {
		api.prevPage();	
	}
}

function popupTrailerBox() {
	
	// document.getElementById('page-dim').style.display = 'block';
	// document.getElementById('trailer-window').style.display = 'block';
	
	$('#page-dim').fadeIn('fast');
	$('#trailer-window').fadeIn('fast');
	
	return false;
}

function closeTrailerBox() {

	// document.getElementById('page-dim').style.display = 'none';
	// document.getElementById('trailer-window').style.display = 'none';
	
	$('#page-dim').fadeOut('fast');
	$('#trailer-window').fadeOut('fast');
}

function changeGrad() {
	$("#button-navigation").css("background-image", "url(images/btnnav-bg-white.png)");
}

function changeBox() {
	$('#banner-movie').hide();
	$('#banner-img').hide();
	
	$("#button-navigation").css("background-image", "none");
	$('#banner-img').fadeIn('slow');
	$('#banner-link').attr("href", "http://www.sunworldpictures.com/library.php?film=Minor_Details");
}

function changeBanner() {	
	var $$ = $('#banner-img');
	var target = $$.css("background-image").replace(/^url|[\(\)]|\"/g, "");
	
	$$.fadeOut("slow");
	
	if ($('#banner-second-image').length == 0) {
		
		$$.wrap("<span style=\"position: relative;\" id=\"banner-second-image\"></span>")
			.parent()
			.prepend("<img>")
			.find(":first-child")
			.attr("src", target)
			.hide()
			.fadeIn("slow");
			
		$$.css({
			"position" : "absolute",
			"left" : 0,
			"top" : this.offsetTop
		});
		
	} else {

		$$.parent()
			.find(":first-child")
			.fadeIn("slow")
			.css("opacity", 1.0);
	}
	$('#banner-link').attr("href", "http://www.sunworldpictures.com/library.php?film=Christmas_Angel");
	
	setTimeout("changeBannerBack();", 7000);
}

function changeBannerBack() {
	var $$ = $('#banner-img');

	$$.parent()
		.find(":first-child")
		.animate({
			opacity: 0.0
		}, 600);
				
	$$.fadeIn();
	$('#banner-link').attr("href", "http://www.sunworldpictures.com/library.php?film=Minor_Details");
	
	setTimeout("changeBanner();", 7000);
	
}

function refreshNewsletterForm() {
	$("#newsletter p").html("Sign up for our newsletter");
	$("#newsletter input").css("display", "inline-block");
}

//
//
// STORE FUNCTIONALITY
//
//

function addItemToCart(id, type) {
	$('.product-cartbtn .product-price').hide();
}