$(document).ready(function() {
	//createFileUploader();
	attachFancybox();
	runSlideshow(2);	
});

function runSlideshow(img_id) {
	
	$('<img />')
    .attr({src: '/images/slideshow/img'+img_id+'.jpg',
		   id: 'slideshow-img-'+img_id})
    .load(function(){
    	
    	$("#slideshow img").addClass("old-img");
    	
        $('#slideshow').append( $(this) );
        $("#slideshow-img-"+img_id).hide();
        
       	$("#slideshow img.old-img").fadeOut(2500, function() {
       		$("#slideshow img.old-img").remove();
       	});
        $("#slideshow-img-"+img_id).fadeIn(2500);
        
        var next_img = (img_id == 7) ? 1 : img_id+1;
		setTimeout(function(){runSlideshow(next_img)}, 4000);
    });
}

function createFileUploader() {
    var uploader = new qq.FileUploader({
        element: document.getElementById('add-img'),
        action: 'ajax/file_upload.php',
        multiple: false,
        onSubmit: function(id, fileName) {
			uploader.setParams({
		   		descr_txt: $("#foto-descr").val() 
			});	
        },
        onProgress: function(id, fileName, completed, total) {
            $("#photo-load-percentage").html(Math.round((completed/total)*100)+"%").show();
        },
        onComplete: function(id, fileName, responseJSON){
        		gallery_img_uploaded(id,fileName,responseJSON);
        		$("#photo-load-percentage").hide("slow", function() {
        			$("#photo-load-percentage").html('');
        		});
        		$("#foto-descr").val("");
		},
        allowedExtensions: ['jpg', 'jpeg', 'gif', 'png']
    });
}

function attachFancybox() {
	$("#left-panel a.img-a").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	false
	});
}

function sendContactMsg() {
	$("#contact-status").html("odosiela sa ...").show();
	$("#contact-send-btn").hide();
	
	var email = jQuery.trim($("#mail-addr").val());
	var txt = jQuery.trim($("#mail-text").val());
	
	$.post('/x', {a: "sendContactMsg", e:email, t:txt}, function(data){        
        if(data.code == 1) {
        	if(data.status == 1) {
    			$("#contact-status").addClass("ok").html("správa bola úspešne odoslaná").show();    		
        	}
        	else {
        		$("#contact-status").addClass("error").html("chyba pri odosielaní správy! použite prosím klasický e-mail alebo skúste neskôr").show();
        	}
        }
        else {
    	   	$("#contact-status").html("").hide();
			$("#contact-send-btn").show();
 			alert(data.msg);
        }
    }, "json");
}

function subscribe() {
	var mailaddr = jQuery.trim( $("#newsletter-mail").val() );
	
	if(mailaddr == '') {
		alert("Zadajte vašu e-mailovú adresu!");
		return;
	}
	
	$("#newsletter-status span").html("odosiela sa ...").show();
	$("#newsletter-btn").hide();
	
	$.post('/x', {a: "subscribe", e:mailaddr}, function(data){        
        if( (data.code == 1) || (data.code == 2) ) {
        	if(data.code == 1) {
    			$("#newsletter-status span").html("adresa bola pridaná");    		
        	}
        	else {
        		$("#newsletter-status span").html("adresa bola zmazaná");
        	}
        	
        	$("#newsletter-mail").val('');
        }
        else {
        	$("#newsletter-status span").html("");
 			alert(data.msg);
        }
        
        $("#newsletter-btn").show();
    }, "json");
}
