//-------------------------------------------------------------------------
function externalLinks() {
	$('a.external').attr('target', '_blank');
}
//-------------------------------------------------------------------------
function imageControl() {
	var testStatusImage = new Image();
	$(testStatusImage).load(function(){
		$('.handle').removeClass('imageOff');
	})
	.error(function(){
		$('.handle').addClass('imageOff');
	});
	testStatusImage.src = 'http://games.qip.ru/img/x.gif' + '?time=' + new Date().getTime();
}
//-------------------------------------------------------------------------
function prepareMenu(){
	$("#mainMenu div").filter(function(){return $(this).attr("class") != "current"}).hover(function(){
		$(this).addClass('active');
	}, function(){
		$(this).removeClass('active');
	});
}
//-------------------------------------------------------------------------
function prepareSubMenu(){
	$("#submenu li.marked div.holdLine").hover(function(){
		$(this).parent().addClass('active');
	}, function(){
		$(this).parent().removeClass('active');
	});
}
//-------------------------------------------------------------------------
function jqStart(id) {
	if ($(id).length) {
		$(id).jqm();
		if ($.browser.msie) {
			$('div.jqmAlert .jqmClose').hover(
				function(){ $(this).addClass('jqmCloseHover'); }, 
				function(){ $(this).removeClass('jqmCloseHover');
			});
		}
	}
}
//-------------------------------------------------------------------------
// RATING
//-------------------------------------------------------------------------
function initRating() {
	var ratingUpdater = new RatingUpdater($('div#statisticBlock'));
	ratingUpdater.init();
}

//-------------------------------------------------------------------------
// TABS
//-------------------------------------------------------------------------
function reverseTab(id, tabClass, caseClass) {
	tabClass = tabClass || '.item_tabs';
	caseClass = caseClass || '.item_case';
	$(tabClass).each(function (i) {
		$(this).removeClass('active');
	});
	$('.' + id).addClass('active');
	$(caseClass).each(function (i) {
		$(this).hide();
	});
	$('#' + id).show();
}
//-------------------------------------------------------------------------
// CAPTCHA
//-------------------------------------------------------------------------
function generateCaptcha() {
	$('#captchaSource').attr('src', '/captcha?' + microtime() + randomize(1000));
}
//------------------------------------------------------------------
function microtime() {
	var now = new Date().getTime() / 1000;
	var s = parseInt(now);
	return (Math.round((now - s) * 1000) / 1000) + ' ' + s;
}
//------------------------------------------------------------------
function randomize(n) {
	return Math.floor(Math.random() * (n+1));
}
//-------------------------------------------------------------------------
// FORMS
//-------------------------------------------------------------------------
function submitForm(id) {
	$(id).submit();
	return false;
}
//-------------------------------------------------------------------------
// GAMES
//-------------------------------------------------------------------------
function fixFlash() {
	if ($('.bannerPlace embed').length) {
		$('.bannerPlace embed').attr('wmode', 'opaque');
	}
}
//-------------------------------------------------------------------------
// START
//-------------------------------------------------------------------------
if (jQuery){
	$(document).ready(function(){
		prepareMenu();
		prepareSubMenu();
		externalLinks();
		jqStart('#gameSource');
		initRating();
	});
}
//-------------------------------------------------------------------------
function RatingUpdater(ratingContainer){
	this.ratingContainer = ratingContainer;
	this.objectId = ratingContainer.find('span.objectId:first').text();
	this.objectType = ratingContainer.find('span.objectType:first').text();
}
//-------------------------------------------------------------------------
RatingUpdater.prototype = {

	ratingContainer : null,
	url : '/ajax/rating/',
	objectId : null,
	objectType : null,

	init :function(){
		var self = this;
		this.ratingContainer.find('input[type=radio].realStar').rating({
			callback: function(value, link){ 
				self.postVote(value); 
			}
		});
		var yourRating = this.ratingContainer.find('span.yourRating').text();
		if (yourRating) {
			this.ratingContainer.find('span.commentaire').show();
		}
	},

	postVote : function(score){
		var self = this;
		$.post(
			this.url,
			{
				action      : 'vote',
				score       : score,
				objectId    : self.objectId,
				objectType  : self.objectType
			},
			function(data, textStatus){
				self.votePosted(data, textStatus);
			},
			"json"
		);
	},

	votePosted : function(data, textStatus){
		if (data.erorr) {
			alert(data.erorr);
			return;
		}
		if (data.status && data.status == 'ok') {
			this.refreshRatingStatistics(data);
			this.ratingContainer.find('input[type=radio].realStar').rating('readOnly',true);
		}
	},

	refreshRatingStatistics : function(data){
		this.ratingContainer.find('span.totalScore').text(data.total_score);
		this.ratingContainer.find('span.totalVotesCount').text(data.total_count);
		this.ratingContainer.find('span.yourRating').text(data.your_vote);
		this.ratingContainer.find('span.commentaire').show();
	}
}
//-------------------------------------------------------------------------
