$( function() {
	
	
	// Start For Image-video Gallery
	$( '.gallyImage' ).live( 'click', function() {
			$( '.gallyVideo' ).removeClass( 'active' ).addClass( 'inactive' );
			$( this ).removeClass( 'inactive' ).addClass( 'active' );
			$( '#showVideo' ).hide();
			$( '#showImage').show();
		});
		
	$( '.gallyVideo' ).live( 'click', function() {
			$( '.gallyImage' ).removeClass( 'active' ).addClass( 'inactive' );
			$( this ).removeClass( 'inactive' ).addClass( 'active' );
			$( '#showImage' ).hide();
			$( '#showVideo').show();
		});	
	
	// End For Image-video Gallery

	if( $( 'div.facebook-register' ).length > 0 ) {
		$( 'div.facebook-register' ).lightbox();
	}
	if( $( 'div.feed-err-message' ).length > 0 ) {
		show_feedback();
	}
	if( $( 'div.feedback_thanks_lightbox' ).length > 0 ) {
		$( 'div.feedback_thanks_lightbox' ).lightbox();
	}

	$( 'form.form_validate' ).each( function() {
		$( this ).validate();
	});

	$( 'input[name=user_login]' ).bind( 'keypress', function() {
		$( 'span.login-message' ).hide();
		$( 'a.login-avail' ).show();
	}).bind( 'blur', function() {
		$( 'a.login-avail' ).trigger( 'click' );
	});

	$( 'a.login-avail' ).click( function() {
		var userLogin = $.trim( $( 'input[name=user_login]' ).val() );
		if( userLogin == '' ) {
			return;
		}
		$.ajax( {
			url : '/ajax/check_login.php',
			type : 'post',
			data : 'l='+userLogin,
			success : function( resp ) {
				$( 'span.login-message' ).html( resp ).show();
				$( 'a.login-avail' ).hide();
			}
		});
	});

	$( 'form.register_form' ).validate({
		rules: {
			user_password : {
				required : true,
				minlength : 5
			},
			confirm_user_password : {
				required : true,
				minlength : 5,
				equalTo : "#user_password"
			},
			user_login : {
				required : true,
				minlength : 3
			}
		},
		submitHandler : function() {
			if( $.trim( $( 'span.login-message' ).text() ) != 'Available' ) {
				alert( 'Login already exists, try another!' );
				$( 'input[name=user_login]' ).focus();
				return false;
			}
			$( 'form.register_form' ).bind( 'submit', function() {
				return true;
			}).submit();
		}
	});
});

function search()
{
	var search_string=document.getElementById("search-term").value;
	search_string=removeSpaces(search_string);
	search_string=trim(search_string);
	window.location="http://"+window.location.host+"/search?q="+search_string;
}

function trim (str, charlist)
{
	var whitespace, l = 0, i = 0;
	str += '';
	if (!charlist) {
		// default list
		whitespace = " \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000";
	} else {
		// preg_quote custom list
		charlist += '';
		whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '$1');
	}
	l = str.length;
	for (i = 0; i < l; i++) {
		if (whitespace.indexOf(str.charAt(i)) === -1) {
			str = str.substring(i);
			break;
		}
	}
	l = str.length;
	for (i = l - 1; i >= 0; i--) {
		if (whitespace.indexOf(str.charAt(i)) === -1) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}

function removeSpaces(string)
{
	var nsText = string;
	nsText = nsText.replace(/(\n\r|\n|\r)/gm,"<1br />");
	nsText = nsText.replace(/\t/g," ");
	re1 = /\s+/g;
	nsText = nsText.replace(re1," ");
	re2 = /\<1br \/>/gi;
	nsText = nsText.replace(re2, "\n");
	return nsText;
}

$.fn.center = function () {
	if( !window.centerElements ) {
		window.centerElements = [];
	}
	for( var i = 0; i < this.length; i ++ ) {
		window.centerElements.push( this[ i ] );
	}

	var centerlize = function() {
		$( window.centerElements ).each( function() {
			var $this = $( this );
			$this.css( { position : 'absolute' } ).css( {
				top : ( ( $( window ).height() - $this.height() ) / 3 ) + $( window ).scrollTop() + "px",
				left : ( ( $( window ).width() - $this.width() ) / 2 ) + $( window ).scrollLeft() + "px"
			});
		});
	};

	$( window ).bind( 'resize', centerlize );
	setTimeout( function() {
		$( window ).trigger( 'resize' );
	}, 100 );
	return this;
};

$.fn.lightbox = function( options ) {
	var defaults = { backColor : '#999', opacity : .5 };
	for( var eachProp in options ) {
		if( defaults[ eachProp ] ) {
			defaults[ eachProp ] = options[ eachProp ];
		}
	}
	defaults.alphaFilter = defaults.opacity * 100;
	$( '<div style="position:absolute;width:100%;height:' + $( document ).height() + 'px;left:0px;top:0px;z-index:1000;background-color:' + defaults.backColor + ';opacity:' + defaults.opacity + ';filter: alpha(opacity = ' + defaults.alphaFilter + ');" id="lightbox-overlay"></div>' ).appendTo( 'body' ).click( function() {
		$( document ).trigger( 'closeLightbox' );
	});
	$( this ).css( 'z-index', 1001 ).show().center();
	$( 'a.closeLightbox' ).click( function( e ) {
		e.preventDefault();
		$( document ).trigger( 'closeLightbox' );
	});
	var $selector = $( this );
	$( document ).unbind( 'closeLightbox' ).bind( 'closeLightbox', function() {
		$( '#lightbox-overlay' ).remove();
		$selector.hide();
	});
};

function afterLogin() {
	history.go( 0 );
}

function show_feedback( )
{
	$( '#feedback_content' ).lightbox();				
}

function vote_article(action, post_id)
{
	var postdata="action="+action+"&post_id="+post_id;
	$.ajax({type: "POST", url: "/ajax/vote_article.php", data: postdata, success: function(msg){var id ='#vote-article';	$(id).html(msg);}});
}

/*
Auto Refresh Page with Time script
By JavaScript Kit (javascriptkit.com)
Over 200+ free scripts here!
*/

//enter refresh time in "minutes:seconds" Minutes should range from 0 to inifinity. Seconds should range from 0 to 59
var limit="15:00"

if (document.images){
var parselimit=limit.split(":")
parselimit=parselimit[0]*60+parselimit[1]*1
}
function beginrefresh(){
if (!document.images)
return
if (parselimit==1)
window.location.reload()
else{
parselimit-=1
curmin=Math.floor(parselimit/60)
cursec=parselimit%60
if (curmin!=0)
curtime=curmin+" minutes and "+cursec+" seconds left until page refresh!"
else
curtime=cursec+" seconds left until page refresh!"
window.status=curtime
setTimeout("beginrefresh()",1000)
}
}

window.onload=beginrefresh

