/**
 * Sample JavaScript public scripts
 * 
 * @author Piotr Zarzycki (hellix)
 * @version 0.1
 */

var hostName = 'http://'+window.location.hostname+'/';

$(document).ready(function(){
    $("#message-send").submit(function(){
        link = "/sendemail";
        var contact = "contact[name]="+$("#name-email").val()+"&contact[email]="
            +$("#message-email").val()+"&contact[message]="+$("#txtarea-email").val();
   
        $.ajax({
            url: link,
            cache: false,
            type: "POST",
            data: contact,
            success: function(data){
               $("#message-send").hide();
               $(".send-email").fadeTo("slow", 0.5);
               $(".send-email").show();
               $(".send-email").fadeTo("slow", 1);
            },
            beforeSend: function(XMLHttpRequest){
               $("#message-send").fadeTo("slow", 0.5);
            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
                alert(textStatus);
                alert(errorThrown);
            }
        });

        return false;
    });
})

function changeImage(objThis, file, alt){
    //Retrieve img tag
    var parentImgA = $(objThis).parent().parent().parent().parent();
    //Text from clicked object
    var valueA = $(objThis).text();
    //li tags - Each li tag contain tag a
    var childrenUl = $(objThis).parent().parent().children('li');
    
    if ($(childrenUl).length > 1)
    {
        //Set style attribute for current a tag
        $(objThis).attr("style", "text-decoration: underline;");
        //Remove style attribute from other a tags
        $(childrenUl).each(function(key, value){
            var tagA = $(value).find('a');
            if (valueA != $(tagA).text())
            {
                 $(tagA).removeAttr("style");
            }
        });
    }

    var pathImg = "./public/upload/projects/thumbnail/"+file;
    var img = parentImgA.find('img').eq(0);
    img.attr('src', pathImg);
    img.attr('alt', alt);
};

function pageChange(page)
{
    var link = "/pagechange,"+page.toString();
    $("#content .project").fadeTo("slow", 0.3);
    
    $.ajax({
        url: link,
        cache: false,
        type: "POST",
        data: null,
        success: function(data){
           var decodeJSON = $.JSON.decode(data);
           $("#content").html(decodeJSON);
           $("#content .project").fadeTo("fast", 1);
        },
        beforeSend: function(XMLHttpRequest){
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			alert(textStatus);
			alert(errorThrown);
		}
    });
}


function showCloud(contactButton)
{
    var attrObj = $(contactButton).attr('class');
    var cloudId = "cloud-"+attrObj.toString();
    
    $("#"+cloudId).addClass('active');
}

function hideCloud(contactButton)
{
    var attrObj = $(contactButton).attr('class');
    var cloudId = "cloud-"+attrObj.toString();

    $("#"+cloudId).removeClass('active');
}


