/**************************************************************************
 * eXeCute                                                                *
 * The cute way of eXe                                                    * 
 * Authors: Sandi Kelenc, Matej Kren, Tim Kos, Igor Pesek                 *
 * Licensed under LGPL                                                    *
 *                                                                        *
 * Copyright 2011                                                         *
 **************************************************************************/

	var correct =0;
	var wrong = 0;
	$(function() {
		$( ".draggable-exe" ).draggable({
			 start: function(event, ui) {
						$(this).hide();
						wrong=1;
						},
			revertDuration: 1000,
			revert: 'invalid',
			snap: ".droppable-exe" ,
			snapTolerance : 40,
			snapMode: 'inner',
			scroll: true,
			scrollSensitivity: 100,
			scrollSpeed: 10,
			distance: 30,
			helper: 'clone',
			stop: function(event, ui){
				 	$(this).show();
				 	$(this).effect('shake', { times:3 }, 200);
					var id = $(this).attr('id');
					var indexArray = id.split("|");
					var index = indexArray[1];
					var x = parseInt($("#stevec"+index).html());
					x = x +wrong - correct;
					$("#stevec"+index).html(x);	
					correct=0;
					wrong=0;
				 }
		});
		$( ".droppable-exe" ).droppable({
			tolerance: 'pointer',
			accept : function(ui){
						var id1 = $(this).attr('id');
						var id2 = ui.attr('id');
						var id1Array = id1.split("|");
						var id2Array = id2.split("|");
						if( id1Array[0]==id2Array[0]  && id1Array[1]==id2Array[1] && id1Array[2]==id2Array[2]){
							return true;
						}
						else{
							return false;
						}
			},
			drop: function( event, ui ) {
					correct=1;
					var html = $(ui.draggable).html();
					$( this )
						.html( html);
					$(ui.draggable).remove();
					$(this).css("border-color", "rgb(69,139,116)");
					$( this ).effect('pulsate', { times:3 }, 200); 
			}
		});
		$( ".draggable-exe1").draggable({
			revertDuration: 1000,
			revert: 'invalid',
			snap: ".droppable-exe1" ,
			snapTolerance : 40,
			snapMode: 'inner',
			scroll: true,
			scrollSensitivity: 100,
			scrollSpeed: 10,
			distance: 30,
			helper: 'clone',
			stop: function(event, ui){
				 	//$(this).effect('shake', { times:3 }, 200);
				 }
		});
		$( ".droppable-exe1" ).droppable({
			tolerance: 'pointer',
			accept : function(ui){
				var id1 = $(this).attr('id');
				var id2 = ui.attr('id');
				var id1Array = id1.split("|");
				var id2Array = id2.split("|");
				if(id1Array[1]==id2Array[1]){
					return true;
				}
				else{
					return false;
				}
			},
			drop: function( event, ui ) {
					var html = $(ui.draggable).html();
					$( this ).html( html);
					$(ui.draggable).hide("slow");
					var id1 = $(this).attr('id');
					var id2 = $(ui.draggable).attr('id');
					var id1Array = id1.split("|");
					var id2Array = id2.split("|");
					var a = parseInt(id1Array[2]);
					var b = id1Array[1];
					var c = id2Array[2];
					$(".revert"+b+":eq("+a+")").show("slow");
					$(".revert"+b+":eq("+a+")").attr('id',"i|"+b+"|"+c+"/i|"+b+"|"+a+"");
					$(this).css("border-color", "rgb(69,139,116)");
					$( this ).effect('pulsate', { times:3 }, 200); 
					var pravilnost = $("#pravilnost"+b).html();
					var praArray = pravilnost.split("|");
					praArray[a]=c;
					pravilnost = praArray.join("|");
					$("#pravilnost"+b).html(pravilnost);
					$(this).droppable('disable');
			}
		});
		
		$(".revert").click(function(){
			var id1 = $(this).attr('id');
			var split = id1.split("/");
			var x =split[0]+"|o";
			
			$(".draggable-exe1").each(function(){
				if($(this).attr('id')==x)
					$(this).show("slow");
			});
			var y = split[1]+"|v";
			$(".droppable-exe1").each(function(){
				if($(this).attr('id')==y){
					$(this).html("Drop here");
					$(this).css("border-color", "#fcefa1")
					$(this).droppable('enable')
				}
			});
			var aArray = split[1].split("|");
			var b = aArray[1];
			var c = parseInt(aArray[2]);
			$("#preverba_"+b).html("Preveri");
			$("#nepr_"+b).hide("quick");
			var pravilnost = $("#pravilnost"+b).html();
			var praArray = pravilnost.split("|");
			praArray[c]=-1;
			pravilnost = praArray.join("|");
			$("#pravilnost"+b).html(pravilnost);
			$(this).hide();
		});
		$(".nepravilni").click(function(){
			var x = $(this).attr('id');
			x = x.split("_");
			x = x[1];
			var pravilnost = $("#pravilnost"+x).html();
			var praArray = pravilnost.split("|");
			$(".droppable-exe1").each(function(){
				var y = $(this).attr('id');
				y = y.split("|");
				if(y[1]==x && praArray[y[2]]!=y[2]){
					$(this).effect('pulsate', { times:3 }, 200); 
					$(this).css("border-color", "red");
				}
			})
		});
		
		$( ":button, :submit" ).button( 
				{icons: { 
					primary: "ui-icon-gear",
					secondary: "ui-icon-triangle-1-s",		
						}
				});
		$( ":button, :submit" ).not(".ui-state-highlight, .button").addClass("exebutton");
		
		$(".scrollable").scrollable();

		$(".items img").click(function() {

				// see if same thumb is being clicked
			if ($(this).hasClass("active")) { return; }

				// calclulate large image's URL based on the thumbnail URL (flickr specific)
			var url = $(this).attr("alt").split("<>")[0];
			var caption = $(this).attr("alt").split("<>")[1];
			var x = $(this).attr("id");

				// get handle to element that wraps the image and make it semi-transparent
			var wrap = $("#image_wrap_"+x).fadeTo("medium", 0.5);

				// the large image from www.flickr.com
			var img = new Image();

				// call this function after it's loaded
			img.onload = function() {

					// make wrapper fully visible
			wrap.fadeTo("fast", 1);

					// change the image
			wrap.find("img").attr("src", url);
			wrap.find(".besediloObSliki").html(caption);
			
			};

				// begin loading the image from www.flickr.com
			img.src = url;

				// activate item
			$(".items img").removeClass("active");
			$(this).addClass("active");

			// when page loads simulate a "click" on the first image
			}).filter(".exeFirst").click();
});
	
	jQuery.fn.center = function () {
		this.css("position","absolute");
		//this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
		this.css("left", ( $("#main").width() - this.outerWidth() ) / 2+$(window).scrollLeft() + "px");
		// v urejanju se ne postavi tocno na sredino, pri exportu pa
		return this;
	}
	


/******************************
*  Document OnLoad event
*
*
********************************/	
$(document).ready(function() {
		
	$(".slidetabs").tabs(".imagesTab > div", {

		// enable "cross-fading" effect
		effect: 'fade',
		fadeOutSpeed: "200",
		fadeInspeed: "0",

		// start from the beginning after the last tab
		rotate: false

	// use the slideshow plugin. It accepts its own configuration
	}).slideshow();
	$(".imagesTab div.tab").unbind("click");
	/*	$(".imagesTab div#tab").bind("click", function(){
		var api = $(".slidetabs").data("tabs");
		var ind = api.getIndex();
		api.getPanes().hide();
		api.click(ind-1);
	});*/
	
	$('.slidetabs').center();
	$(window).bind('resize', function() {
		$('.slidetabs').center({transition:300});
	});
	
		var id1="";
		var id2="";
		var i=0;
		// show tinymce textarea for first empty textarea that is not hint or feedback
		$('textarea.neizbran').each(function () {
			id1=$(this).attr("id").replace("text","divn");
			if(i==0 && $("#"+id1).text()=="" && $(this).attr("name").substring(0,1)!="f" 
				&& $(this).attr("name").substring(0,4)!="hint"){
				//hide div
				var id1=$(this).attr("id").replace("text","div");
				$("#"+id1).hide();
				
				//show tinymce textarea			
				$(this).show();
				//$("#"+id1).removeClass("neizbran");
				$(this).addClass("izbran");
				tinyMCE.execCommand('mceAddControl', true, this.id);
				tinyMCE.execCommand('mceFocus', true, this.id);
				i++;
			}
		});
		// show tinymce textarea to first textarea if tinymce was not showed jet
		$('textarea.neizbran').each(function () {
			id1=$(this).attr("id").replace("text","divn");
			if(i==0){
				//hide div
				var id1=$(this).attr("id").replace("text","div");
				$("#"+id1).hide();
				
				//show tinymce textarea			
				$(this).show();
				//$("#"+id1).removeClass("neizbran");
				$(this).addClass("izbran");
				tinyMCE.execCommand('mceAddControl', true, this.id);
				tinyMCE.execCommand('mceFocus', true, this.id);
				i++;
			}
		});


		$('div.izbran').click(function () {
			$('textarea.izbran').each(function () {
				//show div
				id1=$(this).attr("id").replace("text","div");
				id2=$(this).attr("id").replace("text","divn");
				$("#"+id1).show();
				//hide tinymce textarea
				tinyMCE.execCommand('mceRemoveControl', false, this.id);		
				$(this).removeClass("izbran");
				$(this).hide();
				//copy content of textarea to div
				$("#"+id2).html($(this).val());		
			});
			//hide div
			$(this).hide();
			
			//show tinymce textarea
			id1=$(this).attr("id").replace("div","text");
			$("#"+id1).show();
			//$("#"+id1).removeClass("neizbran");
			$("#"+id1).addClass("izbran");
			tinyMCE.execCommand('mceAddControl', true, id1);
			tinyMCE.execCommand('mceFocus', true, id1);

		});
		
		
//exe - funkcija ki expose-a element, ki ga urejamo
		//ob kliku
		$('div.deviceBorder').click(function() {
			// perform exposing for the clicked element
			
			// izbere barvo od starša
		    var i=0;
		    var color="transparent";
		    $(this).parents().each(function(){
		    	if(i==0){
		    		color=$(this).css('background-color');
		    		if(color!="transparent")
		    			i++;
		    	}
		    });		    	

			var ta=$(this);
			$(this).expose({
				// when exposing is done, change form's background color
				onBeforeLoad: function() {
					ta.css({'background-color': color});
				},
				opacity: 0.5,
				loadSpeed: 'slow',
				// when "unexposed", return to original background color
				onClose: function() {
					ta.css({'background-color': 'transparent'});
					
				}
			});	
		});
		
		// na začetku
		$('div.deviceBorder').each(function() {
			// perform exposing for the each element		
			// izbere barvo od starša
		    var i=0;
		    var color="transparent";
		    $(this).parents().each(function(){
		    	if(i==0){
		    		color=$(this).css('background-color');
		    		if(color!="transparent")
		    			i++;
		    	}
		    });
			var ta=$(this);
			$(this).expose({
				// when exposing is done, change form's background color
				onBeforeLoad: function() {
					ta.css({'background-color': color});
					
				},
				opacity: 0.5,
				loadSpeed: 'slow',
				// when "unexposed", return to original background color
				onClose: function() {
					ta.css({'background-color': 'transparent'});			
				}
			});	
		});
		
		
// ob kliku na true ali false se gumba onemogočita
		$(".truebutton").click(function(){
			var id1="";
			var idDevice = "";
			if($(this).attr("id").substring(0,4)=="true"){
				id1=$(this).attr("id").replace("true","false");
				idDevice = $(this).attr("id").replace("true","");
				if(parseInt(document.getElementById("s0b"+idDevice).getAttribute("even_steven"))%2 == 0){
					$(this).css("background", "#00ff00");
					$(this).parents(".iDevice_inner").find(".slidetabs a.current").addClass("correct");
				}else{
					$(this).css("background", "#ff0000");
					$(this).parents(".iDevice_inner").find(".slidetabs a.current").addClass("incorrect");
				}
			}else{
				id1=$(this).attr("id").replace("false","true");
				idDevice = $(this).attr("id").replace("false","");
				if(parseInt(document.getElementById("s0b"+idDevice).getAttribute("even_steven"))%2 == 1){
					$(this).css("background", "#00ff00");
					$(this).parents(".iDevice_inner").find(".slidetabs a.current").addClass("correct");
				}else{
					$(this).css("background", "#ff0000");
					$(this).parents(".iDevice_inner").find(".slidetabs a.current").addClass("incorrect");
				}
			}
			
			//$(this).attr("value") = "toje string";//document.getElementById("s0b"+idDevice).even_steven;
			
			
			document.getElementById(this.id).disabled = true;
			$(this).addClass("ui-state-disabled");
			document.getElementById(id1).disabled = true;
			$("#"+id1).addClass("ui-state-disabled");
		});

		
		
		
		$("a[title]").tooltip({

			// place tooltip on the right edge
			position: "top right",

			// a little tweaking of the position
			offset: [0,0],

			// use the built-in fadeIn/fadeOut effect
			effect: "fade",
			fadeOutSpeed: 200,

			// custom opacity setting
			opacity: 0.75,
			
			predelay: 400,
			
			delay: 100,

			events: {def:     "mouseenter,mouseleave"}
		}).dynamic({ left: { direction: 'left', bounce: true } });
		
		
		//naključno razporedi možnosti odgovorov
		$('div.renderOptions').each(function() {
			var grp = $(this).children();
	        var cnt = grp.length;

	        var temp,x;
	        for (var i = 0; i < cnt; i++) {
	          temp = grp[i];
	          x = Math.floor(Math.random() * cnt);
	          grp[i] = grp[x];
	          grp[x] = temp;
	      }
	      $(grp).remove();
	      $(this).append($(grp));//
		});
		
		
		
		

});