// <![CDATA[

$(document).ready(function(){

	resetElements();
	postFormMessage();
	if ($('#home').length >0) {
		slideShow();
	}
	
	//resetFormElements();
	$('.form *[title]').inputHint();
	//tooltip();
	//$('.hint').inputHintBox({className:'hint-box',source:'attr',attr:'title',incrementLeft:-52,incrementTop:-35});
	//$('input[title]').inputHints();
	
});

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

this.tooltip = function(){	
	/* CONFIG */		
		xOffset = 10;
		yOffset = 20;		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result		
	/* END CONFIG */		
	$(".form *[title]").hover(function(e){											  
		this.t = this.title;
		this.title = "";									  
		$("body").append("<p id='tooltip'>"+ this.t +"</p>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#tooltip").remove();
    });	
	
	$(".form *[title]").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});
	
	//$(".highlight").unbind('mouseenter mouseleave');
	/*
	$(".highlight").hover(function(e){											  
		$("#tooltip").remove();
		},function(){	
		$("#tooltip").remove();
    });
	*/
};

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

function slideShow() {

	//add a class the the first element
	$('.slide 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').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
	$('.caption .content').html($('.slide li:first-child').find('img').attr('rel'))
	.animate({opacity: 1.0}, 400);
	
	//Call the gallery function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('gallery()',8000);
	
}

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 = next.find('img').attr('rel');
	//alert(caption);
	
	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}, 2000, function(){
	current.animate({opacity: 0.0}, 2000).removeClass('active last-active');
	});
	
	$('.caption')
	.animate({opacity: 0.0}, 500, function() {
	$('.slide').css({height:height});
	$('.caption').animate({opacity: 1.0}, 500);
	//Display the content
	$('.caption .content').html(caption);
	});
}

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

this.resetElements = function(){
	$('#tab_contact h2').corner('top 8px');
	
	$("#home .row-2 img:first").parent("p").css({
		margin:'0px'
		});

	$(".current").next().css({
		display:'block'
		});
	
	$(".col-right li:first").nextAll().css({
		maxHeight:'144px',
		overflow:'hidden'
		});
	
	$("#home .col-right li:first").nextAll().css({
		maxHeight:'none',
		overflow:'visible'
		});
	
	if ($(".current").next().length >0) {
		$("#header").css({
		margin:'0px 0px 60px 0px'
		});
	}
	
	var current = $(".current");
	var width_1 = current.parent('li').prev().width();
	var width_2 = current.parent('li').prev().prev().width();	
	var width_new = 1014 - (width_1 + width_2);
	$(".submenu").css({
		width: width_new
	});
	
	
}

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

this.resetFormElements = function(){
	var defaultText_fn = 'First Name';
    $('#message_name_first').val(defaultText_fn).focus(function() {
        if ( this.value == defaultText_fn ) this.value = '';
		//$(this).css({color:'#999999'});
    }).blur(function(){
        if ( !$.trim( this.value ) ) this.value = defaultText_fn;
		});
	
	var defaultText_ln = 'Last Name';
    $('#message_name_last').val(defaultText_ln).focus(function() {
        if ( this.value == defaultText_ln ) this.value = '';
    }).blur(function(){
        if ( !$.trim( this.value ) ) this.value = defaultText_ln;
    	});
}

jQuery.fn.inputHints = function() {
    // hides the input display text stored in the title on focus
    // and sets it on blur if the user hasn't changed it.

    // show the display text
    $(this).each(function(i) {
        $(this).val($(this).attr('title'));
    });

    // hook up the blur & focus
    $(this).focus(function() {
        if ($(this).val() == $(this).attr('title'))
            $(this).val('');
    }).blur(function() {
        if ($(this).val() == '')
            $(this).val($(this).attr('title'));
    });
};

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

this.postFormMessage = function(){
$("#form_data_message").submit(function(){
	$(".popup").remove();
	var path = $(this).attr('action');
	var http_root = $("body").attr("rel");
	var processing = $('.processing');
	processing
		.css({
			margin:"5px 0px 0px 10px",
			position:"absolute",
			visibility:"visible"
			});
		
	processing.html('<div><p>processing <img src="'+http_root+'img_icon/loader_0.gif"/></p></div>');
	$.post(path, $("#form_data_message").serialize(),function(xml){
			//var str = $("#form_data_message").serialize();
			//alert(str);
			$("label").removeClass('error');
			$("form img").css({visibility:'hidden'});
			processing
				.css({
					visibility:"hidden"
					});
			processFormMessage(xml,http_root);
		});
	return false;
	});
}

this.processFormMessage = function(xml,http_root){
	$(document.body).append("<div id=\"popup_result\" class=\"popup\"></div>");
	var target = $('#popup_result');
	var scrollTop = $(window).scrollTop();
	var scrollLeft = $(window).scrollLeft();
	var width = 400;
	var top = 200;
	var marginLeft = "-"+ ((scrollLeft + width)/2);
	target
		.css({
			top:(scrollTop + top) + "px", 
			//top:"50%",
			left:"50%",
			marginTop:"-100px",
			marginLeft:marginLeft + "px",
			width:width + "px",
			zIndex:"200",
			display:"none"
			});
		
	target.load(http_root+"incl_layouts/result.php", {}, function(){
		$("error", xml).each(function(){
			var elementid = $(this).attr('elementid');
			var message = $(this).attr('message');
			$("#"+elementid+"_label").addClass('error');
			$("#"+elementid+"_img").css({visibility:'visible'});
			$(".result").append("<img src='"+http_root+"img_icon/attention.png' /> <b>" + message + "</b> <br />");
			target.fadeIn('slow', function(){	
				closePopup(target);
				$('.form *[title]').inputHint();
			});	
		});
		
		$("result", xml).each(function(){
		var message = $(this).attr('message');
		$(".result").append("<img src='"+http_root+"img_icon/info.png' /> <b>" + message + "</b> <br />");
		target.fadeIn('fast', function(){
			clearFormElements();
			closePopup(target);
			$('.form *[title]').inputHint();
			});
		});
	});/**/
}

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

this.guide = function(){	
$(".guide").click(function(){
	var path = $(this).attr("href");
	JDOM.popup(path,'guide','width=400,height=600, scrollbars=yes'); 
	return false;
	});
}

this.closePopup = function(target){	
$(".close").click(function(){
	target.fadeOut('fast', function(){
		target.remove();
		});
	return false;
	});
}

this.locationBack = function(target){	
$(".back").click(function(){
	var path = $(this).attr("href");
	$(".popup").remove();
	document.location = path;
	return false;
	});
}

this.toggleAttach = function(target){
$("#uploads a").unbind('click');
$("#uploads a").click(function(){
	var target = $(this).attr("href");
	$('#'+target).slideToggle(500);
	return false;
	});
}


this.clearFormElements = function(target){ //  ==  function clear_form_elements(target) {
    $(target).find(':input').each(function() {
        switch(this.type) {
            case 'password':
            case 'select-multiple':
            case 'select-one':
            case 'text':
            case 'textarea':
                $(this).val('');
                break;
            case 'checkbox':
            case 'radio':
                this.checked = false;
        }
    });
}


// ]]>