/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function firstPostImageInit() {
	$(".storyentry").each(function() {
		var firstImg = $(this).find('img:first');
		if(firstImg != 0) {
			$(this).parent().parent().find('.img:first').html(firstImg)
		}
	})
}

function validateEmail(email) {
	var splitted = email.match("^(.+)@(.+)$");
	if (splitted == null) return false;
	if (splitted[1] != null ) {
		var regexp_user=/^\"?[\w-_\.]*\"?$/;
		if (splitted[1].match(regexp_user) == null) { return false; }
	}

	if(splitted[2] != null) {
		var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
		if (splitted[2].match(regexp_domain) == null) {
			var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
			if (splitted[2].match(regexp_ip) == null) { return false; }
		}
		return true;
	}
	return false;
}

jQuery(document).ready(function(){
	/*
	$(".post_content img").lazyload({
		effect : "fadeIn"
	});
	*/
	
	$('#accordion .header a.view_post_button').click(function() {
		$(this).parent().parent().parent().next().slideToggle('slow');
		return false;
	}).next().hide();
	
	$('.content a.view_post_button').click(function() {
		$(this).parent().parent().slideToggle('slow');
		return false;
	});

	$('.comments ul').jScrollPane({
		showArrows: false,
		scrollbarWidth: 21
	});

	$('.scroller').bind('click',
		function() {
			$(this).parent().find('ul')[0].scrollBy(parseInt($(this).attr('rel')));
			return false;
		});

	firstPostImageInit();

	$('#menu dl').hover(function() {
		$(this).find('ul:first').css({display: 'block'})
	}, function() {
		$(this).find('ul:first').css({display: 'none'})
	})

	$('.content').hide();
	$('div.post_a_comment').hide();
	$('div.send_to_a_friend').hide();
	$('a.post_a_comment').click(function() {
		$(this).parent().next().next().slideToggle('slow')
		
		return false;
	});
	$('a.send_to_friend').click(function() {
		$(this).parent().next().next().next().slideToggle('slow')

		return false;
	});

	$('a.send_to_a_friend_button').click(function() {
		var the_form = $(this).parent()

		var el = the_form.find('input[name=your_name]')
		if(el.attr('defaultValue') == el.attr('value') || el.attr('value').length < 3) {
			el.animate( { opacity: 0.2 }, 175 ).animate( { opacity: 1 }, 150 );
			return false;
		}
		el = the_form.find('input[name=your_email]')
		if(el.attr('value').length < 3 || !validateEmail(el.attr('value'))) {
			el.animate( { opacity: 0.2 }, 175 ).animate( { opacity: 1 }, 150 );

			return false;
		}
		el = the_form.find('input[name=friends_email]')
		if(el.attr('value').length < 3 || !validateEmail(el.attr('value'))) {
			el.animate( { opacity: 0.2 }, 175 ).animate( { opacity: 1 }, 150 );

			return false;
		}
		el = the_form.find('textarea')
		if(el.attr('defaultValue') == el.attr('value') || el.attr('value').length < 3) {
			el.animate( { opacity: 0.2 }, 175 ).animate( { opacity: 1 }, 150 );
			return false;
		}

		the_form.find('input[type=submit]').click();

		return false;
	})

	$('a.send_a_comment_button').click(function() {
		var the_form = $(this).parent().parent()

		var el = the_form.find('input[name=author]')
		if(el.attr('defaultValue') == el.attr('value') || el.attr('value').length < 3) {
			el.animate( { opacity: 0.2 }, 175 ).animate( { opacity: 1 }, 150 );
			return false;
		}
		el = the_form.find('input[name=email]')
		if(el.attr('value').length < 3 || !validateEmail(el.attr('value'))) {
			el.animate( { opacity: 0.2 }, 175 ).animate( { opacity: 1 }, 150 );
			
			return false;
		}
		el = the_form.find('textarea')
		if(el.attr('defaultValue') == el.attr('value') || el.attr('value').length < 3) {
			el.animate( { opacity: 0.2 }, 175 ).animate( { opacity: 1 }, 150 );
			return false;
		}

		the_form.find('input[type=submit]').click();
		
		return false;
	})
});

function inputOnBlur(element) {
	if(element.value == "") {
		element.value = element.defaultValue;
	}
}
function inputOnFocus(element) {
	if(element.value == element.defaultValue) {
		element.value = "";
	}
}

var hdrimgs_baseURL;
var hdrimgs = [];
var hdrindex = 0;
function headerChngImg() {
	var curIndex = hdrindex;
	
	if(++hdrindex > hdrimgs.length - 1) {
		hdrindex = 0;
	}
	//alert(hdrimgs[hdrindex])
	$('<img />')
    .attr('src', hdrimgs_baseURL + hdrimgs[hdrindex])
    .load(function(){
		var container = $('#header_img');
		var new_img = this
        container.animate({opacity: 0.1}, 'slow', 'linear', function() {
			container.html( $(new_img) ).animate({opacity: 1})
		});
    });
}
function headerCarousel(imgs, baseURL) {
	hdrimgs_baseURL = baseURL;
	hdrimgs = imgs;
	setInterval('headerChngImg()', 5000);
}