$(document).ready(function() {
	
	// Fancybox
	$('.fancybox').fancybox();

	// IE Distorted Text Fix
	(function($) {
		$.fn.customFadeIn = function(speed, callback) {
			$(this).fadeIn(speed, function() {
				if(!$.support.opacity)
					$(this).get(0).style.removeAttribute('filter');
				if(callback != undefined)
					callback();
			});
		};
		$.fn.customFadeOut = function(speed, callback) {
			$(this).fadeOut(speed, function() {
				if(!$.support.opacity)
					$(this).get(0).style.removeAttribute('filter');
				if(callback != undefined)
					callback();
			});
		};
		$.fn.customFadeTo = function(speed,to,callback) {
			return this.animate({opacity: to}, speed, function() {
				if (to == 1 && jQuery.browser.msie)
					this.style.removeAttribute('filter');
				if (jQuery.isFunction(callback))
					callback();
			});
		};
	})(jQuery);


	// Email Sign-Up Form
	$('#email_signup form').submit(function() {
		var $this = $(this);
    	var data = $this.serialize();

		$.ajax({
			url: "<?php echo base_url(); ?>email/signup",
			type: "POST",
			dataType: "html",
			data: data,
			success: function(data) {
				$('#email_signup p').html(data);
			},
			error: function(data) {
				$('#email_signup p').html('There was an error');
			}

		});
		return false;
	});



	// Contact Form
	function checkInput() {
		var selected = $(this).attr('name');
		switch(selected) {
			case 'name': var content = 'Name'; break;
			case 'phone': var content = 'Phone'; break;
			case 'email': var content = 'E-Mail Address'; break;
			case 'comments': var content = 'Comments'; break;
		}
		
		var select_value = $(this).val();
		if(select_value == content) {
			$(this).val('');
		} else if(select_value == '') {
			$(this).val(content);
		}
	}
	
	$("input").bind('focusin', checkInput);
	$("input").bind('focusout', checkInput);
	$("textarea").bind('focusin', checkInput);
	$("textarea").bind('focusout', checkInput);
	
	$('#checkbox').click(function() {
		if($('#copy_self').hasClass('off')) {
			$('#copy_self').val('1').addClass('on').removeClass('off');
			$('#checkmark').css('display','block');
		} else if($('#copy_self').hasClass('on')) {
			$('#copy_self').val('0').addClass('off').removeClass('on');
			$('#checkmark').css('display','none');
		}
	});
	
	
	(function($) {
		// Blogroll
		var list = $('#bloglist');
		var list_items = $('#bloglist li').size() - 2;

		$(".blog_down").click(function() {
			var top = parseInt($('#bloglist').css('top'));
			if(top > -(list_items*52)) {
				if(top % 52 == 0) {
					$('#bloglist').animate({'top':top-52+'px'}, 500);
				}
			}
			return false;
		});
		$(".blog_up").click(function() {
			var top = parseInt($('#bloglist').css('top'));
			if(top < -1) {
				if(top % 52 == 0) {
					$('#bloglist').animate({'top':top+52+'px'}, 500);
				}
			}
			return false;
		});
	})(jQuery);
	
	(function($) {
		// linkroll
		var list = $('#linklist');
		var list_items = $('#linklist li').size() - 8;

		$(".link_down").click(function() {
			var top = parseInt($('#linklist').css('top'));
			if(top > -(list_items*52)) {
				if(top % 52 == 0) {
					$('#linklist').animate({'top':top-52+'px'}, 500);
				}
			}
			return false;
		});
		$(".link_up").click(function() {
			var top = parseInt($('#linklist').css('top'));
			if(top < -1) {
				if(top % 52 == 0) {
					$('#linklist').animate({'top':top+52+'px'}, 500);
				}
			}
			return false;
		});	
	})(jQuery);
	
	(function($) {
		// articleroll
		var list = $('#articlelist');
		var list_items = ($('#articlelist li').size()/2) - 2;

		$(".art_down").click(function() {
			var top = parseInt($('#articlelist').css('top'));
			if(top > -(list_items*104)) {
				if(top % 106 == 0) {
					$('#articlelist').animate({'top':top-106+'px'}, 500);
				}
			}
			return false;
		});
		$(".art_up").click(function() {
			var top = parseInt($('#articlelist').css('top'));
			if(top < -1) {
				if(top % 106 == 0) {
					$('#articlelist').animate({'top':top+106+'px'}, 500);
				}
			}
			return false;
		});
	})(jQuery);
	
	// Quote Box
	var containers = $(".quote-wrap");
	containers.css('display','none').addClass('qoff');
	containers.first().css('display','block').removeClass('qoff').addClass('qon');
	var quote_count = $('.quote-wrap').size();
	quote_count = parseInt(quote_count);
	if(quote_count > 1) { var quote_int = window.setInterval(quote_up, 2500); }	
	
	function quote_down() {
		var showing = $('.qon');
		var active = $('.qon').attr('id').replace('q','');
		active = parseInt(active);
		
		if(active == 1) { var next = quote_count; }
		else { var next = active-1; }
	
		showing.fadeOut('100').removeClass('qon').addClass('qoff');
		$('#q'+next).fadeIn('750').removeClass('qoff').addClass('qon');
				
		quote_int = window.clearInterval(quote_int);
		quote_int = window.setInterval(quote_up, 2500);
		return false;
	}
	$(".quote_down").click(quote_down);
	
	function quote_up() {
		var showing = $('.qon');
		var active = $('.qon').attr('id').replace('q','');
		active = parseInt(active);
		
		if(active == quote_count) { var next = 1; }
		else { var next = active+1; }
		
		showing.fadeOut('100').removeClass('qon').addClass('qoff');
		$('#q'+next).fadeIn('750').removeClass('qoff').addClass('qon');
		
		quote_int = window.clearInterval(quote_int);
		quote_int = window.setInterval(quote_up, 2500);
		return false;
	}
	$(".quote_up").click(quote_up);
	
	// Slider Box
	var speed = 13000;
	var containers = $(".slider_content");
	containers.css('display','none').addClass('off');
	containers.first().css('display','block').removeClass('off').addClass('on');
	var slider = $("#slider");
	var boxes = $(".sb");
	var slide_count = $('.slider_content').size();
	slide_count = parseInt(slide_count);
	if(slide_count > 1) { var slide_int = window.setInterval(slide_right, speed); }
	
	function slide_left() {
		var showing = $('.on');
		var active = $('.on').attr('id').replace('sc','');
		active = parseInt(active);
		
		if(active == 1) { var next = slide_count; }
		else { var next = active-1; }

		showing.fadeOut('100').removeClass('on').addClass('off');
		$('#sc'+next).fadeIn('750').removeClass('off').addClass('on');
		 
		boxes.removeClass('active');
		var box = '#sb'+next;
		$(box).addClass('active');
		
		slide_int = window.clearTimeout(slide_int);
		slide_int = window.setTimeout(slide_right, speed);
		return false;
	}
	$("#slider_left").click(slide_left);
	
	function slide_right() {
		var showing = $('.on');
		var active = $('.on').attr('id').replace('sc','');
		active = parseInt(active);
		
		if(active == slide_count) { var next = 1; }
		else { var next = active+1; }
		
		showing.fadeOut('100').removeClass('on').addClass('off');
		$('#sc'+next).fadeIn('750').removeClass('off').addClass('on');
		 
		boxes.removeClass('active');
		var box = '#sb'+next;
		$(box).addClass('active');
		
		slide_int = window.clearTimeout(slide_int);
		slide_int = window.setTimeout(slide_right, speed);
		return false;
	}
	$("#slider_right").click(slide_right);
	$('.next_slide').click(slide_right);
	$('#slider').hover(function() {
		// MouseEnter
		slide_int = window.clearInterval(slide_int);
	}, function() {
		// MouseLeave
		slide_int = window.setInterval(slide_right, speed);
	});
	$('#slider_bar').hover(function() {
		// MouseEnter
		slide_int = window.clearInterval(slide_int);
	}, function() {
		// MouseLeave
		slide_int = window.setInterval(slide_right, speed);
	});
	
	
	$(".sb").click(function() {
		var id = $(this).attr('id').replace('sb','');
		var content = '#sc'+id;
		var box = '#sb'+id;
		
		var showing = $(".on");
		showing.fadeOut('100').removeClass('on').addClass('off');
		$(content).fadeIn('750').removeClass('off').addClass('on');
		
		boxes.removeClass('active');
		$(box).addClass('active');
		return false;
	});
	
	
});
