var initList = initList || []; 

initList.push(function(){
	$('.previous-button').html('<');
	$('.next-button').html('>');

	//Popups tic tac toe
	var contentPopups= $('.questions').html();
	//console.log(contentPopups);

	//Tictactoe
	var tictactoe = '<div data-sg-id="btn-popup" data-sg-key="1""></div>'+
					'<div data-sg-id="btn-popup" data-sg-key="2"></div>'+
                    '<div data-sg-id="btn-popup" data-sg-key="3"></div>'+
                    '<div data-sg-id="btn-popup" data-sg-key="4"></div>'+
                    '<div data-sg-id="btn-popup" data-sg-key="5"></div>'+
                    '<div data-sg-id="btn-popup" data-sg-key="6"></div>'+
                    '<div data-sg-id="btn-popup" data-sg-key="7"></div>'+
                    '<div data-sg-id="btn-popup" data-sg-key="8"></div>'+
                    '<div data-sg-id="btn-popup" data-sg-key="9"></div>';

	sg.restartTictactoe = function() {
		requirejs(['jquery', 'sgPopUp', 'sgGameTictactoe'],  function($,sgPopUp,sgGameTictactoe) {
			$('.board-turn').remove();
			$('.row-squares').remove();
			$('.questions').empty();
			$('.questions').html(contentPopups);

			//Popups opened previously
			$('body > [data-sg-id="item-popup"]').each(function(index, value){
				if(!$(this).hasClass('popup_end')){
					$(this).remove();
				}
			})
				


			$('div[data-sg-id="game-tictactoe"]').empty().html(tictactoe);
			var btnPopups = $('div[data-sg-id="game-tictactoe"]').find('div[data-sg-id=btn-popup]');
			sgPopUp.restart(btnPopups);

			var gameVar = $('div[data-sg-id="game-tictactoe"]');

			var index = gameVar.data('index');
			console.log(index);
			sgGameTictactoe.initGame(gameVar,index);
		});
	}
});



function winner(){
	var keys = [];
	$('.square.win').each(function(index,value){
		var key= $(this).attr('data-sg-key');
		keys.push(key);
	});

	console.log(keys);

	var deg = 0;
	var diagonal =false;
	var top = 0;
	var left = 0;

	//Put line
	if((keys.indexOf("1") != -1 && keys.indexOf("9") != -1) || (keys.indexOf("3") != -1 && keys.indexOf("7") != -1)){
		//Diagonal
		if(keys.indexOf("3") != -1){
			deg = 90;
			top = "-26";
			left = "-26";
		}else{
			deg=1;
			left = "-14";
		}
		diagonal= true;
	}else{
		//rect
		if((keys.indexOf("1") != -1 && keys.indexOf("3") != -1) || (keys.indexOf("4") != -1 && keys.indexOf("6") != -1) || (keys.indexOf("7") != -1 && keys.indexOf("9") != -1) ){
			deg= 90;
			left = "187";
			if (keys.indexOf("1") != -1 ){
				top = "-154";
			}else if(keys.indexOf("4") != -1 ){
				top = "-21";
			}else{
				top = "114";
			}
		}else{
			top = "-10";
			if(keys.indexOf("1") != -1 ){
				left = "59";
			}else if(keys.indexOf("2") != -1){
				left = "195";
			}else{
				left = "331";
			}
		}
	}

	var img = "<img src='./img/";

	if(diagonal){
		img += "SB_L_G10_U02_L0a_TRIQUI-linea-punteada.png'></img>";
	}else{
		img += "SB_L_G10_U02_L0a_TRIQUI-linea-punteadarecta.png'></img>";
	}

	var css = {
		"-ms-transform": "rotate("+deg+"deg)", /* IE 9 */
		"-webkit-transform": "rotate("+deg+"deg)", /* Chrome, Safari, Opera */
        "transform": "rotate("+deg+"deg)",
        "position": "absolute",
        "top": top+"px",
        "left": left+"px"
    	};

	var imgElem = $(img).css(css);
	$('div[data-sg-id="game-tictactoe"]').append(imgElem);

	$('div[data-sg-id="btn-popup"][data-sg-key="end"]').click();

}

