$.fn.splitUp = function(splitBy,wrapper) {
	$all= $(this).find('>*');
	var fragment=Math.ceil($all.length/splitBy);
	for (i=0;i<fragment;i++)
		$all.slice(splitBy*i,splitBy*(i+1)).wrapAll(wrapper);
	return $(this);
}

$(document).ready(function(){

	// Newsletter field
	$(".newsletter .text-field").click(function() {
		if ($(this).attr('value') == 'ENTER YOUR EMAIL') {
			$(this).attr('value', '');
		}
	});

	$(".newsletter .text-field").blur(function() {
		if ($(this).attr('value') == '') {
			$(this).attr('value', 'ENTER YOUR EMAIL');
		}
	});

	// Subnav hover
	$("nav ul li").mouseover(function () {
		$(this).addClass('hover');
	});
	$("nav ul li").mouseout(function () {
		$(this).removeClass('hover');
	});

	// Subnav image cycle
	$("nav li li a").mouseover(function() {
		if ($(this).attr('data-image')) {
			$(this).parent('li').parent('ul').siblings('div').children('img').attr('src', $(this).attr('data-image'));
		}
	});

	// Image panel overlay
	$(".single a.hover-overlay").hover(function() {
		$(this).addClass('selected');
		$(this).append('<span class="overlay"></span>');
	}, function () {
		$(this).removeClass('selected');
		$(this).children('span.overlay').remove();
	});

	// Gallery thumbnail click
	$("#gallery-thumbnails .gallery-thumb a").click(function() {
		$("#gallery-main img").attr('src', $(this).attr('href'));
		return false;
	});

	// Homepage cycle
	$("#cycle").cycle({
		fx:      'fade',
		speed:   'slow',
		timeout: 0,
		height: '278',
		pager:   '#cycle-pagination',
		cleartype: true,
		cleartypeNoBg: true
	});

	// Twitter cycle
	$("#tweets").cycle({
		fx:      'scrollHorz',
		next:    '.box-cycle-next',
		prev:    '.box-cycle-prev',
		timeout: 5000,
		speed:   500,
		easing:  'swing',
		cleartype: true,
		cleartypeNoBg: true
	});

	// Testimonials cycle
	$("#testimonials-cycle").splitUp(2,'<div>').cycle({
		fx:      'scrollUp',
		timeout: 7000,
		speed:   1000,
		easing:  'swing',
		cleartype: true,
		cleartypeNoBg: true
	});

	// Gallery thumbs cycle
	$("#gallery-thumbnails .slider").cycle({
		fx:      'scrollHorz',
		next:    '.box-cycle-next',
		prev:    '.box-cycle-prev',
		timeout: 0,
		speed:   500,
		easing:  'swing',
		cleartype: true,
		cleartypeNoBg: true
	});

});

