/**
 *
 *  Client Side main javaScript file
 *
 *
 */

$(document).ready(function() {
    $(".bottom-arrow").click(function () {
        var elmHeight = parseInt($(".slide-container").outerHeight(), 10);
        var currentPos = parseInt($(".slide-container").position().top, 10);
        currentPos -= 376+3;
        if(-currentPos <= elmHeight - 376+3) {
            $(".slide-container").animate({top : currentPos}, {queue : true});
        }
        return false;
    });
    
    $(".top-arrow").click(function () {
        var elmHeight = parseInt($(".slide-container").outerHeight(), 10);
        var currentPos = parseInt($(".slide-container").position().top, 10);
        currentPos += 376+3;
        if(currentPos <= 58) {
            $(".slide-container").animate({top : currentPos}, {queue : true});
        }
        return false;
    });
    
    //change opacity on project thumbs
    $(".project-thumb").mouseenter(function() {
        $(this).find(".black-img").hide();
        $(this).find(".coloured-img, .project-img-title").show();
        //$(this).animate({opacity : 1}, 250);
    })
    
    $(".project-thumb").mouseleave(function() {
        $(this).find(".black-img").show();
        $(this).find(".coloured-img, .project-img-title").hide();
        //$(this).animate({opacity : 0.4}, 250);
    })
    
    //show image in overlayed element over the whole page
    $(".gallery-item").click(function() {
        var scrollOffset = parseInt($(document).scrollTop(), 10);
        scrollOffset = (!isNaN(scrollOffset)) ? scrollOffset : 150;
        
        var width = parseInt($(window).width(), 10);
        var height = parseInt($(window).height(), 10);
        var imageOptions = eval($(this).find(".img-options").text());
        var browserWidth = parseInt(document.documentElement.clientWidth, 10);
        
        var imgTag = '<img src="' + imageOptions.url + '" alt="Large Preview" style="margin-top: '+ scrollOffset +'px" />';
        $("body").remove("#large-preview-wrapper").append('<div id="large-preview-wrapper" style="background-position:center '+(scrollOffset+250)+'px">' + imgTag + '</div>');
        
        $("#large-preview-wrapper img").load(function() {
            $(this).css("display", "block");
            var divHeight = $(this).outerHeight();
            var divWidth = $(this).outerWidth();
            divHeight = divHeight > height ? divHeight : height - 60 ;
            divWidth = divWidth > width ? divWidth : width;
            $("#large-preview-wrapper").css({"height": divHeight + "px", "width" :"100%"});
        });
        
        $("#large-preview-wrapper").css({width:width, height:height}).click(function() {
            $(this).remove();
        })
        
        return false;
    });

    
    //add target="_blank" to project-url link for xhtml validation walkaround
    $(".project-url, .society-link, #resume-btn, #rss-feed-link").attr("target", "_blank");
    
    //add new comment
    $(".add-comment-btn").click(function() {
        var commentArticleID = $("#comment-article-id").val();
        var commentName = $("#comment-author").val();
        var commentBody = $("#comment-body").val();
        var ajaxURL = $(this).attr("href");
        if(commentArticleID && commentName && commentBody) {
            $.ajax({
                cache : false,
                type : "post",
                url : ajaxURL,
                dataType : "json",
                data : "comment_article_id="+commentArticleID+"&comment_description="+commentBody+"&comment_author="+commentName,
                success : function(data) {
                    if(data.success) {
                        $("#comment-author").val("");
                        $("#comment-body").val("");
                        $(".count-comments").text(parseInt($(".count-comments").text(), 10)+1);
                        var newCommentHTML = '<div class="comment-item">';
                        newCommentHTML += "<p>Posted by: "+data.data.author+" on "+data.data.date_posted+"</p>";
                        newCommentHTML += "<div>"+data.data.description+"</div>";
                        newCommentHTML += "</div>";
                        $(".comments-list-wrapper div:first").prepend(newCommentHTML);
                        alert(data.message);
                    }
                    else {
                        alert(data.message);
                    }
                }
            });
        }
        else {
            alert("Please enter valid data for article!");
        }
        return false;
    })
    
    var checkSiteTheme = setSiteTheme();
    if(siteTheme != checkSiteTheme) {
        /*
        var currentStyleURL = $("#cssLink").attr("href");
        var newStyleURL = currentStyleURL.replace(/dark|light/, checkSiteTheme);
        $("#cssLink").attr("href", newStyleURL);
        */
    }
    
    function setSiteTheme(){
        var siteTheme = "light";
        var timeObj = new Date();
        var currentHour = timeObj.getHours();
        if(currentHour > 18 || currentHour < 6) {
            siteTheme = "dark";
        }
        
        return siteTheme;
    }
    
})