function toggle() {
	var ele = document.getElementById("toggleText");
	var text = document.getElementById("preview");
	if(ele.style.display == "block") {
    		ele.style.display = "none";
		text.innerHTML = "Preview";
  	}
	else {
		ele.style.display = "block";
		text.innerHTML = "Close Preview";
	}
}

function hideAll() {
	document.getElementById("filter_category_left").style.display = "none";
	document.getElementById("filter_category_right").style.display = "none";
	document.getElementById("filter_category_left_straight").style.display = "none";
	document.getElementById("filter_category_right_straight").style.display = "none";
	document.getElementById("filter_category_left_gay").style.display = "none";
	document.getElementById("filter_category_right_gay").style.display = "none";
	
	document.getElementById("filter_star_left").style.display = "none";
	document.getElementById("filter_star_right").style.display = "none";
	document.getElementById("filter_star_left_straight").style.display = "none";
	document.getElementById("filter_star_right_straight").style.display = "none";
	document.getElementById("filter_star_left_gay").style.display = "none";
	document.getElementById("filter_star_right_gay").style.display = "none";
	
	document.getElementById("filter_studio_left").style.display = "none";
	document.getElementById("filter_studio_right").style.display = "none";
	document.getElementById("filter_studio_left_straight").style.display = "none";
	document.getElementById("filter_studio_right_straight").style.display = "none";
	document.getElementById("filter_studio_left_gay").style.display = "none";
	document.getElementById("filter_studio_right_gay").style.display = "none";
}

function orientationChange() {
	var allOrientation = document.getElementById("orientation_0").checked;
	var straightOrientation = document.getElementById("orientation_101").checked;
	var gayOrientation = document.getElementById("orientation_102").checked;
	
	index = document.getElementById('filter').selectedIndex;
	name = document.getElementById('filter').options[index].text;
	eleValue = document.getElementById('filter').options[index].value;

	hideAll();
	
	if(allOrientation == true) {
		document.getElementById("filter_" + eleValue + "_left").style.display = "block";
		document.getElementById("filter_" + eleValue + "_right").style.display = "block";
	}
	if(straightOrientation == true) {
		document.getElementById("filter_" + eleValue + "_left_straight").style.display = "block";
		document.getElementById("filter_" + eleValue + "_right_straight").style.display = "block";
	}
	
	if(gayOrientation == true) {
		document.getElementById("filter_" + eleValue + "_left_gay").style.display = "block";
		document.getElementById("filter_" + eleValue + "_right_gay").style.display = "block";
	}

	$("#filter_list_stars_ul_str").html("");
	$("#filter_list_stars_ul_gay").html("");
 	$("#filter_list_stars_ul_all").html("");
        starList = document.getElementById('starList');
	studioList = document.getElementById('studioList');
	starList.value = "";
	studioList.value = "";	
}

function filterChange() {
	hideAll();
	orientationChange();
	$.post('/ajax/clearListInformation',
			{},
			function(result) {
			}
		);
	index = document.getElementById('filter').selectedIndex;
	name = document.getElementById('filter').options[index].text;
	eleValue = document.getElementById('filter').options[index].value;
}

function customTheaterChange() {
	var tid = document.getElementById('affiliateTheater').value;
	if(tid == "") {
		var tid = document.getElementById('destinationSelection').value;
	}
	$.post('/ajax/gettheaterorientation',
	{ 'tid':tid },
		// when the Web server responds to the request
		function(result) {
		  if(result == '102') {
			document.getElementById("orientation_0").checked   = false;
			document.getElementById("orientation_101").checked = false;
			document.getElementById("orientation_102").checked = true;		  
		  } else {
			document.getElementById("orientation_0").checked   = false;
			document.getElementById("orientation_101").checked = true;
			document.getElementById("orientation_102").checked = false;
		  }
		  orientationChange();
		  filterChange();
		}
	);
}

function selectDestination() {
	if($('input[name=destination]:checked').val() == 0) {  // aebn theaters
		$('#customTemplatesContainer').hide();
		$('#aebnDestinationContainer').show();
	}
	else {  // affiliate templates
		$('#aebnDestinationContainer').hide();
		$('#customTemplatesContainer').show();
	}
}

function getlist() {
	if(document.getElementById('orientation_102').checked == true) {
		var orientation = '102';
	} else {
		var orientation = '101';
	}

	if(document.getElementById('orientation_0').checked == true) {
		var categoryS = document.getElementById('categorySAll').value;
		var categoryG = document.getElementById('categoryGAll').value;
	} else {
		var categoryS     = document.getElementById('categoryS').value;
		var categoryG     = document.getElementById('categoryG').value;
	}
	
	var campaign      = document.getElementById('affiliateCampaign').value;
	var aebnTheater   = document.getElementById('destinationSelection').value;
	var customTheater = document.getElementById('affiliateTheater').value;
	var filter        = document.getElementById('filter').value;
	var starList      = document.getElementById('starList').value;
	var studioList    = document.getElementById('studioList').value;
	var titleList     = document.getElementById('titleList').value;
	var size          = document.getElementById('size').value;
	var effect        = document.getElementById('effect').value;
	var color         = document.getElementById('color').value;
	var txtColor      = document.getElementById('color2').value;
	var lkColor       = document.getElementById('color3').value;
	var brColor       = document.getElementById('color4').value;
	var borderSize    = document.getElementById('borderSize').value;
	var affId         = document.getElementById('affId').value;
	
	if(aebnTheater == "" && customTheater == "") { alert ("You must select a theater!"); }
	else if(filter == "category" && categoryS == "0" && categoryG == "0") { alert ("You must select a category!"); }
	else if(filter == "star" && starList == "") { alert ("You must select at least one (1) star!"); }
	else if(filter == "studio" && studioList == "") { alert ("You must select at least one (1) studio!"); }
	else if(filter == "title" && titleList == "") { alert ("You must select at least one (1) title!"); }
	else {
		$.post('/ajax/generateLinkingCode',
			{ 'campaign':campaign, 'aebnTheater':aebnTheater, 'customTheater':customTheater, 'orientation':orientation, 'filter':filter, 'categoryS':categoryS, 'categoryG':categoryG, 'size':size, 'effect':effect, 'color':color, 'txtColor':txtColor, 'lkColor':lkColor, 'brColor':brColor, 'borderSize':borderSize, 'starList':starList, 'studioList':studioList, 'titleList':titleList, 'affId':affId},
			// when the Web server responds to the request
			function(result) {
				$("textarea#textareaCode").val(result);
				$("#various1").click();
			}
		);
	}
}

function savecurrent(id, name) {
	$.post('/ajax/saveListInformation',
		{'id':id, 'name':name },
		function(result) {
		}
	);
}

function addStar() {
	var allOrientation = document.getElementById("orientation_0").checked;
	var straightOrientation = document.getElementById("orientation_101").checked;
	var gayOrientation = document.getElementById("orientation_102").checked;

	if(allOrientation == true) {
		index = document.getElementById('allStars').selectedIndex;
		name = document.getElementById('allStars').options[index].text;
		value = document.getElementById('allStars').options[index].value;
		box = "_all";
	}

	if(straightOrientation == true) {
		index = document.getElementById('strStars').selectedIndex;
		name = document.getElementById('strStars').options[index].text;
		value = document.getElementById('strStars').options[index].value;
		box = "_str";
	}

	if(gayOrientation == true) {
		index = document.getElementById('gayStars').selectedIndex;
		name = document.getElementById('gayStars').options[index].text;
		value = document.getElementById('gayStars').options[index].value;
		box = "_gay";
	}

	starList = document.getElementById('starList');
	if(starList.value.indexOf(value) != -1) {
		alert("This star has already been added.");
	} else {
 		if(value.indexOf('0') != 0) { 
			$("<li id=\""+value+"\"><span style=\"color: black;\">" + name + "</span><a style=\"cursor: pointer;\" onclick=\"removeStar("+value+");\" title=\"Remove\" class=\"right\">X</a></li>").appendTo($("#filter_list_stars_ul"+box))
			if(starList.value == "") {
				starList.value = value;
			} else {
				starList.value = starList.value + "|" + value;	
			}
			savecurrent(value, name);
		}
	}
}
function removeStar(name) {
	$("#"+name+"").remove();
	starList = document.getElementById('starList');
	starListNew = starList.value.replace(name + "|", "");
	starListNew = starListNew.replace("|" + name, "");
	starListNew = starListNew.replace(name, "");
	starList.value = starListNew;
}
function addStudio() {
	
	var allOrientation = document.getElementById("orientation_0").checked;
	var straightOrientation = document.getElementById("orientation_101").checked;
	var gayOrientation = document.getElementById("orientation_102").checked;

	if(allOrientation == true) {
		index = document.getElementById('allStudios').selectedIndex;
		name = document.getElementById('allStudios').options[index].text;
		value = document.getElementById('allStudios').options[index].value;
		box = "_all";
	}

	if(straightOrientation == true) {
		index = document.getElementById('strStudios').selectedIndex;
		name = document.getElementById('strStudios').options[index].text;
		value = document.getElementById('strStudios').options[index].value;
		box = "_str";
	}

	if(gayOrientation == true) {
		index = document.getElementById('gayStudios').selectedIndex;
		name = document.getElementById('gayStudios').options[index].text;
		value = document.getElementById('gayStudios').options[index].value;
		box = "_gay";
	}
	
	studioList = document.getElementById('studioList');
	if(studioList.value.indexOf(value) != -1) {
		alert("This studio has already been added or you did not select a proper studio.");
	} else {
		if(value.indexOf('0') != 0) {
			$("<li id=\""+value+"\"><span style=\"color: black;\">" + name + "</span><a style=\"cursor: pointer;\" onclick=\"removeStudio("+value+");\" title=\"Remove\" class=\"right\">X</a></li>").appendTo($("#filter_list_studio_ul"+box))
		
			if(studioList.value == "") {
				studioList.value = value;
			} else {
				studioList.value = studioList.value + "|" + value;	
			}
			savecurrent(value, name);
		}
	}
}
function removeStudio(name) {
	$("#"+name+"").remove();
        studioList = document.getElementById('studioList');
        studioListNew = studioList.value.replace(name + "|", "");
        studioListNew = studioListNew.replace("|" + name, "");
        studioListNew = studioListNew.replace(name, "");
        studioList.value = studioListNew;
}

function resetStuff() {
	$("#affiliateCampaign option:first").attr('selected', true);
	$("#destinationSelection option:first").attr('selected', true);
	$("#filter option:first").attr('selected', true);
	$("#size option:first").attr('selected', true);
	$("#effect option:first").attr('selected', true);
	$("#borderSize option:first").attr('selected', true);
	
	$("#categorySAll option:first").attr('selected', true);
	$("#categoryGAll option:first").attr('selected', true);
	$("#categoryS option:first").attr('selected', true);
	$("#categoryG option:first").attr('selected', true);

	$("#allStars option:first").attr('selected', true);
	$("#strStars option:first").attr('selected', true);
	$("#gayStars option:first").attr('selected', true);
	
	$("#allStudios option:first").attr('selected', true);
	$("#strStudios option:first").attr('selected', true);
	$("#gayStudios option:first").attr('selected', true);
	
	$("#color").val('#FFFFFF');
	$("#color2").val('#000000');
	$("#color3").val('#0000FF');
	$("#color4").val('#000000');
	
	$("#color").keyup();
	$("#color2").keyup();
	$("#color3").keyup();
	$("#color4").keyup();
	
	$("#orientation_0").attr('checked', false);
	$("#orientation_101").attr('checked', true);
	$("#orientation_102").attr('checked', false);
	
	orientationChange();
	filterChange();
}


function getStudioInformation(letter) {	
	var allOrientation = document.getElementById("orientation_0").checked;
	var straightOrientation = document.getElementById("orientation_101").checked;
	var gayOrientation = document.getElementById("orientation_102").checked;

	$("#allStudios").html("<option>Loading...Please Wait</option>");
	$("#strStudios").html("<option>Loading...Please Wait</option>");
	$("#gayStudios").html("<option>Loading...Please Wait</option>");

	document.getElementById("allStudios").disabled = true;
	document.getElementById("strStudios").disabled = true;
	document.getElementById("gayStudios").disabled = true;
	
	if(allOrientation == true) {
		type = 'all';
	}

	if(straightOrientation == true) {
		type = 'str';
	}

	if(gayOrientation == true) {
		type = 'gay';
	}
	
	$.post('/ajax/getStudios',
		{'type':type, 'letter':letter},
		function(result) {
			$('#allStudios').html(result);
			$('#strStudios').html(result);
			$('#gayStudios').html(result);
			document.getElementById("allStudios").disabled = false;
			document.getElementById("strStudios").disabled = false;
			document.getElementById("gayStudios").disabled = false;
		}
	);
}

function getStarInformation(letter) {
	var allOrientation = document.getElementById("orientation_0").checked;
	var straightOrientation = document.getElementById("orientation_101").checked;
	var gayOrientation = document.getElementById("orientation_102").checked;

	$("#allStars").html("<option>Loading...Please Wait</option>");
	$("#strStars").html("<option>Loading...Please Wait</option>");
	$("#gayStars").html("<option>Loading...Please Wait</option>");

	document.getElementById("allStars").disabled = true;
	document.getElementById("strStars").disabled = true;
	document.getElementById("gayStars").disabled = true;
	
	if(allOrientation == true) {
		type = 'all';
	}

	if(straightOrientation == true) {
		type = 'str';
	}

	if(gayOrientation == true) {
		type = 'gay';
	}
	
	$.post('/ajax/getStars',
		{'type':type, 'letter':letter},
		function(result) {
			$('#allStars').html(result);
			$('#strStars').html(result);
			$('#gayStars').html(result);
			document.getElementById("allStars").disabled = false;
			document.getElementById("strStars").disabled = false;
			document.getElementById("gayStars").disabled = false;
		}
	);
}

$(document).ready(function() {
	
	$("#color").bind('cut paste', function(e) { window.setTimeout(function() { $("#color").keyup(); $("#color").change(); }, 1); });
	$("#color2").bind('cut paste', function(e) { window.setTimeout(function() { $("#color2").keyup(); $("#color2").change(); }, 1); });
	$("#color3").bind('cut paste', function(e) { window.setTimeout(function() { $("#color3").keyup(); $("#color3").change(); }, 1); });
	$("#color4").bind('cut paste', function(e) { window.setTimeout(function() { $("#color4").keyup(); $("#color4").change(); }, 1); });
	
	$("#colorpicker").farbtastic("#color");
	$("#colorpicker2").farbtastic("#color2");
	$("#colorpicker3").farbtastic("#color3");
	$("#colorpicker4").farbtastic("#color4");

	$("#colorpicker").show();
	$("#colorpicker2").hide();
	$("#colorpicker3").hide();
	$("#colorpicker4").hide();

	// binding click functionality to the color selectors
	$('#color').click(function() {
		$("#colorpicker2").hide();
		$("#colorpicker3").hide();
		$("#colorpicker4").hide();
		$('#colorpicker').show();
	});
	$('#color2').click(function() {
		$("#colorpicker").hide();
		$("#colorpicker3").hide();
		$("#colorpicker4").hide();
		$('#colorpicker2').show();
	});
	$('#color3').click(function() {
		$("#colorpicker").hide();
		$("#colorpicker2").hide();
		$("#colorpicker4").hide();
		$('#colorpicker3').show();
	});
	$('#color4').click(function() {
		$("#colorpicker").hide();
		$("#colorpicker3").hide();
		$("#colorpicker2").hide();
		$('#colorpicker4').show();
	});	
	
	
	$("#various1").fancybox({
		'autoDimensions'    : 'true',
		'autoScale'         : 'true',
		'titlePosition'		: 'inside',
		'transitionIn'		: 'none',
		'transitionOut'		: 'none'
	});
	
	$("#demo-link1").fancybox({
		'autoScale'			: false,
		'autoDimensions'    : false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe',
		'scrolling'         : 'no',
		'width'             : 120,
		'height'            : 90
	});
	
	$("#demo-link2").fancybox({
		'autoScale'			: false,
		'autoDimensions'    : false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe',
		'scrolling'         : 'no',
		'width'             : 120,
		'height'            : 240
	});
	
	$("#demo-link3").fancybox({
		'autoScale'			: false,
		'autoDimensions'    : false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe',
		'scrolling'         : 'no',
		'width'             : 120,
		'height'            : 600
	});
	
	$("#demo-link4").fancybox({
		'autoScale'			: false,
		'autoDimensions'    : false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe',
		'scrolling'         : 'no',
		'width'             : 240,
		'height'            : 300
	});
	
	$("#demo-link5").fancybox({
		'autoScale'			: false,
		'autoDimensions'    : false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe',
		'scrolling'         : 'no',
		'width'             : 240,
		'height'            : 400
	});
	
	$("#demo-link6").fancybox({
		'autoScale'			: false,
		'autoDimensions'    : false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe',
		'scrolling'         : 'no',
		'width'             : 250,
		'height'            : 250
	});
	
	$("#demo-link7").fancybox({
		'autoScale'			: false,
		'autoDimensions'    : false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe',
		'scrolling'         : 'no',
		'width'             : 300,
		'height'            : 250
	});
	
	$("#demo-link8").fancybox({
		'autoScale'			: false,
		'autoDimensions'    : false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe',
		'scrolling'         : 'no',
		'width'             : 300,
		'height'            : 375
	});
	
	$("#demo-link9").fancybox({
		'autoScale'			: false,
		'autoDimensions'    : false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe',
		'scrolling'         : 'no',
		'width'             : 468,
		'height'            : 90
	});
	
	$("#demo-link10").fancybox({
		'autoScale'			: false,
		'autoDimensions'    : false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe',
		'scrolling'         : 'no',
		'width'             : 480,
		'height'            : 225
	});
	
	$("#demo-link11").fancybox({
		'autoScale'			: false,
		'autoDimensions'    : false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe',
		'scrolling'         : 'no',
		'width'             : 500,
		'height'            : 375
	});
	
	$("#demo-link12").fancybox({
		'autoScale'			: false,
		'autoDimensions'    : false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe',
		'scrolling'         : 'no',
		'width'             : 640,
		'height'            : 400
	});
	
	$("#demo-link13").fancybox({
		'autoScale'			: false,
		'autoDimensions'    : false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe',
		'scrolling'         : 'no',
		'width'             : 640,
		'height'            : 425
	});
	
	$("#demo-link14").fancybox({
		'autoScale'			: false,
		'autoDimensions'    : false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe',
		'scrolling'         : 'no',
		'width'             : 728,
		'height'            : 90
	});
	
	$("#demo-link15").fancybox({
		'autoScale'			: false,
		'autoDimensions'    : false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe',
		'scrolling'         : 'no',
		'width'             : 800,
		'height'            : 225
	});
	
	$("#demo-link16").fancybox({
		'autoScale'			: false,
		'autoDimensions'    : false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe',
		'scrolling'         : 'no',
		'width'             : 890,
		'height'            : 100
	});

});

