// IVW - PIXEL
function ReloadPixelImages ()
{
	var IVW="http://mtv.ivwbox.de/cgi-bin/ivw/CP/mtvgameawards_de";
	document.szmimages.src = IVW+"?r="+escape(document.referrer)+"&d="+(Math.random()*100000);
}


/*********************************************************************
Dependencies: Prototype 1.6.x or newer, Scriptaculous 1.8.1 or newer
**********************************************************************/

var Voting = Class.create();

Voting.prototype =
{
	initialize: function(votedCategories)
	{
		votedCategories = votedCategories.without(0);

		this.options = Object.extend(
		{
			votCat: null,
			openingInProgress: false,
			catFuncsClasses: null,
			catFuncsClassesToOpen: null,
			votedCatArray: votedCategories,
			flashPlayerUris: new Array(237122, 187982, 194209, 138409, 97595), //not in use
			flashSiteUrl: 'trailer/game_trailer_', //add .htm on the end (after a number)
			currentPlayer : null,
			votingNumber: 10
		}, arguments[1] || {}); //eigentlich keine options, sondern instanzvariablen!!! ändern!

		this.lastPlayerBackground = null;
		this.votedCounter = votedCategories.size();
		this.raffle = false;
		this.prohibitVoting = true;
		this.openVotePopup = true;
		this.dontShowPopup = true;

	},

	slideUpVotes: function()
	{
		var childElems = $('voting_container').childElements();
		for(var i = 1; i <= childElems.size(); i++)
		{
			var votCatId = 'catVotes_' + i;
			Effect.SlideUp(votCatId, { duration: 0.5 });
		}
		setTimeout("", 150000);
		voting.prohibitVoting = false;
		voting.dontShowPopup = false;
	}
}

//Event.observe(window, 'load', voting.startAccordion);

/**
* Opens a category and replaces background image from catFuncs
*/
var openVotCats = function(votCatElem)
{
	if(voting.options.openingInProgress == true)
		return;
	voting.options.openingInProgress = true;
	split_array = votCatElem.id.split("_");
	var cat_number = split_array.last();
	var cat_funcs = 'catFuncs_' + cat_number;

	if(voting.options.votCat != null)
	{
		split_array = voting.options.votCat.id.split("_");
		var old_cat_number = split_array.last();
		var old_cat_funcs = 'catFuncs_' + old_cat_number;
		voting.options.catFuncsClasses = voting.options.catFuncsClassesToOpen;
		voting.options.catFuncsClassesToOpen = $(cat_funcs).classNames().toArray();
	}else
		voting.options.catFuncsClassesToOpen = $(cat_funcs).classNames().toArray();

	if(voting.options.votCat != null && voting.options.votCat != votCatElem)
	{
		ReloadPixelImages ();	// IVW
		dispatcher.sendCall(); // Omniture

		Effect.SlideDown(votCatElem, { duration: 0.5 });
		closeVotCats(voting.options.votCat);
		$(cat_funcs).removeClassName(voting.options.catFuncsClassesToOpen.last());
		cat_number = cat_number * 1;
		if(voting.options.votedCatArray.indexOf(cat_number) > -1)
		{
			$(cat_funcs).addClassName('catFuncsCloseVoted');
		}else
			$(cat_funcs).addClassName('catFuncsClose');

		$(old_cat_funcs).removeClassName($(old_cat_funcs).classNames().toArray().last());
		$(old_cat_funcs).addClassName(voting.options.catFuncsClasses.last());
		voting.options.votCat = votCatElem;
	}else if(voting.options.votCat == null)
	{
		ReloadPixelImages ();	// IVW
		dispatcher.sendCall(); // Omniture

		Effect.SlideDown(votCatElem, { duration: 0.5 });
		$(cat_funcs).removeClassName(voting.options.catFuncsClassesToOpen.last());
		cat_number = cat_number * 1;
		if(voting.options.votedCatArray.indexOf(cat_number) > -1)
		{
			$(cat_funcs).addClassName('catFuncsCloseVoted');
		}else
			$(cat_funcs).addClassName('catFuncsClose');

		voting.options.votCat = votCatElem;
	}else if(voting.options.votCat == votCatElem)
	{
		closeVotCats(voting.options.votCat);
		//$(cat_funcs).removeClassName('catFuncsClose');
		$(cat_funcs).removeClassName($(cat_funcs).classNames().toArray().last());
		$(cat_funcs).addClassName(voting.options.catFuncsClasses.last());
		voting.options.votCat = null;
	}

	//Exo_AdReload.update('BannerGroup'); // deprecated
	setTimeout("voting.options.openingInProgress = false", 500);
}

/**
* Closes a category.
*/
var closeVotCats = function(votCatElem)
{
	Effect.SlideUp(votCatElem, { duration: 0.5 });
	if(voting.options.currentPlayer != null)
		closePlayer('gmp_embed_player_' + voting.options.currentPlayer);
}

var vote = function(catId, gameId)
{
	if(voting.options.votedCatArray.indexOf(catId) > -1 || voting.prohibitVoting)
	{
		return;
	}

	voting.options.votedCatArray[voting.options.votedCatArray.size()] = catId
	var params = "/ajax_index.php?ajaxCmd=vote&catId=" + catId + "&gameId="+gameId;
	doAjaxRequest(params, prohibitVoting);
}

var prohibitVoting = function(response)
{
	split_array = response.responseText.split("=");
	var catId = split_array.last();
	voting.options.catFuncsClassesToOpen[voting.options.catFuncsClassesToOpen.size() - 1] = 'catFuncsVoted';
	for(var i = (catId * 5 - 4); i <= (catId * 5); i++)
	{
		var v_button_id = 'voting_button_' + catId + '_' + i;
		$(v_button_id).removeClassName('voting_button_on');
		$(v_button_id).addClassName('voting_button_off');
		var img_id = 'v_button_img_'+ catId + '_' + i;
		$(img_id).src = "images/cat/cat_corner_voted_inner.gif";
		$(img_id).onMouseover = "";
		$(img_id).onMouseout = "";

	}

	var catBoxId = 'catBox_' + catId;
	var catBoxBackground = "background: #000000 url(images/cat/cat_box" + catId +"_on.gif);";
	$(catBoxId).setStyle(catBoxBackground);
	var catElem = 'catVotes_' + catId;
	$(catElem).setStyle("background: #000000 url(images/cat/cat_extra_space_on.gif);");
	catElem = $(catElem);
	openVotCats(catElem);
	/*if(voting.votedCounter == 0)
		Effect.SlideUp('win_form_body', { duration: 0.1 });*/
	voting.votedCounter++;

	if(voting.votedCounter == voting.options.votingNumber)//win form anzeigen
		openWinPopup();
}

var votingButtonOver = function(imgElem)
{
	var split_array = imgElem.id.split("_");
	var cat_number = split_array[split_array.size() - 2];
	cat_number = cat_number * 1;
	if(voting.options.votedCatArray.indexOf(cat_number) > -1)
		return;
	imgElem.src = "images/cat/voting_button_on.gif";
}

var votingButtonOut = function(imgElem)
{
	var split_array = imgElem.id.split("_");
	var cat_number = split_array[split_array.size() - 2];
	cat_number = cat_number * 1;
	if(voting.options.votedCatArray.indexOf(cat_number) > -1)
		return;
	imgElem.src = "images/cat/voting_button_off.gif";
}

var play_in_new_window = function(movie_nr, window_name)
{
	var url = serverConfig.getURL();
	url += "/trailer/game_trailer_" + movie_nr + ".htm";

	fenster = window.open (
		"",
		window_name, // Name des neuen Fensters
		+"toolbar=0" // Toolbar
		+",location=0" // Adress-Leiste
		+",directories=0" // Zusatzleisten
		+",status=0" // Statusleiste
		+",menubar=0" // Menü
		+",scrollbars=0" // Scrollbars
		+",resizable=0" // Fenstergrösse veränderbar?
		+",width=500" // Fensterbreite in Pixeln
		+",height=400" // Fensterhöhe in Pixeln
	);

	fenster.moveTo(
		200, // X-Koordinate
		200 // Y-Koordinate
	); // der linken oberen Ecke

	fenster.location.href = url;
		//Dateiname und Pfad der Datei, die angezeigt werden soll
}

//not in use
var playMovie = function(element, gmp_id)//movieBoxElem)
{
	var split_array = element.id.split("_");
	var player_id = 'gmp_embed_player_' + split_array.last();
	if(voting.options.currentPlayer != null && voting.options.currentPlayer == split_array.last())
		return;
	else if(voting.options.currentPlayer != null)
		closePlayer('gmp_embed_player_' + voting.options.currentPlayer);

	voting.options.currentPlayer = split_array.last();
	var frame_id = "movie_frame_" + voting.options.currentPlayer;
	//$(frame_id).src = voting.options.flashSiteUrl + voting.options.currentPlayer + '.htm';
	load_video(gmp_id, frame_id, player_id);
	Effect.BlindDown(player_id, { duration: 0.5 });
	if(voting.options.currentPlayer == 5)
	{
		$(player_id).next().setStyle('background:#000000 url(images/cat/cat_vote_bg_end_mov.gif) no-repeat')
	}

	voting.lastPlayerBackground = $(player_id).next().getStyle('background');
}

//not in use
var load_video = function(gmp_id, div_id, player_id)
{
    // needs the file: swfobject.js
    var so = new SWFObject ("http://media.mtvnservices.com/mgid:uma:video:mtv.de:" + gmp_id, player_id, "504", "363", "6", "#ffffff");

    so.addParam ("menu", "false");
    so.addParam ("wmode", "transparent");
    so.addParam ("allowfullscreen", "false");
    so.addParam ("allowscriptaccess", "always");
    so.addParam ("enablejavascript", "true");
    so.addParam ("swLiveConnect", "true");

    so.addVariable ("embedId", player_id);
    so.addVariable ("autoPlay", "true");

    so.write (div_id);
}

//not in use
var closePlayer = function(playerId)
{
	Effect.BlindUp(playerId, { duration: 0.5 });
	var frame_id = "movie_frame_" + voting.options.currentPlayer;
	$(frame_id).src = "trailer/game_blank.htm";
	if(voting.lastPlayerBackground != null && voting.options.currentPlayer == 5)
	{
		var player_id = 'gmp_embed_player_' + voting.options.currentPlayer;
		voting.lastPlayerBackground = $(player_id).next().setStyle(voting.lastPlayerBackground);
		voting.lastPlayerBackground = null;
	}
	voting.options.currentPlayer = null;
}

var closeWinPopup = function(pop_id)
{
	if(voting.raffle)
	{
		Effect.Fade(pop_id);
	}
	else
		$(pop_id).hide();
	$('overlay').hide();
}

var openVotePopup = function()
{
	if(!voting.openVotePopup || voting.dontShowPopup)
		return;

	$('overlay').show();
	$('win_form_win').show();
	$('headline').scrollTo();
	voting.openVotePopup = false;

}

var openWinPopup = function()
{
	$('overlay').show();
	$('win_form').show();
	Effect.SlideDown('win_form_body', { duration: 1.5 });
	//Effekt.apear('win_form');
	var form = $('form_win');
	var first = form.focusFirstElement();
	//first.scrollTo();
	$('headline').scrollTo();
}

var checkWinForm = function()
{
	if(voting.raffle)
		return;
	var form_fields_output = new Array('Name', 'Vorname', 'Anschrift',
		'Postleitzahl', 'Stadt', 'Telefon', 'Geburtsdatum', 'Email');//nicht alle werden benötigt!!
	var form = $('form_win');
	var form_inputs = form.getInputs();
	var error = "";
	var agb_error = "";
	var mail_error = "";
	var params = "/ajax_index.php?ajaxCmd=win";
	for(var i = 0; i < form_inputs.size(); i++)
	{
		//alert(form_inputs[i].name + " " + form_inputs[i].value);
		if(form_inputs[i].name == "agb")
		{
				if(!form_inputs[i].checked)
				{
					agb_error	= "<br/>AGBs m&uuml;ssen akzeptiert werden.";
				}


		}else if(form_inputs[i].name != 'country' && form_inputs[i].name != 'tel' && form_inputs[i].name != 'birth')
		{
			if(form_inputs[i].value == "")
			{
				error += ", " + form_fields_output[i];
			}else
			{
				if(form_inputs[i].name == 'mail')
				{
					var check_mail = checkEmail(form_inputs[i].value);
					if(!check_mail)
						mail_error = '<br/>Die E-Mail-Adresse ist falsch!';

				}

				params += "&" + form_inputs[i].name + "=" + form_inputs[i].value;
			}

		}else
		{
			if(form_inputs[i].value != "")
				params += "&" + form_inputs[i].name + "=" + form_inputs[i].value +
					 "&" + $('country').name + "=" + $('country').value;
		}
	}

	//alert("error " + error + " params " + params);

	if(error != "" || agb_error != "" || mail_error != "")
	{
		if(error != "")
		{
			error = "Folgende Felder m&uuml;ssen noch ausgef&uuml;hlt werden:<br/>" + error.substring(1, error.length);
			$('message').update(error);
		}
		if(error != "" && (agb_error != "" || mail_error != ""))
		{
			var inner = $('message').innerHTML;
			agb_error = inner + agb_error + mail_error;
			$('message').update(agb_error);
		}else if(agb_error != "" || mail_error != "")
		{
			$('message').update(agb_error + mail_error);
		}

		$('message').setStyle('color: red;');
	}else
	{
		doAjaxRequest(params, openThanxPopup);
	}
}

var checkEmail = function(mail)
{

	var check = mail.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);

	if(check)
	{
	   return true;
	}

	return false;
}


var openThanxPopup = function(response)
{
	//alert(response.responseText);
	if(response.responseText == "!*!success!*!")
	{
		//$('win_form_messages').insert({bottom: });
		$('message').setStyle('color:white;');
		voting.raffle = true;
		closeWinPopup('win_form');
		$('message_win').update("Danke f&uuml;r deine Teilnahme. Wir w&uuml;nschen dir viel Gl&uuml;ck!");
		voting.openVotePopup = true;
		openVotePopup();
	}else
	{	alert(response.responseText);
		$('message').setStyle('color:red;');
		$('message').update("Es ist ein &Uuml;bertragungsfehler aufgetreten. " +
			"&Uuml;berpr&uuml;fe deine Angaben oder probiere es noch mal zu einem sp&auml;teren Zeitpunkt.");
		return;
	}



}