/**
 * Actonjs Library v. 1.0
 * Author: Actonbit.gr < sales@actonbit.gr >
 * Copyrights: Actonbit.gr < sales@actonbit.gr >
 */

Actonjs.namespace("Actonjs.slideshow");

Actonjs.slideshow = function () {
	
	var server_name;
	var images, properties, imageSrcs, imageLegends, imageDivs, buttonDivs;
	var slide, interval, current;
	var loadedImages;
	var stime;

	function reset() {
		server_name  = window.location.protocol + '//' + window.location.hostname;
		images = Array();
		properties = Array();
		imageSrcs = Array();
		imageLegends = Array();
		imageDivs = Array();
		imageLegendDivs = Array();
		buttonDivs = Array();
		loadedImages = 0;
		slide = false;
		interval = 0;
		current = -1;
	}
	
	function retrieveData() {
		// We mock the procedure of retrieving the data here, normally we 
		// would be querying a controller to retrieve this data.
		var html = false;
		slide = true;
		interval = 5;
		
		var con = Actonjs.AJAX();
		if (con.init()) {
			con.request({
				url:'/slideshow/init/',
				params:'BS=1',
				method:'POST',
				success:function() {
					var response=con.getResponse();
					var success=response.s;
					var p_count = response.pc;
					if (p_count>0) {
						for (var i=0; i<p_count; i++) {
							images.push(new Image());
						}
						var p = response.p;
						for (var i=0; i<p.length; i++) {
							imageSrcs[i] = p[i].path;
							imageLegends[i] = p[i].le;
							buttonDivs[i] = $('slide_btn_0'+(i+1));
							var html = '<div class="slide_image_btn_inner" style="cursor:pointer; position:absolute; width:194px;" onclick="javascript:window.location=\''+server_name+'/property/show/id:'+p[i].pid+'\';">';
							html += '<div class="clear">'+p[i].la+'</div>';
							html += '<div class="slide_image_btn_desc">'+p[i].desc+'</div>';
							html += '<div class="slide_image_btn_price" style="position:absolute; top:55px;">'+p[i].price+'</div>';
							html += '</div>';
							buttonDivs[i].innerHTML = html;
						}
						kickStart();
					}
					else {
						console.log('kklklk');
						/*De vrethikan katholou akinita, opote akuronoume tin vitrina entelos.*/
						$('home_slide_loader').style.display = 'none';
						$('home_slide_table').style.display = 'none';
					}
				},
				failure:function(){
					$('home_slide_loader').style.display = 'none';
					$('home_slide_table').style.display = 'block';
					alert('Παρουσιάστηκε κάποιο σφάλμα κατά τη μετατροπή της καταχώρησης, παρακαλώ δοκιμάστε αργότερα.');
				}
			});
		}
		
		/*
		imageSrcs[0] = 'http://remaxdelta.actonbit.gr/photos/l/house_photo_01.jpg';
		imageLegends[0] = 'Αυτή είναι η φωτογραφία νούμερο 1!';
		buttonDivs[0] = Actonjs.get('slide_btn_01');
		html  = '<div class="slide_image_btn_inner">';
		html += '<div class="clear">Κάτω Ηλιούπολη</div>';
		html += '<div class="slide_image_btn_desc">3 κρ, 1 μπ, 108 τ.μ.</div>';
		html += '<div class="slide_image_btn_price">&euro; 210.000</div>';
		html += '</div>';
		buttonDivs[0].innerHTML = html;
		
		imageSrcs[1] = 'http://remaxdelta.actonbit.gr/photos/l/house_photo_02.jpg';
		imageLegends[1] = 'Αυτή είναι η φωτογραφία νούμερο 2!';
		buttonDivs[1] = Actonjs.get('slide_btn_02');
		html  = '<div class="slide_image_btn_inner">';
		html += '<div class="clear">Αμπελόκηποι</div>';
		html += '<div class="slide_image_btn_desc">3 κρ, 1 μπ, 100 τ.μ.</div>';
		html += '<div class="slide_image_btn_price">&euro; 185.000</div>';
		html += '</div>';
		buttonDivs[1].innerHTML = html;
		
		imageSrcs[2] = 'http://remaxdelta.actonbit.gr/photos/l/house_photo_03.jpg';
		imageLegends[2] = 'Αυτή είναι η φωτογραφία νούμερο 3!';
		buttonDivs[2] = Actonjs.get('slide_btn_03');
		html  = '<div class="slide_image_btn_inner">';
		html += '<div class="clear">Νέα Πολιτεία</div>';
		html += '<div class="slide_image_btn_desc">3 κρ, 1 1/2 μπ, 121 τ.μ.</div>';
		html += '<div class="slide_image_btn_price">&euro; 275.000</div>';
		html += '</div>';
		buttonDivs[2].innerHTML = html;
		
		imageSrcs[3] = 'http://remaxdelta.actonbit.gr/photos/l/house_photo_04.jpg';
		imageLegends[3] = 'Αυτή είναι η φωτογραφία νούμερο 4!';
		buttonDivs[3] = Actonjs.get('slide_btn_04');
		html  = '<div class="slide_image_btn_inner">';
		html += '<div class="clear">Νεάπολη</div>';
		html += '<div class="slide_image_btn_desc">2 κρ, 1 μπ, 76 τ.μ.</div>';
		html += '<div class="slide_image_btn_price">&euro; 130.000</div>';
		html += '</div>';
		buttonDivs[3].innerHTML = html;
		*/
		/*
		imageSrcs[4] = 'http://remaxdelta.actonbit.gr/photos/l/house_photo_05.jpg';
		imageLegends[4] = 'Αυτή είναι η φωτογραφία νούμερο 5!';
		buttonDivs[4] = Actonjs.get('slide_btn_05');
		html  = '<div class="slide_image_btn_inner">';
		html += '<div class="clear">Εύοσμος</div>';
		html += '<div class="slide_image_btn_desc">3 κρ, 1 μπ, 80 τ.μ.</div>';
		html += '<div class="slide_image_btn_price">&euro; 155.000</div>';
		html += '</div>';
		buttonDivs[4].innerHTML = html;
		*/
		// END of mocking data retrieval
		
	}
	
	function kickStart() {
		var image_wrapper = Actonjs.get('slide_main_image_wrapper');
		var image_legend = Actonjs.get('slide_main_image_legend');
		var div = false;
		for (var i=0; i<images.length; i++) {
			div = document.createElement("div");
			div.id = 'slide_image_0'+(i+1);
			div.style.display = 'none';
			div.innerHTML = '<img src="'+imageSrcs[i]+'"/>';
			image_wrapper.appendChild(div);
			imageDivs[i] = div;
		}
		
		for(var i=0; i<images.length; i++) {
			div = document.createElement("div");
			div.id = 'slide_legend_0'+(i+1);
			div.style.display = 'none';
			div.innerHTML = imageLegends[i];
			image_legend.appendChild(div);
			imageLegendDivs[i] = div;
		}
		
		for (var i=0; i<images.length; i++) {
			images[i].onload = function () {
				loadedImages++;
				if (loadedImages == images.length) {
					new Effect.Fade('home_slide_loader',{duration:1, fps:25, queue: {scope: 'slideshowPrep', position: 'end' }});
					new Effect.Appear('home_slide_table',{afterFinish:function(){slideshow(0)}, duration:1, fps:25, queue: {scope: 'slideshowPrep', position: 'end' }});
				}
			}
			images[i].src = imageSrcs[i];
		}
	}
	
	// Kaleitai san callback sto afterFinish tou Appear tou pinaka sti fortosi.
	function slideshow(index) {
		if (stime) { clearTimeout(stime); }
		if (slide) { slide_move(); }
		else { setCurrent(index); }
	}
	
	function slide_move() {
		var next = getNextImage(current);
		var milliseconds = interval*1000;
		applyMask();
		if (current!=-1){
			new Effect.Fade(imageDivs[current].id, {duration:0.3, queue:{scope:'slide_fades',position:'end'}});
			buttonDivs[current].parentNode.style.background='transparent';
			imageLegendDivs[current].style.display = 'none';
		}
		new Effect.Appear(imageDivs[next].id, {duration:0.3, queue: {scope:'slide_fades', position:'end'}, afterFinish:function(){Actonjs.slideshow.removeMask();}});
		buttonDivs[next].parentNode.style.background='#FFFFFF';
		imageLegendDivs[next].style.display = 'block';
		
		stime = setTimeout(function(){slide_move();},milliseconds);
		current = next;
	}
	
	function setCurrent(index) {
		if (index > -2 && index <= (images.length-1) ) {
			if (current!=index) {
				if (current!=-1){
					imageDivs[current].style.display = 'none';
					buttonDivs[current].parentNode.style.background = 'transparent';
					imageLegendDivs[current].style.display = 'none';
				}
				imageDivs[index].style.display='block';
				buttonDivs[index].parentNode.style.background='#FFFFFF';
				imageLegendDivs[index].style.display = 'block';
				if (slide) {
					if (stime) { clearTimeout(stime); }
					var milliseconds = interval*1000;
					stime = setTimeout(function(){slide_move();},milliseconds);
				}
			}
			current = index;
		}
	}
	
	function getNextImage(cur) {
		var next = 0;
		if (cur === false || cur == -1 || cur == (images.length-1)) {
			next = 0;
		}
		else if (cur<(images.length-1)) {
			next = current+1;
		}
		
		return next;
	}
	
	function removeMask() {
		var el = Actonjs.get('home_slide_protective_mask');
		if (el) {
			el.style.display = 'none';
		}
	}
	
	function applyMask() {
		var el = Actonjs.get('home_slide_protective_mask');
		if (el) {
			el.style.display = 'block';
		}
	}
	
	return {
		init:	function() {
			reset();
			retrieveData();
		},
		setCurrent:	function(index){setCurrent(index);},
		removeMask:	function(){removeMask();},
		applyMask:	function(){applyMask();}
	}
}();

Actonjs.onReady(Actonjs.slideshow.init);
