if (top.location != location) { top.location.href = document.location.href; }

//###############    GENERIC FUNCTIONS    ##################



function reloadBanner(noTrack) {
	if (typeof bannerJsAccessible == "boolean") {
	
		var src = '/banner.php?';
		
		$(".banner_option").each(function(){
			src = src + $(this).attr("name") + "=" + encodeURIComponent($(this).attr("value")) + "&";
		});
		
		$("#bannerpreview").attr("src", function(){
			return src + "preview=1";
		})
		
		// use Google Analytics to track when a banner preview events.
		if (typeof noTrack == "undefined") {
			try {
				pageTracker._trackEvent("Banner", "Preview");
			} catch(err) {}
		}
	} else {
	
		// track disabled previews
		try {
			pageTracker._trackEvent("Banner", "Not Accessible");
		} catch(err) {}
		
		$("#previewcontainer").html('<img src="/img/disabled.png" alt="Disable your ad blocking software.">');
	}
}
function pageDimensions() {var x,y;if (self.innerHeight) {x = self.innerWidth;y = self.innerHeight;} else if (document.documentElement && document.documentElement.clientHeight) {x = document.documentElement.clientWidth;y = document.documentElement.clientHeight;} else if (document.body) {x = document.body.clientWidth;y = document.body.clientHeight;}return [x,y];}
function scrollOffset() { var x,y; if (window.pageYOffset) { x = window.pageXOffset; y = window.pageYOffset; } else if (document.documentElement && document.documentElement.scrollTop) { x = document.documentElement.scrollLeft; y = document.documentElement.scrollTop; } else if (document.body) { x = document.body.scrollLeft; y = document.body.scrollTop; } return [x, y]; }
function cookiesAllowed() {setCookie('checkCookie', 'test', 1); if (getCookie('checkCookie')) {deleteCookie('checkCookie');return true;}return false;} function setCookie(name,value,expires, options) {if (options===undefined) { options = {}; }if ( expires ) {var expires_date = new Date();expires_date.setDate(expires_date.getDate() + expires)}document.cookie = name+'='+escape( value ) + ( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + ( ( options.path ) ? ';path=' + options.path : '' ) + ( ( options.domain ) ? ';domain=' + options.domain : '' ) + ( ( options.secure ) ? ';secure' : '' );}function getCookie( name ) {var start = document.cookie.indexOf( name + "=" );var len = start + name.length + 1;if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {return null;}if ( start == -1 ) return null;var end = document.cookie.indexOf( ';', len );if ( end == -1 ) end = document.cookie.length;return unescape( document.cookie.substring( len, end ) );}function deleteCookie( name, path, domain ) { if ( getCookie( name ) ) document.cookie = name + '=' + ( ( path ) ? ';path=' + path : '') + ( ( domain ) ? ';domain=' + domain : '' ) + ';expires=Thu, 01-Jan-1970 00:00:01 GMT';}
function handleEnter (field, event) {
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13) {
		try {
			var i;
			for (i = 0; i < field.form.elements.length; i++)
			if (field == field.form.elements[i])
			break;
			i = (i + 1) % field.form.elements.length;
			field.form.elements[i].focus();
			return false;
		} catch (err) {

		}
	}
	else
	return true;
}
	
	
// which line are we changing fonts for
var currentLine = 0;


$(document).ready(function(){

	//######  MISCELLANEOUS PICKERS AND CLICKERS  ######
	$("#tab_size_options div").click(function(){
		$("#banner_size_input").attr("value", $(this).attr("id"));
		
		var bgimagecode = document.getElementById('bgimagecode').value;
		if (bgimagecode.length > 4 && bgimagecode != 'undefined') {
			document.location.href = "crop.php?bgimagecode=" + bgimagecode + "&bannersize=" + document.getElementById('banner_size_input').value;
		} else {
			reloadBanner();
			window.scroll(0,100);
		}
	});
	
	if (!cookiesAllowed()) { alert("Your browser is not accepting cookies from this website. This is either because you have them disabled or are using software like Norton or McAfee security suites or ad blocking software. These types of software believe we're spam because we have the name 'banner' in our name! If you want the full functionality of the website, please enable cookies or disable this type of software while you are on our site. Thanks and sorry for the annoying notice!");}


	$("#cmCloseButton").click(function(){
		$("#overlay").click();
	});
	$("#cmHex").keydown(function(e){ if (e.keyCode == 13) { $("#cmCloseButton").click(); } }); 


	
	
	// NEW SLIDER INIT
	
	$("#borderslider").slider({
		value: 	$("#input_bordersize").val(),
		max: 	30,
		slide:	function(event, ui) {
					$("#msg").html(ui.value);
					$("#input_bordersize").val(ui.value);
				},
		stop: 	function (event, ui) { reloadBanner(); }
	});
	
	$("#overlayslider").slider({
		value:	$("#input_overlayamt").val(),
		slide:	function(event, ui) {
					$("#input_overlayamt").val(ui.value);
				},
		stop:	function (event, ui) { reloadBanner(); }
	});
	
	for (var i=0; i<4; i++) {
		$("#halignslider_" + i).slider({
			min:	-20,
			max:	120,
			value: 	$("#halign_" + i).val(),
			stop:	function (event, ui) { 
						$("#halign_" + $(this).attr("id").match(/_([0-9]+)$/)[1]).val(ui.value);
						reloadBanner();
					}
		});
	}
	
	for (var i=0; i<4; i++) {
		$("#valignslider_" + i).slider({
			min:	-20,
			max:	120,
			value: 	$("#valign_" + i).val(),
			stop:	function (event, ui) { 
						$("#valign_" + $(this).attr("id").match(/_([0-9]+)$/)[1]).val(ui.value);
						reloadBanner();
					}
		});
	}


	// Set ReloadBanner Event Listeners
	$(".updateonchange").change(function(){reloadBanner();return false;});


	// Show ColourMod
	$(".color_picker").click(function(){
		var id = this.id;
		mypickcolor("#input_" + id, "#" + id, this);
		$("#overlay").show();
		$("#cmHex").select();
		$("body").disableSelection();
	});


	
// Pageload Settings

	$(".color_picker").css("background-color", function(){
		return "#" + $("#input_" + $(this).attr("id")).val();
	});
	reloadBanner(false);

	$("#overlay").click(function(){
		$(this).hide();
		$("#fonts").hide();
		$("#ColourMod").hide();
		$("body").enableSelection();
		reloadBanner();
		return false;
	})
	
	$("#bgimage").change(function(){
		if (/.+\.(jpg|jpeg|png|gif)$/i.test($(this).val())) {
			$("#subby").removeAttr('disabled');
		} else {
			if (/.+\.bmp$/.test($(this).val())) {
				alert ('Bitmap (BMP) files are not compatible. Please convert your file to PNG, GIF, or JPG. NOTE: Changing the extension does not convert the file type.');
			} else {
				alert('The type of the file you selected is not supported. Please convert it to PNG, GIF, or JPG. NOTE: Changing the extension does not convert the file type.');
			}
			$(this).val('');
			$("#subby").attr('disabled', 'disabled');
		}
	});
});


// New Font Functions
var currentFontLine = 0;

function showFonts(line ,obj) {
	currentFontLine = line;
			
	$("#overlay").show();
	$("#fonts").css("top", (findPos(document.getElementById('tabs'))[1] + "px")).show();	
}

function setFont(id) {
	$("#font_" + currentFontLine).val('i' + id);
	$("#sample_" + currentFontLine).attr("src", "/img/fonts/" + id + ".png");
	$("#overlay").hide();
	$("#fonts").hide();
	
	reloadBanner();
	
	return false;
}