var rotatingTimeoutBinding = null;

var prepRotatingImages = function() {
	var items = $('#rotating_images .rotating-item').not('#rotating_images_controls').length;
	var spans = '';
	for (i = 1; i <= items; i++) {
		if (i != 1 && i % 5 == 1) {
			spans += '<br /><br />';	
		}
		spans += '<span>' + i + '</span>';
	}
	$('#rotating_images_controls').append(spans).show();
	$('#rotating_images .rotating-item:first').show();
	bindRotatingImagesControl();
	rotateImages(items-1);
};

var bindRotatingImagesControl = function() {
	$('#rotating_images_controls').click(function(e) {
		e.stopPropagation();
		var index = parseInt($(e.target).text()) - 2;
		clearTimeout(rotatingTimeoutBinding);
		rotateImages(index);
	});
};

var rotateImages = function(index) {
	var containers = $('#rotating_images .rotating-item').not('#rotating_images_controls');
	index = (index + 1) % containers.size();
	$(containers.hide().get(index)).fadeIn(1500);
	$($('#rotating_images_controls span').removeClass('active').get(index)).addClass('active');
	rotatingTimeoutBinding = setTimeout(function(){ rotateImages(index); }, 10000);
};

var colorTokensInNav = function() {
	$('#main_nav > ul > li > a').each(function(){
		var text = $(this).text().replace('+','<span class="yellow">+</span>');
		$(this).html(text);
	});
};


$(document).ready(function() {
	prepRotatingImages();
	colorTokensInNav();
	
	if($("div.scrollable").length) {
		$("div.scrollable").scrollable({ size: 1 });
	}
	
	$('li.nav-selected').parents('li').addClass("nav-selected");
		
	prepScrollbars();
	prepEmailThis();
	prepLightWindows();
	colorizePressReleasesList();
	$('#press_releases thead').click(function() {
		$('#press_releases tbody tr').css('background-color', '#ffffff');
		colorizePressReleasesList(); 
	} );
});


var prepScrollbars = function() {
	$('#main_content').scrollbuttons(350); //pass in height in pixels of the viewport. This is needed for crossbrowser compatibility.
}

var prepLightWindows = function() {
	$('a.lightwindow').colorbox({fixedWidth:750, fixedHeight:775, iframe:true, modalClose:"x"});
}

var prepEmailThis = function() {
    $('#email_this .close_button').bind('click', function() {
        $('#email_this').hide();
    });
}

var colorizePressReleasesList = function() {
	$('#press_releases tbody tr:even').css('background-color', '#fdf9cb');
};

var emailThis = function() {
    $('#email_this').toggle();
    $('#message').val(jQuery.trim( $('#main_content').text() )+'\n\n'+window.location);
}

var emailThisSubmit = function() {

    $('#email_this #loader').show();
    $("#email_this_error").html("");
    
    $.ajax({
       url: '/controllers/email_this.php',
       type: 'post',
       dataType: 'json',
       data: {
            from_name: $('#from_name').val(),
            from_email: $('#from_email').val(),
            to_email: $("#to_email").val(),
            subject: $("#subject").val(),
            message: $("#message").val(),
            ccmCaptchacode: $("#ccmCaptchacode").val()
        },
        success: function(data) {
            if(data.sent == true) {
                alert('your message has been sent');
                $('#email_this').toggle();
            } else if(data.errors) {
               for(var i=0; i<data.errors.length; i++) {
                    $("#email_this_error").append("<br />"+data.errors[i]);
               }
               $("#email_this_error").show();
            }
        },
        error: function() {
            $("#email_this_error").html("Email could not be sent due to a server error.");
        },
       complete: function() {
            $('#email_this #loader').hide();
       }
    });
}
