var featureShow = new Array();
featureShow[0] = 'images/features/home1.jpg';
featureShow[1] = 'images/features/home2.jpg';
featureShow[2] = 'images/features/home3.jpg';
featureShow[3] = 'images/features/home4.jpg';

var featureShowLinks = new Array();
featureShowLinks[0] = null;
featureShowLinks[1] = null;
featureShowLinks[2] = null;
featureShowLinks[3] = null;

function processSlideshow(elem, imageList, imageDuration, fadeSpeed, current, links) 
{
	var listSize = imageList.length;
    if (!current || current >= listSize) current = 0;
    if (!imageDuration) imageDuration = 2000;
    if (!fadeSpeed) fadeSpeed = 1000;
    $(elem + " img").attr("src", imageList[current]);
    if (current == (listSize - 1)) { 
		$(elem).css("background", "transparent url(" + imageList[0] + ") no-repeat");
    } else {
        $(elem).css("background", "transparent url(" + imageList[current + 1] + ") no-repeat");
    }
    $(elem + " img").animate({ opacity: "1" }, imageDuration).
	      animate({ opacity: "0.01" }, fadeSpeed, function() 
		   {
		   	 var isLink = (links[current + 1]!=null?1:0);
			 $(this).css('opacity','1').parent().attr('href',(isLink?links[current + 1]:'javascript:void(0);')).css('cursor',(isLink?'pointer':'default'));
			 processSlideshow(elem, imageList, imageDuration, fadeSpeed, current + 1,links); 
		   });
}

Array.prototype.random = function( r )
{
 var i = 0, l = this.length;
 if( !r ) { r = this.length; }
 else if( r > 0 ) { r = r % l; }
 else { i = r; r = l + r % l; }
 return this[ Math.floor( r * Math.random() - i ) ];
};

(function($){
$.fn.extend({
	/**
	 * Stores the original version of offset(), so that we don't lose it
	 */
	_offset : $.fn.offset,
	/**
	 * Set or get the specific left and top position of the matched
	 * elements, relative the the browser window by calling setXY
	 * @param {Object} newOffset
	 */
	offset : function(newOffset){
	    return !newOffset ? this._offset() : this.each(function(){
			var el = this;
			var hide = false;
			if($(el).css('display')=='none'){
				hide = true;
				$(el).show();
			};
			var style_pos = $(el).css('position');
			// default to relative
			if (style_pos == 'static') {
				$(el).css('position','relative');
				style_pos = 'relative';
			};
			var offset = $(el).offset();
			if (offset){
				var delta = {
					left : parseInt($(el).css('left'), 10),
					top: parseInt($(el).css('top'), 10)
				};
				// in case of 'auto'
				if (isNaN(delta.left))
					delta.left = (style_pos == 'relative') ? 0 : el.offsetLeft;
				if (isNaN(delta.top))
					delta.top = (style_pos == 'relative') ? 0 : el.offsetTop;
				if (newOffset.left || newOffset.left===0)
					$(el).css('left',newOffset.left - offset.left + delta.left + 'px');
				if (newOffset.top || newOffset.top===0)
					$(el).css('top',newOffset.top - offset.top + delta.top + 'px');
			};
			if(hide) $(el).hide();
		});
	}
});
})(jQuery);

function validateContact(formData,jqForm,options){
	var ret = true;
	var req = $('input[name=name],input[name=email],input[name=phone],textarea[name=feedback]');
	$(req).each(function(i){
		label = req.eq(i).prev().prev();
		if (!req.eq(i).fieldValue()[0])
		{
			ret = false;
			if (label.text().search(/required/) == -1) { label.append(' (required)').wrapInner('<span class="warning"></span>'); }
		}
		else
		{
			label.html(label.text().replace(/\(required\)/,''));
		}
	});
	return ret;
}

function showResponse(responseText,statusText)  { 
  		$('#contact-form-wrapper').html('<h4>Thank You for Contacting Us</h4>'+
		'<p id="contact-response">Your message has been sent. Please <a id="contact-cb" href="#">click here</a> to continue browsing.</p>');
}
function getUrlVars() {
	var vars = {};
	var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
		vars[key] = value;
	});
	return vars;
}

jQuery(function($){
	$('#nav-contact').hover(
		function(){ $(this).css('background-position','0 -38px'); }, 
      	function(){ $(this).css('background-position','0 0'); }
    ).click(function(){
		if($('#contact').is(":hidden") == true)
        {
			$(this).addClass('contact-toggle-up');
			$('#contact').slideDown('normal');
			pageTracker._trackPageview("/contact-us/" );
        }
        else
        {
			$(this).removeClass('contact-toggle-up');
			$('#contact').slideUp('normal');
        }
	}); 
	$('#contact-submit').hover(
            function(){$(this).css('background-position','-671px 0');},
            function(){$(this).css('background-position','-523px 0');}
	);
	var formOptions = { 
        beforeSubmit:  validateContact,
        success:       showResponse
    };
    $('#contact-form').ajaxForm(formOptions);
	$('#contact-cb').live('click', function(){ 
      	$('#nav-contact').removeClass('contact-toggle-up');
		$('#contact').slideUp('normal');
	  	return false;
    });
	/*1*/
	var loffset = Math.round($('#obj1').offset().left+$('#obj1').width()+20);
	var toffset = Math.round($('#obj1').offset().top);
	$('#obj-desc1').wrapInner("<span></span>").offset({left:loffset,top:toffset});
	/*2*/
	var loffset = Math.round($('#obj2').offset().left+$('#obj2').width()+20);
	var toffset = Math.round($('#obj2').offset().top);
	$('#obj-desc2').wrapInner("<span></span>").offset({left:loffset,top:toffset});
	
	//For xhtml-strict valid external links
	$('a[rel=external]').attr('target', '_blank');	
	//processSlideshow('#home #content',featureShow,4000,1000,0,featureShowLinks);
	processSlideshow('#home #slideshow',featureShow,5000,2000,0,featureShowLinks);
});