$(document).ready(function(){
	
	$('#criteria').change(function() {
		if($(this).val() == 'byNiche') {
			$.get('http://www.referralmatrix.com/index.php', {execute: 'ajax', action: 'niches'}, function(data) {
				$('#criteria').after('&nbsp;<select name="niche" id="niche">'+data+'</select>');
			});
		}
		else {
			$('#niche').remove();
		}
	});
	
	$('a[@name=checkUser]').click(function() {
		link = $(this);
		if($('#user_login').val()) {
			$.get('index.php?execute=ajax&action=checkUser&user='+$('#user_login').val(), function(data) {
					if(data.match('no')=='no') {
				$('#checkUser').remove();
					$(link).prev('#user_login').before('<p id="checkUser">Username is available.</p>');
				}
				else {
					$('#checkUser').remove();
					$(link).prev('#user_login').before('<p id="checkUser"><span>Username is not available.</span></p>');
				}
			});
		}
	});

	$('div.vote_button/a/img').bind('click', submitVote); 

	$('a[@name=banner]').click(function() {
		$.get('index.php?execute=ajax&action=bannerClick&id='+$(this).attr('id'), function(data) {});
	});

	$('a[@name=arsite]').click(function() {
		$.get('index.php?execute=ajax&action=arsiteClick&id='+$(this).attr('id'), function(data) {});
	});

});

function count_click(id, type) {
	$.get('index.php?execute=ajax&action=count_click&id='+id+'&type='+type);
}

function submitVote(evt)
{
	var tmp = evt.target.getAttribute('id').substr(5);
	var widgetId = tmp.substr(0, tmp.indexOf('_'));
	var voteNbr = tmp.substr(tmp.indexOf('_')+1);

	$.get("./frontend/code/submitvote.php",
	{ ratingID: widgetId, value: voteNbr},
		function(data){
			if (data)
			{		
				var v = document.getElementById("vote_" + voteNbr);
				$(v).html(data);
				var vb = document.getElementById("vote_button_" + voteNbr);
				$(vb).html("<img src='./images/thumbup_off.gif'> <img src='./images/thumbdown_off.gif'>");
			}
	}
	);
}


function showLoginNote()
{
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	
	var note=document.getElementById('note');
	note.style.left = (((arrayPageSize[0] - 20 - 400) / 2) + 'px');
	note.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - 30) / 2) + 'px');
	note.style.visibility='visible';
}

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}



function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


function hideLoginNote()
{
	var note=document.getElementById('note');
	note.style.visibility='hidden';
}


