/* $Id: main.js 445 2008-12-12 22:13:58Z vincent $ */

$(document).ready(function(){
	
	if ($("#fancyme .fancyselect").length > 0) {
		$('.fancyselect').selectmenu({width: 190, maxHeight: 400});
	}
	
	
	if ($.browser.msie && $.browser.version.substr(0,1)<8) {
		if ($("#footright").length > 0) {
			var footPOs = $("#footnav").width();
			$("#footright").css({width: footPOs});
		};
	}



	$("#bh_title").dropShadow({left:-15, top:0, blur:2, color:"#B67747", opacity: .8});
	
	if ($("#searchHome").length > 0) {
		$("#searchHome").submit(function(){
			var action = $(this).attr("action");
			var value = $("#searchHome #search").attr("value");
			$(this).attr("action", action + '?searchamanjaro=' + value);
			console.log(action, value);
		});
	};
	

	
	// Remove Text From search boxes
	$('.searchBox').focus(function(){
		if($(this).val() == $(this).attr('defaultValue')){$(this).val('');}
	});

	$('.searchBox').blur(function(){
		if($(this).val() == '') { $(this).val($(this).attr('defaultValue')); }
	});

	$('#username').focus(function(){
		if($(this).val() == $(this).attr('defaultValue')){$(this).val('');}
	});

	$('#username').blur(function(){
		if($(this).val() == '') { $(this).val($(this).attr('defaultValue')); }
	});
	
	$("#show_mood").click(function(){
		var position = $(this).position();
		$("#mood_popup").load("/assets/images/forum/moods.phtml", function(){
				$("#mood_popup").css({"top":position.top + 20, "left":position.left });
				$("#generalModal").show();
				$("#mood_popup").slideDown("normal", function(){
					$("#mood_popup li img").click(function(){
						var mood = $(this).attr("alt");
						$("#show_mood").html('<img id="'+mood+'" src="http://img.afullcup.com/images/vmoods/images/'+mood+'.gif" alt="'+mood+'" title="'+mood+'" />');
						$("#mood_popup").hide();
						$("#generalModal").hide();
						$.post("/users/updatemood", { moodid: mood} );
					});
				});
				return false;
		});
	});

	$("#userstatus").click(function(){
		$("#userstatus span").hide();
		$("#user_status").show();
		var oldStatus = $("#user_status").val();
		$('#user_status').blur(function(){
			var status = $(this).val();
			if (status == '') { $("#userstatus span").text('Click here to update your status.'); }
			else { $("#userstatus span").text(status);}
			
			$("#user_status").hide();
			$.post("/users/updatestatus", { userStatus: status} );
			$("#userstatus span").show();
		});
	});
	
	$("#notices_total").click(function(){
		var position = $(this).position();
		$("#user_notices").css({"top":position.top + 20, "left":position.left - 20 });
		$("#generalModal").show();
		$("#user_notices").show();
		return false;
	});
	
	$("#generalModal").click(function(){
		$("#generalModal").hide();
		$("#user_notices").hide();
		$("#mood_popup").hide()
	});

	$("a[href^='http']").attr('target','_blank');
	
	$("label[for='agree']").html('* I agree to the A Full Cup <a target="_blank" href="/tos">Terms of Service</a>');
	
	$('.isDate').datepicker({
		inline: false
	});
	
	$('.tip').bt({
		padding: 20,
		fill: 'rgba(46, 122, 190, .8)',
		strokeWidth: 3,
		strokeStyle: '#CCCCCC',
		cssStyles: {color: '#FFF', fontWeight: 'bold'},
		positions: ['right', 'left', 'bottom']
	});

	// Home Page coupon pop-ups
	$(".nc_wrap").click(function (e) {
		var wrap = $(this);
		var position = wrap.offset();
		$("#newpop").hide();
		
		var popup = $(this).siblings('.nc_popup');
		$("#newpop").html(popup.html());
		$("#newpop").css({"top":position.top - 135, "left":position.left + 205 });
		$("#newpop").show();
		$("#backgroundPopup").show();
		$(".closepopup").click(function(){
			$("#newpop").hide();
			$("#newpop").html('');
			$("#backgroundPopup").hide("fast");
		});
	});

	$("#backgroundPopup").click(function(){
		$("#newpop").hide();
		$("#newpop").html('');
		$("#backgroundPopup").hide("fast");
	});
	
	// Set specific variable to represent all iframe tags.
	var iFrames = $('iframe');
	// Resize heights.
	function iResize()
	{
		// Iterate through all iframes in the page.
		for (var i = 0, j = iFrames.length; i < j; i++)
		{
			// Set inline style to equal the body height of the iframed content.
			iFrames[i].style.height = iFrames[i].contentWindow.document.body.offsetHeight + 'px';
		}
	}

	// Check if browser is Safari or Opera.
	if ($.browser.safari || $.browser.opera)
	{
		// Start timer when loaded.
		$('iframe').load(function()
			{
				setTimeout(iResize, 0);
			}
		);

		// Safari and Opera need a kick-start.
		for (var i = 0, j = iFrames.length; i < j; i++)
		{
			var iSource = iFrames[i].src;
			iFrames[i].src = '';
			iFrames[i].src = iSource;
		}
	}

	//$("#userRegister fieldset .field").eq(0).hide();
	
	$("#pmpopup").dialog({
				resizable: false,
				height:140,
				modal: true,
				buttons: {
					Close: function() {
						$(this).dialog('close');
						$(this).dialog('destroy');
					},
					Read: function() {
						$(this).dialog('close');
						$(this).dialog('destroy');
						window.open('/forums/private.php');
					}
				},
				title: 'New Private Message',
				position: ["top", 100],
				zIndex: 30000000
	});
	
	if ($("#home_ads_top_hold").length > 0) {
		var divHold = $("#home_ads_top_hold");
		var position = divHold.offset();
		$("#home_ads_vert").css(
			{'top' : position.top + 'px', 'left' :  position.left+'px', 'position' : 'absolute'}
		);

		$("#home_ads_vert").show();
	};
	
});

$.fn.clearForm = function() {
  return this.each(function() {
    var type = this.type, tag = this.tagName.toLowerCase();
    if (tag == 'form')
      return $(':input',this).clearForm();
    if (type == 'text' || type == 'password' || tag == 'textarea')
      this.value = '';
    else if (type == 'checkbox' || type == 'radio')
      this.checked = false;
    else if (tag == 'select')
      this.selectedIndex = -1;
  });
};

function resetForm(id) {
	$('#'+id).each(function(){
	        this.reset();
	});
}

function resetInput(id) {
	$(id).each(function(){
	        $(this).attr("checked", "");
	});
}