//try to eliminate IE 6 flicker
try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}

function homeAnimationStart() {
	$('div#homefeature img#featurebgstart').fadeOut(1000, function() {
		$('div#homefeaturetabs div.homefeaturetabcontent:visible div.homefeaturestripe').animate({
			top:'62px'
		}, 750);
	});
};

// pass in the 'created_at' string returned from twitter //
function parseTwitterDate(stamp) {		
	var dateArray = stamp.split(' ');
	var myDate = new Date(Date.parse(dateArray[1]+" "+dateArray[2]+", "+dateArray[3]+" "+dateArray[4]+" UTC"));
	var myDay = myDate.getDate();
	var myMonth = myDate.getMonth() + 1;
	var myYear = myDate.getFullYear();
// get the two digit hour //
	var myHour = myDate.getHours();
	var myMinutes = myDate.getMinutes();
// convert to AM or PM //
	var ampm = myHour<12 ? ' AM' : ' PM';
	if (myHour>12) myHour-= 12;
	if (myHour==0) myHour = 12;
	if (myMinutes<9) {myMinutes = "0" + myMinutes};
// return the formatted string //
	return myMonth + '/' + myDay + '/' + myYear + ', ' + myHour + ':' + myMinutes + ampm;
}

var twitterUserName = "xigonow"; 

/* Initialize everything when page is ready */
$(document).ready(function() {
	
	// Preload CSS images
	//$.preloadCssImages();
	
	var homepagePanels = $("div#homefeaturetabs div.homefeaturetabcontent");
	var homepagePanelHeaders = $("div#homefeaturetabs div.homefeaturetabcontent h2");
	
	/* Homepage feature area */
	$("div#homefeaturetabs div.homefeaturestripe a.mainfeaturelink").hover(function() {
		$(homepagePanelHeaders).removeClass("hovered");
		$(homepagePanels).find("img.panelbg").stop("true","false").fadeOut(400, function() {
			$(homepagePanels).removeClass("hovered");
		});
		var myPanel = $(this).parents("div.homefeaturetabcontent");
		var myImage = $(myPanel).find("img.panelbg");
		$(myPanel).find("h2").addClass("hovered");
		$(myImage).stop("true","false").fadeIn(400, function() {
			$(myPanel).addClass("hovered");
		});
		
	}, function() {
		$(homepagePanelHeaders).removeClass("hovered");
		$(homepagePanels).find("img.panelbg").stop("true","false").fadeOut(400, function() {
			$(homepagePanels).removeClass("hovered");
		});
	});
	
	/* Question box init */
	var questionBoxSections = $("div#quickaccordion div.accordionsection");
	
	$("div#quickaccordion h3.accordiontrigger a").click(function() {
		// Remove the sectionopen class from all sections
		$(questionBoxSections).removeClass("sectionopen");
		
		// No matter what, close all open sections
		$(questionBoxSections).find("div.accordioncontent").slideUp("normal", function() {
			// Fix some IE8 bugs with disappearing margins
			$(this).parent().addClass("iefix");
			$(this).parent().removeClass("iefix");
		});
		
		// If the next section wasn't open, open it
		if($(this).parent().next().is(':hidden') == true) {
			
			// Add the sectionopen class
			$(this).parents("div.accordionsection").addClass("sectionopen");
			
			// Open the section
			$(this).parent().next().slideDown('normal', function() {
				// Fix some IE8 bugs with disappearing margins
				$(this).parent().addClass("iefix");
				$(this).parent().removeClass("iefix");
			});
		};
			
		return false;
	});


	/* Superfish init */
	if ($.browser.msie) {
		$('ul.sf-menu').superfish({
			animation:   {height:'show'},
			speed: 'normal',
			autoArrows: false,
			dropShadows: false,
			delay: 300
		})
	} else {
		$('ul.sf-menu').superfish({
			animation:   {opacity:'show',height:'show'},
			speed: 'normal',
			autoArrows: false,
			dropShadows: false,
			delay: 300
		})
	};

	/* Twitter scripts */
	$("div#twitterabout").twitter({
		limit:6,
		ors:twitterUserName,
		rpp:6
	});
	
	$("div#twitterfrom").twitter({
		limit:6,
		from:twitterUserName,
		rpp:6
	});
	
	/* Initialize tabs */
	$("#tabs").tabs();
	
	/* Products and Services, linking the whole div */
	$("div.productitem div.producthead").click(function(event) {
		if($(event.target).is('div')) {
			var goTo = $(this).find("a").attr("href");
			window.location = goTo;
		}	
	});
	
	/* Hover effects */
	$("div.productitem div.producthead").hover(function() {
		$(this).addClass("hover");
	},
	function() {
		$(this).removeClass("hover");
	});
	$("div.registercolumn label, div.registercolumn input").hover(function() {
		$(this).parents("div.registercolumninnerbox").addClass("hover");
	},
	function() {
		$(this).parents("div.registercolumninnerbox").removeClass("hover");
	});
	
	/* Equalize heights */
	$("div.registercolumn div.registercolumninnerbox2").equalHeights();
	$("div.registercolumn div.registerfeatures").equalHeights();
	$("div.beforeandafter ul").equalHeights();
	
	/* IE 8 resized image height fix */
	$("div.testimonialphoto img").removeAttr("width").removeAttr("height");
	
});

// Do on window load
$(window).load( function() {
	
	setTimeout(function(){
		homeAnimationStart();
	}, 1000);
});
