cards = {}
cards.Cards = function(){
    /*
     * Pokazanie formularza
     */
    this.evCards1Click = function(){
        document.getElementById("CardsPreviewImage").src = this.lastClickMiniature
        document.getElementById("CardsFormTable").style.display = "block"
    }
    /*
     * Wysyłka
     */
    this.evCards2Click = function(){
        if ($("#card_destination_email").val() == "") {
            alert(cards.lang.destination_email_novalid)
            $("#card_destination_email").focus()
            return
        }
        var params = {}
        params.type = "POST";
		 params.url = "/modules/Cards/sender.php";
        params.success = function(data, textStatus){
			//console.log(data, textStatus)
			if(data=="send"){
				cards.instance.setResultText(cards.lang.sended)
			}else{
				cards.instance.setResultText(cards.lang.sending_error)
			}
        };
		params.error=function(XMLHttpRequest, textStatus, errorThrown){
			//console.log(XMLHttpRequest, textStatus, errorThrown)
			cards.instance.setResultText(cards.lang.sending_error)
		}
        params.data = {}
		params.data["do"]="send"
		var lastBig=this.lastClickMiniature.replace(/_m\./g, '.')
		var lastBigA=lastBig.split("/")
		lastBig=lastBigA.pop()
		params.data.imageName=lastBig
		params.data.card_destination_email=$("#card_destination_email").val()
		params.data.card_correspondence=$("#card_correspondence").val()
		params.data.card_sender_name=$("#card_sender_name").val()
        $.ajax(params)
		$("#CardsComunicateText").html("<img src='/modules/layout/processing-100x100.gif' alt=''/>")
    }
	this.setResultText=function(t){
		$("#CardsComunicateText").html(t)
		$("#CardsComunicateText").fadeOut(3000,function(){
			$(this).html("")
			$(this).fadeIn(10);
		})
	}
    /*
     * Rezygnacja
     */
    this.evCardsCancelClick = function(){
        document.getElementById("CardsFormTable").style.display = "none"
    }
    /*
     * Zmiana zdjęcia
     */
    this.evChangeBig = function(ref){
        this.lastClickMiniature = ref.src
        document.getElementById("CardsBigImage").src = this.lastClickMiniature.replace(/_m\./g, '.')
    }
    cards.instance = this
    var images = []
    var firstMiniature = "/modules/Cards/" + cards.config.imagesIds[0] + "_m.jpg"
    for (var i = 0; i < cards.config.imagesIds.length; i++) {
        images.push("<img src='/modules/Cards/" + cards.config.imagesIds[i] + "_m.jpg' onclick='cards.instance.evChangeBig(this)' class='CardsSmallImage'/>")
    }
    var template = new cards.template()
    var fm = template.getForm()
    var s = template.getLayout(fm, images.join(" "))
    document.getElementById(cards.config.placeholderId).innerHTML = s
    this.evChangeBig({
        src: firstMiniature
    })
    // console.log(s)
}
onLoadQueue.appendOnLoad(cards.Cards)
