﻿
$(document).ready(function() {
    jQuery.preLoadImages("./images/en_over.png", "./images/ro_over.png");
    $(".hover").mouseover(function() {
        $(this).attr("src", $(this).attr("src").replace("_off", "_over"));
    });
    $(".hover").mouseout(function() {
        $(this).attr("src", $(this).attr("src").replace("_over", "_off"));
    });
    $(".change").click(function() {
        setImage($(this).attr("alt"));
    });
});

function modelThumbHover() {
    $(".model img").mouseover(function() {
        $(this).attr("src", $(this).attr("src").replace("grayscale=true", "grayscale=false"));
    });
    $(".model img").mouseout(function() {
        $(this).attr("src", $(this).attr("src").replace("grayscale=false", "grayscale=true"));
    });
}

function resizeImage(image) {
    image.removeAttr("height");
    image.removeAttr("width");
    if ($(window).width() > $(window).height()) {
        var h = image.height() * $(window).width() / image.width();
        if (h < $(window).height()) {
            h = $(window).height();
            image.attr("height", h);
        } else {
            image.attr("width", $(window).width());
        }
    } else {
        var w = image.width() * $(window).height() / image.height();
        if (w < $(window).width()) {
            w = $(window).width();
            image.attr("width", w);
        } else {
            image.attr("height", $(window).height());
        }
    }
}

$(window).resize(function() {
    resizeImage($('#slideshow div.active img'));
    fixHeight();
});

$(window).load(function() {
    fixHeight();
});

var wHeight = 0;
var action = false;
function fixHeight() {
    if ($("#vertical").offset().top < 10) {
        $("#vertical").css("top", "10px");
        $("#vertical").css("margin-top", "0px");
        wHeight = 580;
        action = true;
    }
    else if (action && ($(window).height() > wHeight)) {
        $("#vertical").css("top", "50%");
        $("#vertical").css("margin-top", "-285px");
        action = false;
        wHeight = 0;
    }
}

function randomChild(obj) {
    var c = obj.children().length;
    var r = Math.ceil(Math.random() * c);
    return obj.children(':nth-child(' + r + ')');
};

function slideSwitch() {
    var $active = $('#slideshow div.active');
    if ($active.length == 0) $active = randomChild($('#slideshow'));
    var $next = $active.next().length ? $active.next() : $('#slideshow div:first');
    //var $sibs = $active.siblings();
    //var rndNum = Math.floor(Math.random() * $sibs.length);
    //var $next = $($sibs[rndNum]);
    $active.addClass('last-active');
    $next.css({ opacity: 0.0 })
        .addClass('active')
        .animate({ opacity: 1.0 }, 1000, function() {
            $active.removeClass('active last-active');
        });
    resizeImage($('#slideshow div.active img'));
}

var defaultImage;
function setImage(str) {
    defaultImage.attr("src", "./files/books/" + str);
}

function addRow(str) {
    $('#uploadedFiles > tbody > tr:first').append(str);
}
function hideUploader(str) {
    $(".upload").hide();
}

function UploadError(sender, args) {
    $('.alert').fadeIn('fast');
}

var sessionID = "";
var added = new Array();
function UploadComplete(sender, args) {
    var contentType = args.get_contentType();
    var fileName = args.get_fileName();
    if (contentType.toLowerCase() == "image/jpeg" | contentType.toLowerCase() == "image/jpg") {
        var add = true;
        for (var i = 0; i < added.length && add; i++) {
            if (added[i] == fileName)
                add = false;
        }
        if (add) {
            addRow("<td style='vertical-align: top;'><div class='uploaded-image'><img src='./handlers/contactthumbnail.ashx?contact=" + sessionID + "/" + fileName + "&grayscale=false&width=65&height=40' /></div></td>");
            added.push(fileName);
        }
        if (added.length == 6)
            hideUploader();
    }
    else {
        UploadError();
    }
}


function slide() {
    var $active = $('.slide div.active');
    if ($active.length == 0) $active = randomChild($('.slide'));
    var $next = $active.next().length ? $active.next() : $('.slide div:first');
    $active.addClass('last-active');
    $next.css({ opacity: 0.0 })
        .addClass('active')
        .animate({ opacity: 1.0 }, 1000, function () {
            $active.removeClass('active last-active');
        });
}