/**************************************/
/******** Site Public Functions *******/
/**************************************/

this.autoComplete = function(){

var http_root = $("body").attr("rel");
	$("#input").autocomplete(http_root+rp_layout+"tag_autocomplete_mysql.php", {
		width: 176,
		matchContains: true,
		//mustMatch: true,
		//minChars: 0,
		//multiple: true,
		//highlight: false,
		//multipleSeparator: ",",
		selectFirst: false
	});
}	
	
/*****************/

function slideShow() {

	//add a class the the first element
	$('#slides li:first-child').addClass('active');
	
	//Set the opacity of all images to 0
	$('.slide li').css({opacity: 0.0});
	
	//Get the first image and display it (set it to full opacity)
	$('.slide li:first-child').css({opacity: 1.0});
	
	//Set the caption background to semi-transparent
	//$('.slide .caption h2').css({opacity: 0.7});

	//Resize the width of the caption according to the image width
	//$('.slide .caption').css({width: $('.slide > li').find('img').css('width')});
	
	//var height = $('.slide li').find('img').css('height');
	//$('.slide').css({height:height});
	
	//Get the caption of the first image from REL attribute and display it
	$('#slides .caption h2').html($('.slide li:first-child').find('img').attr('title'));
	$('#slides .caption p').html($('.slide li:first-child').find('img').attr('rel'));
	
	//Call the gallery function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('gallery()',5000);
	
}

function gallery() {
	
	//if no IMGs have the show class, grab the first image
	var current = ($('.slide li.active')?  $('.slide li.active') : $('.slide li:first-child'));

	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('caption'))? $('.slide li:first-child') :current.next()) : $('.slide li:first-child'));	
	
	//Get next image caption
	var caption_title = next.find('img').attr('title');
	var caption_description = next.find('img').attr('rel');
	
	//Get next image height
	//var height = next.find('img').css('height');
	
	//Set the fade in effect for the next image, show class has higher z-index
	current.addClass('last-active');
	next.css({opacity: 0.0})
	.addClass('active')
	.animate({opacity: 1.0}, 1000, function(){
		current.animate({opacity: 0.0}, 1000).removeClass('active last-active');
		//$('.caption').animate({opacity: 1.0}, 1000);
		$('#slides .caption h2').html(caption_title);
		$('#slides .caption p').html(caption_description);
	});
	/*
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
	
	$('.caption').animate({opacity: 0.0}, 1000);
	*/
	
	/*
	$('.caption').animate({opacity: 0.0}, 500, function() {
		$('.slide').css({height:height});
		$('.caption').animate({opacity: 1.0}, 500);
		//Display the content
		$('.caption p').html(caption_description);
	});*/
}

/*****************/

this.replaceTitle = function(){
	$('#main h2').each(function(index){
		var title = $(this).text().toLowerCase();
		var title = title.replace(/,/g, "");
		var title = title.replace(/'/g, " ");
		var title = title.replace(/ /g, "-");

		//alert(title);
		$(this).attr("id", "title-" + title);
		$(this).text(''); 	
		});
}

/*****************/

this.changeButton = function(){
	
	if ($('#portfolio').length >0) {
		$('.change').css({
			backgroundPosition: 'right -58px' 
		});
	}
	
	if ($('#blog').length >0) {
		$('.change').css({
			backgroundPosition: 'right -29px' 
		});
	}
	
	/*
	setTimeout(function(){ 
		
		$('.change').css({
			backgroundPosition: 'right -29px' 
		});
		$('#who-me').css({
			backgroundPosition: 'center top' 
		}); 
	}, 10000);
	
	setTimeout(function(){ 
		$('.change').css({
			backgroundPosition: 'right -58px' 
		});
		$('#who-me').css({
			backgroundPosition: 'right top' 
		}); 
	}, 20000);
	
	setTimeout(function(){ 
		$('.change').css({
			backgroundPosition: 'right 0px' 
		});
		$('#who-me').css({
			backgroundPosition: 'left top' 
		}); 
	}, 30000);
	
	setTimeout("changeButton()", 30000);
	*/
}

/*****************/

this.scrollTop = function(){
	$('a[href=#top]').click(function(){
        $('html, body').animate({scrollTop:0}, 'slow');
        return false;
    });
}

/*****************/

this.appendCaption = function(){
	$("#content-profile-left img").each(function () {
	var imgwidth = $(this).width();
	var title = $(this).attr("alt");
	
	$(this).wrap("<div class=\"image_container\"></div>");
	$(this).parent().css({
		width: imgwidth+2,
		float: 'right',
		fontSize: '11px',
		lineHeight:'11px',
		fontStyle:'italic',
		margin: '0px 0px 20px 20px'
		});
	$(this).parent().append("<div class=\"image_description\">" + title + "</div>");
	
	});
}

this.insertAfterCaption = function(){
	$("#content-profile-left img").each(function () {
	var imgwidth = $(this).width();
	var alt = $(this).attr("alt");
	//alert(alt);
	
	$("<div class='image_description'>"+ alt +"</div>").insertAfter(this);
	$('.image_description').css({
		color: '#ffffff',
		fontSize: '11px',
		lineHeight:'11px',
		fontStyle:'italic',
		margin: '0px 0px 0px 0px',
		float: 'right',
		width: imgwidth
		});
	
	});
}

this.afterCaption = function(){
	$("#content-profile-left img:first").each(function () {
	var $this = $(this);
	var imgwidth = $this.width();
	var title = $this.attr("title");
	var alt = $this.attr("alt");
	
    $this.after('<div class="caption">' + '<i><span style="float:left;">' + title + '</span><span style="float:right;">' + alt +'</span></i>' + '</div>');

	$('.caption').css({
		margin: '5px 0px 0px 0px',
		fontSize: '10px',
		width: imgwidth
		});
	
	});
}

/*****************/

this.postForm_signUp = function(){
$("#form-signup").submit(function(){
	var path = $(this).attr('action');
	var http_root = $("body").attr("rel");
	var processing = $('.processing');
	processing.html('<div><p><img src="'+http_root+rp_image_global+img_loader+'"/> loading</p></div>');
	$.post(path, $("#form-signup").serialize(),function(xml){
		//var str = $("#form_data").serialize();
		//alert(str);
		processForm_signUp(xml,http_root);
		});
	return false;
	});
}

this.processForm_signUp = function(xml,http_root){
	$("error", xml).each(function(){
		var elementid = $(this).attr('elementid');
		var message = $(this).attr('message');
			//$("#"+elementid+"_label").addClass('error');
			//$("#"+elementid+"_img").css({visibility:'visible'});
		$(".respond-sign-up").html("<img src='"+http_root+rp_image_global+"attention.png' /> " + message + "<br />");
		$('form *[title]').inputHint();
		//alert(message);
	});
		
	$("result", xml).each(function(){
		var message = $(this).attr('message');
		$(".respond-sign-up").html( "<b>Success!</b> " + message + "<br /> <br /> If you do not receive a verification email there may have been a problem with the address, or it might be hiding in your junk mail. Please either try again or you can email me at <a href='mailto:tom@gingermonkeydesign.com'>tom@gingermonkeydesign.com</a>.");
		var target = $("#sign-up");
		clearFormElements(target);
		//$('form *[title]').inputHint();
		//alert(message);
	});
}

/*****************/

this.postForm_searchTag = function(){
$("#form_search").submit(function(){
	$(".popup").remove();
	var path = $(this).attr("action");
	var http_root = $("body").attr("rel");
	//alert(path);
	$(document.body).prepend("<div id=\"popup_search\" class=\"popup\"></div>");
	
	var target = $('#popup_search');
	var scrollTop = $(window).scrollTop();
	var scrollLeft = $(window).scrollLeft();
	var width = 300;
	var top = 200;
	var marginLeft = "-"+((scrollLeft + width)/2);
	target
		.css({
			top:(scrollTop + top) + "px", 
			left:"50%",
			marginLeft:marginLeft + "px",
			width: width + "px",
			zIndex:"20"
			});
	
	target.fadeIn('fast', function(){		
		target.html('<div class="ajaxloader"><img src="'+http_root+rp_image_global+img_loader+'"/> loading</div>');
		$('.ajaxloader')
			.css({
				padding:"20px", 
				textAlign:"center",
				backgroundColor:"#ffffff",
				color: "#000000"
				});	
	
	$.post(path, $("#form_search").serialize(),function(xml){
			//var str = $("#form_search").serialize();
			//alert(str);
			processForm_searchTag(xml,http_root,target);
			});
		})
	return false;
	});
}

this.processForm_searchTag = function(xml,http_root,target){
	
	target.load( http_root+rp_layout+"result.php", {}, function(){
	
		$("error", xml).each(function(){
			var elementid = $(this).attr('elementid');
			var message = $(this).attr('message');
			$(".result").append("<img src='"+http_root+rp_image_global+"attention.png' /> " + message + "<br />");
			closePopup(target);
			$('form *[title]').inputHint();
			//alert(message);
		});
			
		$("result", xml).each(function(){
			var message = $(this).attr('message');
			var tagname = $(this).attr('tagname');
			$(".result").append( message );
			document.location = http_root+'tags/'+tagname; 
			//var target = $("#sign-up");
			//clearFormElements(target);
			//var str = $('#input').val();
			//$('form *[title]').inputHint();
			//alert(message);
		});
	});
}

/*****************/

function lookup(inputString) {
		var http_root = $("body").attr("rel");
		if(inputString.length == 0) {
			// Hide the suggestion box.
			$('#suggestions').hide();
		} else {
			$.post(http_root+"inc-layout/rpc.php", {queryString: ""+inputString+""}, function(data){
				if(data.length >0) {
					$('#suggestions').show();
					$('#autoSuggestionsList').html(data);
				}
			});
		}
} // lookup
	
function fill(thisValue) {
		$('#input').val(thisValue);
		setTimeout("$('#suggestions').hide();", 200);
}


/*****************/

this.postForm_unsubscribe = function(){

	var http_root = $("body").attr("rel");
	$("input[name=yes]").click(function(){
		$("#form-unsubscribe").submit(function(){
			var path_post = $(this).attr('action');
			//alert(path_post);
			//$(this).parent().html('<img src="'+http_root+'img-icon/loader-2b.gif"/> loading');
			$.post(path_post, function(data) {
				alert(data);
				document.location = http_root+'index.php'; 
			});
			return false;
		})
	});	
	
	$("input[name=no]").click(function(){
		$("#form-unsubscribe").submit(function(){
			document.location = http_root+'index.php'; 
			return false;
		})	
	});
	
}
