/*most functions are in functions.js*/
/*default.js contains most onload events, some are still scattered throughout the code*/
/*cfg object setup takes place after this file loads, please init.push(function(){ <code> }); to correctly apply the cfg variable; see head.php*/


/*js regex setup*/
var regex = new Object();
regex['first_word']=/[a-z]*/i;

/*start of javascript after cfg variable collection*/

/*ajaxCtrl - object that holds any persistent ajax information, usually used to abort existing requests*/
var ajaxCtrl = new Object();
ajaxCtrl['test']='1';

/*tmpFunc - object that holds all the tempfunctions that are persistent but not persistent enough to require a global namespace*/
var tmpFunc = new Object();




//capture keypresses
$(document).keypress(function (event){ 
	//capture return
	if('true'==cfg.sv_capture_key_013) {
		return event.keyCode == 13 ? false : true;
	}
	return true;
});

init=new Array();


/*inc-head.php has a <script> area for JS/php hybrid code*/
$(document).ready(function(){
	/*dragon*/
	/*in an effort to simplify js across the website assigning a method to init, these methods will be called on $(document).ready*/
	$.each(init, function(i1, i2) { if('function'==typeof(i2)){i2.apply();} });
	/*nogard*/
	
	setTimeout( function() { $("[tabindex='1']").focus().select(); }, 50 );
	
	//getMousePosition(100);
	
	/*prep css.js*/
	//onLoadCSS();
	/*Lightbox*/
	$("a[rel^='prettyPhoto']").bind('click', function(){
		lockScroll(true);
	});
	$("a[rel^='prettyPhoto']").prettyPhoto({
		animationSpeed: 'normal', /* fast/slow/normal */
		opacity: 0.80, /* Value between 0 and 1 */
		autoplay_slideshow: false,
		showTitle: false, /* true/false */
		allowresize: false, /* true/false */
		default_width: cfg.content_width,
		default_height: getViewport()[1], 
		counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
		theme: 'light_rounded', /* light_rounded / dark_rounded / light_square / dark_square / facebook */
		hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
		wmode: 'opaque', /* Set the flash wmode attribute */
		autoplay: false, /* Automatically start videos: True/False */
		modal: false, /* If set to true, only the close button will close the window */
		social_tools: '',
		changepicturecallback: function(){}, /* Called everytime an item is shown/changed */
		callback: function(){lockScroll(false);} /* Called when prettyPhoto is closed */

	});
	$.ajaxSetup({
		ifModified:true,
		cache:false,
		timeout:5000
	});
	/*menu highlighter*/
	if("undefined"!=typeof(cfg.sv_selected)) {
		$.each(cfg.sv_selected, function(index){
			$('[menuid="'+index+'"]').addClass('selected');
		});
	}
	
	
	/*Share*/
	$(".share").bind('click', function(){
		$("#sharebox").val($(this).attr('link'));
		$("#sharebox").attr("title", $(this).attr('title'));
		$("#shareReturn").attr('value', $(window).scrollTop());
		$.scrollTo($("#shareblock"), {duration: 800, axis: 'y'});
					
		getTinyLink($("#sharebox").val(), $("#sharebox").attr('title'));
			
		$("#sharebox").focus().select();
	});
	
	$(".shareButton").bind('mouseover', function(){
		if($("#sharebox").val().length==0){
			getTinyLink(cfg.sv_request_uri, 'title');
			$("#sharebox").val(cfg.sv_request_uri);
		}
	});
	
	$("#shareReturn").bind('click', function(){
		$.scrollTo($(this).attr('value'), {duration: 800, axis: 'y'});
	});
	
	$("#tinybox").bind('click', function(){
		$(this).focus().select();
	});
	$("#tinybox").bind('click', function(){
		$(this).focus().select();
	});
	
	$("#retryTinyBox").bind('click', function(){
		getTinyLink($("#sharebox").val(), $("#sharebox").attr('title'));
	});
	
	
	
	/*activity feed switch controls*/
	$(".activityFeed_toggle").toggle(
		function() {
			$(this).text('Showing '+cfg.cv_user_name+" Only");
			$("#activityFeed dd[user!="+cfg.cv_user_id+"]").hide();
			touchLastDiv();
		},
		function(){
			$(this).text('Showing All');
			$("#activityFeed dd").show();
			touchLastDiv();
		}
	);
	
	$(".activityFeed_more").bind('click', function(){
		$.post(cfg.base_url+"includes/ajax/getActivityFeed.php", {'offset':$('#activityFeed').attr('offset'), 'limit':$('#activityFeed').attr('limit'), 'u':cfg.cv_user_id}, function(data){
			$('#activityFeed').append(data);
			$('#activityFeed').attr('offset', parseInt($('#activityFeed').attr('offset'))+parseInt($('#activityFeed').attr('limit')));
			touchLastDiv();
		});
	});
	
	/*auctuion create*/
	$(".auctionField").bind('change', function(){
		if($(this).val().length>=1){$(this).attr('status', '1');}
		else {$(this).attr('status', '0');}
	})
	
	/*feedback*/
	$("#feedback_comment").bind('keyup', function(){
		$("#feedback_charCount").text(150 - $(this).val().length+" characters remaing");
		if($(this).val().length<=150) {$("#feedback_submit").removeAttr('disabled');}else{$("#feedback_submit").attr('disabled', true);}
	});
	
	/*check for display table compatabilty*/
	if($("#globalmenu").height()-10>$("#globalmenu dd:first-child").height()) {
		js_warning("Elements on this page cannot display correctly with your current browser, please update your browser version.");
	}
	
	
	/*Default Values*/
	$(".default_value").each(function(){
		if(""==$(this).val()) {
			$(this).val($(this).attr('default'));
		}
	}).bind('focus', function(){
		if($(this).val()==$(this).attr('default')){ $(this).val(''); }
	}).bind('blur', function(){
		if($(this).val()==''){ $(this).val($(this).attr('default')); }
	});
	$(".defaulted_form").bind('submit', function() {
		$(".default_value").each(function() {
			if($(this).val()==$(this).attr('default')){ $(this).val(''); }
		});
		return true;
	});
	
	/*new defualt system*/
	$("[default]").live('focus', function() {
		if($(this).val()==$(this).attr('default')) {
			$(this).val('');
		}
	}).live('blur', function() {
		if($(this).val().length==0) {
			$(this).val($(this).attr('default'));
		}	
	});
	
	// WHAT DO: update ajax calls to apply trigger document update
	
	$(document).bind('update', function() {
		$("textarea").each(function() {
			$(this).autoResize({
				onResize: function() {},
				animateCallback: function() {},
				animate: true,
				animateDuration : 150,
				extraSpace : 20,
				limit: (getViewport()[1]-100)
			});
		});
	});//.trigger('update');
	/*must be delayed otherwise it's too close to the pageload to function correctly*/
	setTimeout("$(document).trigger('update');", 50);
	
	$('.deleteConfirm').live('click', function(){
		$(this).after('<span class="deleteComment pointer">[Yes]</span>\t\t<span class="pointer">[No]</span>');
		$(this).next().attr('data', $(this).attr('data'));
		
		$(this).next().next().bind('click', function() {
			$(this).parent().children('div').show();
			$(this).parent().children('span').remove();
		});
	
		$(this).before('<span>are you sure?<br></span>').hide();
		
	});
	$('.deleteComment').live('click', function(){ deleteComment($(this)); });
	
	$(".favoriteGame").bind('click', function() {
		change_game_favorite($(this), 'update_game_favorite');
	});
	
	/*CSS horizontal menu vertical dropdown menu child menu width adjust*/
	$(".hmvd").each(function(i, object) {
		$(object).find('li>ul>li').width($(object).find('li').width());
	});
	
	/*sorry about the confusion, we were sticking purely with $(document), but had to change to $(window) when it was discovered that IE does not trigger the scroll event for $(document);*/
	//$(window).bind('bottom.pewpew', function(){});
	$(window).bind('scroll', function() {
		if('undefined'!=typeof($(window).data('events').bottom)) {
			moreContentLoading.show();
		}
		
		var docHeight=$(document).height();
		var docScroll=$(document).scrollTop();
		var winHeight=$(window).height();
		if(docHeight < (docScroll + winHeight + 2)) {
			if(0==jQuery.active) {
				window.clearInterval($(document).data('scroll_trigger'));
				$(document).data('scroll_trigger', setTimeout( function(){$(window).trigger('bottom')}, 0));
			} else {
				window.clearInterval($(document).data('scroll_trigger'));
				$(document).data('scroll_trigger', setTimeout( function(){$(window).trigger('scroll')}, 750));
			}
		}
	}).trigger('bottom');
	
	/*
	$(document).bind('click', function() {
		$(".theme[rel='stylesheet']").attr('href', 'http://herebegames.com/includes/inc-theme.php?t=1');
	});
	$(document).bind('dblclick', function() {
		$(".theme[rel='stylesheet']").attr('href', 'http://herebegames.com/includes/inc-theme.php?t=0');
	});
	*/
});
