
( function( $ ) {
		$.fn.scrollLoad = function( options ) {
		
			var defaults = {
				url : '',
				data : '',
				scrollThreshold : 90,
				onload : function( data, itsMe ) {
					alert( data );
				},
				start : function( itsMe ){},
				continueWhile : function() {
					return true;
				},
				getData : function( itsMe ) {
					return '';
				}
			};

			for( var eachProperty in defaults ) {
				if( options[ eachProperty ] ) {
					defaults[ eachProperty ] = options[ eachProperty ];
				}
			}

			return this.each( function() {
				this.scrolling = false;
				this.scrollPrev = this.onscroll ? this.onscroll : null;
				$( this ).bind( 'scroll', function ( e ) {
					if( this.scrollPrev ) {
						this.scrollPrev();
					}
					if( this.scrolling ) return;
					//var totalPixels = $( this ).attr( 'scrollHeight' ) - $( this ).attr( 'clientHeight' );
					if( Math.round( $( this ).attr( 'scrollTop' ) / ( $( this ).attr( 'scrollHeight' ) - $( this ).attr( 'clientHeight' ) ) * 100 ) > defaults.scrollThreshold ) {
						defaults.start( this );
						this.scrolling = true;
						$this = $( this );
						$.ajax( { url : defaults.url, data : defaults.getData( this ), type : 'post', success : function( data ) {
							$this[ 0 ].scrolling = false;
							defaults.onload( data, $this[ 0 ] );
							if( !defaults.continueWhile( data ) ) {
								$this.unbind( 'scroll' );
							}
						}});
					}
				});
			});
		}
		
		jQuery.fn.extend({
		    disableSelection : function() { 
		    return this.each(function() { 
		            this.onselectstart = function() { return false; }; 
		            this.unselectable = "on"; 
		            jQuery(this).css('user-select', 'none'); 
		            jQuery(this).css('-o-user-select', 'none'); 
		            jQuery(this).css('-moz-user-select', 'none'); 
		            jQuery(this).css('-khtml-user-select', 'none'); 
		            jQuery(this).css('-webkit-user-select', 'none'); 
		    }); 
		} 
		});
		
	})( jQuery );

$( function() {

	$(".clickContact a").live("click", function() {
		$(this).next().toggle();
	});
	
	$( '#email_me' ).click( function(){
		$( '#email_unreg' ).lightbox();
	});	

	$( '#go_unreg' ).click( function(){
		var email = $('input.unreg_email').val();
		 var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			if(reg.test(email) == false) {
				alert('Invalid Email Address');
			return false;
		}
		$( '#im_done' ).html('Please wait...');
		$.ajax({
			type: 'POST',
			url: '/ajax/unreg_newsletter.php',
			data: 'email=' + email,
			success: function(resp){
				$('#im_done').html(resp);
			}
		});
	});

	$('input.select_go').click( function(){
		$('#fill_data').html('<div style="padding:0 30px;color:#555">Loading...</div>');
		$.ajax({
			type :'POST',
			url : '/ajax/ajax_load_price_tracker.php',
			data : 'city_id='+$('div.select_city select').val() + '&product_id='+$('div.select_product select').val(),
			success : function(resp){
				$('#fill_data').html(resp);
			}
		});
	
	}).trigger('click');


	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();
		}
	});
	
	
	$( '#most_discussed' ).show();
	$( '#most_popular' ).hide();
		
	$( '#most_discussed_a' ).live( 'click', function() {
		$( this ).removeClass( 'inactive_nav1' );
		$( this ).addClass( 'active_nav1' );
		
		$( '#most_popular_a' ).addClass( 'inactive_nav1' );
		
		$( '#most_popular' ).hide();
		$( '#most_discussed' ).show();
		
	});
	
	$( '#most_popular_a' ).live( 'click', function() {
		$( this ).removeClass( 'inactive_nav1' );
		$( this ).addClass( 'active_nav1' );
		
		$( '#most_discussed_a' ).addClass( 'inactive_nav1' );
		
		$( '#most_popular' ).show();
		$( '#most_discussed' ).hide();
		
	});
	
	
	$( '#goto' ).live( 'click', function() {
		var pageno = $( '#dest_pg' ).val();
		if( pageno != '' ){
			if(!isNaN( parseInt( pageno ) )){
				url = $( '.current_url_path' ).attr('id') + pageno + "/";
				window.location = url;
				return false;
			}else{
				alert('Please enter integer number.');
				$( '#dest_pg' ).val('');
				return false;
			}
		}else{
			alert('Please enter page number.');
			return false;
		}
	});
	
	
	$( '#goto_search' ).live( 'click', function() {
		var pageno = $( '#dest_pg' ).val();
		if( pageno != '' ){
			if(!isNaN( parseInt( pageno ) )){
				url = $( '.current_url_path' ).attr('id') + '&pn=' + pageno;
				
				window.location = url;
				return false;
			}else{
				alert('Please enter integer number.');
				$( '#dest_pg' ).val('');
				return false;
			}
		}else{
			alert('Please enter page number.');
			return false;
		}
	});
	
	$( '#search' ).live( 'click', function() {
		var search_text = $( '#search_text' ).val();
		if( search_text == '' ){
			alert('Please enter string.');
			$( '#search_text' ).focus();
			return false;
		}
	});
	
	
});



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

