
function addDigg() {
	(function() {
		var s = document.createElement('SCRIPT'), s1 = document.getElementsByTagName('SCRIPT')[0];
		s.type = 'text/javascript';
		s.async = true;
		s.src = 'http://widgets.digg.com/buttons.js';
		s1.parentNode.insertBefore(s, s1);
	})();
}

function fbLikeButton(replacementId) {

	var fbStart = '<iframe src="http://www.facebook.com/plugins/like.php?href=';
	var fbURL = encodeURIComponent($(location).attr('href'));
	var fbEnd = '&amp;layout=button_count&amp;show_faces=false&amp;width=90&amp;action=like&amp;colorscheme=light&amp;height=20" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:90px; height:35px;" allowTransparency="true"></iframe>';
	
	
	fbFinal = fbStart + fbURL + fbEnd;
	
	$('#' + replacementId).html(fbFinal);

}

function getUrlVars2() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
		hash[0] = hash[0].replace("+"," ");
		if (hash[1] == 'on') {
			$('#' + hash[0]).attr('checked');
		}
		
		$('#' + hash[0]).val(hash[1]);
		
    }
}

// Must be run after main date function
function fancyDate(sourceSelector, targetSelector) {
	$(sourceSelector).each(function(){
	str = $(this).html();
	newVar = str.split(" ");
	outputHtml = '<span class="d"> <span class="d' + newVar[0] + '"></span> <span class="m' + newVar[1] + '"></span> </span>';
	$(this).next(targetSelector).html(outputHtml);		
	});

}



function featureTimerSlider() {
	$("#timer").attr('style','');
	$("#timer").animate({"width": "0"}, 7500, "linear", function() {
		cycleFeature();
		featureTimerSlider();
	});
}

	
	
function getFormVars(formId) {
	outputVar = "";
	i=0;
	$('#' + formId + ' input').each(function(){

		if ($(this).attr('type') != "hidden") {
			labelVal = "|" + $(this).next('label').text();
		} else {
			labelVal = "";
		}

		if ($(this).attr('type') == "checkbox" || $(this).attr('type') == "radio") {
			currentVal = $(this).is(":checked") + labelVal;
		} else {
			currentVal = $(this).val() + labelVal;
		}
	
	
		if (i==0) {
			outputVar = $(this).attr('id') + "=" + currentVal;
		} else {
			outputVar = outputVar + "&" + $(this).attr('id') + "=" + currentVal;
		}
		i++;
		
	});
	
	$('#' + formId + ' select').each(function(){
		
		if (i==0) {
			//outputVar = $(this).attr('id') + "_" + $(this).val() + "=" + $(this).find('option:selected').text() + "|" + $(this).next('label').text(); // Store selected text
			outputVar = $(this).attr('id') + "_" + $(this).val() + "=" + $(this).find('option:selected').val() + "|" + $(this).next('label').text(); // Store selected value
		} else {
			//outputVar = outputVar + "&" + $(this).attr('id') + "_" + $(this).val() + "=" + $(this).find('option:selected').text() + "|" + $(this).next('label').text(); // Store selected text
			outputVar = outputVar + "&" + $(this).attr('id') + "_" + $(this).val() + "=" + $(this).find('option:selected').val() + "|" + $(this).next('label').text(); // Store selected value
		}
		
		$('#' + formId + ' textarea').each(function(){
			
			if (i==0) {
				outputVar = $(this).attr('id') + "=" + encodeURIComponent($(this).val());
			} else {
				outputVar = outputVar + "&" + $(this).attr('id') + "=" + encodeURIComponent($(this).val());
			}
			
		});
		
		i++;
	});
	
	return outputVar;
}

	
	
function reRunableFunctions() {
	processDate();
	fancyDate(".sourceDate", ".targetDate");
	tweetLinking();
	makelink();
	jqueryPopups3();
}

$(document).ready(function() {
	processDate();
	fancyDate(".sourceDate", ".targetDate");
	startTheAjaxPagination();
	$('.clear-search').focus(function(){
		$(this).addClass('focus');
	});
	makelink();
	tweetLinking();
	jqueryPopups3();
	
	$('#featured-current').html($('.thumb:first').parent().find('.main').html());
	$('#featured-select-list li:first').addClass('selected');	
	$('#find-event-sbutmit').click(function(){
		$('#find-event-form').submit();
		
	});
	
	// Post pages only
	$(document.getElementById('pt-post')).each(function(){
		$.getScript('/resources/jquery.lightbox-0.5.min.js',function(){
			$('.image-holder a').lightBox();
		});
		$.getScript('http://platform.twitter.com/widgets.js');
		addDigg();
		fbLikeButton('fbLike');	
		(function() {
			var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
			po.src = 'https://apis.google.com/js/plusone.js';
			var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
		})();
		
	});
	
	$('#footer-tweet').jTweetsAnywhere({
		username: 'SportiveCom',
		count: 1
	});
	
	$(document.getElementById('tweetFeed')).each(function(){	
		$('#tweetFeed').jTweetsAnywhere({
			username: 'SportiveCom',
			count: 1
		});
	});
	
	
	// Results Table Sorter
	$(document.getElementById('sportive_results_table')).each(function(){
		$.getScript('/resources/jquery.tablesorter.min.js',function(){
			$("#sportive_results_table").tablesorter(); 
		});
	});
	
	// Homepage only function
	$(document.getElementById('pt-homepage')).each(function(){
		changeFeatured();
		featureTimerSlider();
	});
	

	
});


