/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


$(document).ready(init);

var loaded = 0;
var projects;
var lastSlide;
var color;
function init(){
    $(".project").hide();
    if(!isiPhone()){
    $(".project").hover(showProjectHover,hideProjectHover);
    }
    projects = $(".project img");
    loadProject(loaded);
    //    $.preload(".project-image");
    if($(".project").length>0&&$.browser.msie&&$.browser.version == "7.0"){
        $(window).resize(resize);
        $(window).trigger('resize');
        $(body).width('1300');
    }
    $(".project, #head a, #body-detail-navigation div>a, .row-small-left a, .row-right-big a, project-image-description div").hover(addColor,removeColor);
    $("#body-navigation a, #body-detail-navigation p a, #body-clients a, #index #head a").hover(removeColor,addColor);
    $('#body img').lazyload({
        placeholder : "/img/transparent.png", //TODO: Exchange placeholder
        effect : "fadeIn",
        threshold : "10"
    });
    checkFooter();
}
function checkFooter(){
    topval = $("#footer").position().top;
    if(topval<$(document).height()){
        goTop = $(document).height()-35;
        console.log(goTop);
        if($("#list").hasClass('true')){
        $("#footer").css("top",goTop);
        $("#footer").css('bottom','auto');
        }
        $("#index #footer").css("margin-top",0);
    }
}

function addColor(){
    $(this).animate({
        color: "#3C3C3C" 
    },100);
    $(this).find('div').animate({
        color: "#3C3C3C" 
    },100);
}
function removeColor(){
    $(this).animate({
        color: "#a0a0a0" 
    },100);
    $(this).find('div').animate({
        color: "#a0a0a0" 
    },100);
}

function resize(){
    $width = $(window).width() - 196;
    //    if($("#body-content").width()>$width){
    $("#body-content").width($width);
        
    //    }
    //    if($("#body-projects").width()>$width){
    $("#body-projects").width($width);
//    }
}


function loadProject(el){
    if(el<projects.length){
        actual = projects[el];
        $(actual).closest('.project').fadeIn(200);
        loaded++;
        setTimeout('loadProject(loaded)',100);
        checkFooter();
    }
    
}

function showProjectHover(){
    $(this).find('.project-hover').fadeIn(200);
    $(this).find('.project-title').addClass('hover',200);
}

function hideProjectHover(){
    $(this).find('.project-hover').fadeOut(200);
    $(this).find('.project-title').removeClass('hover',200);
}

function showProject(project){
    project.fadeIn(200);
}
function showStartInfo(){
	$("#bg-overlay").height($(window).height());
        $("#content-description").show();
        $("#bg-overlay2").show();
        $("#slide-bg").cycle('pause');
}
function hideStartInfo(){
	$("#content-description").hide();
    $("#bg-overlay2").hide();
    $("#slide-bg").cycle('resume');
}
function startBgResize(){
	$("#content").hover(showStartInfo,hideStartInfo);
/*
    $("#content-info").toggle(function(){
        $("#bg-overlay").height($(window).height());
        $("#content-description").show();
        $("#bg-overlay").show();
    },function(){
        $("#content-description").hide();
        $("#bg-overlay").hide();
    });*/

    $("#slide-bg").cycle({
        fx : 'fade',
        speed : 2500,
        timeout: 5000,
        slideResize: 0
    });
    //    $("#slide-bg img").css('position','absolute');
    //    lastSlide = $("#slide-bg img").last();
    //    setTimeout('slideCycle()',5000);
    $("#slide-bg").click(nextSlide);
    $(window).resize(adaptBg);
    $(window).trigger('resize');
    $(window).keyup(slideKeys);
}

//function slideCycle(){
//    nextSlide = lastSlide.prev();
//    lastSlide.animate({
//        opacity: 0
//    },1000,function(){
//        lastSlide.hide();
//        lastSlide.css('opacity',1);
//        lastSlide = nextSlide;
//        setTimeout('slideCycle()',5000);
//    })
//}
function slideKeys(evt){
    arrow = {
        left: 37, 
        right: 39
    };
    keycode = evt.keyCode;
    switch(keycode){
        case arrow.left:
            prevSlide();
            break;
        case arrow.right:
            nextSlide();
            break;
    }
    
}
function nextSlide(){
    $("#slide-bg").cycle('next');
}
function prevSlide(){
    $("#slide-bg").cycle('prev');
}
function adaptBg(){
    wwidth  = $(window).width();
    wheight = $(window).height();
    
    
    // Resize the img object to the proper ratio of the window.
    var iw = $("#slide-bg").children('img').width();
    var ih = $("#slide-bg").children('img').height();
        
    if ($(window).width() > $(window).height()) {
        if (iw > ih) {
            var fRatio = iw/ih;
            $("#slide-bg img").css("width",$(window).width() + "px");
            $("#slide-bg img").css("height",Math.round($(window).width() * (1/fRatio)));

            var newIh = Math.round($(window).width() * (1/fRatio));

            if(newIh < $(window).height()) {
                var fRatio = ih/iw;
                $("#slide-bg img").css("height",$(window).height());
                $("#slide-bg img").css("width",Math.round($(window).height() * (1/fRatio)));
            }
        } else {
            var fRatio = ih/iw;
            $("#slide-bg img").css("height",$(window).height());
            $("#slide-bg img").css("width",Math.round($(window).height() * (1/fRatio)));
        }
    } else {
        var fRatio = ih/iw;
        $("#slide-bg img").css("height",$(window).height());
        $("#slide-bg img").css("width",Math.round($(window).height() * (1/fRatio)));
    }
		
// Center the image
//		if (typeof(jqez.center) == 'undefined' || jqez.center) {
//			if ($("#slide-bg").children('img').width() > $(window).width()) {
//				var this_left = ($("#slide-bg").children('img').width() - $(window).width()) / 2;
//				$("#slide-bg img").css({
//					"top"  : 0,
//					"left" : -this_left
//				});
//			}
//			if ($("#slide-bg").children('img').height() > $(window).height()) {
//				var this_height = ($("#slide-bg").children('img').height() - $(window).height()) / 2;
//				$("#slide-bg img").css({
//					"left" : 0,
//					"top" : -this_height
//				});
//			}
//		}
    
}

function back(){
    window.history.back(1);
    Event.preventDefault();
    return false;
}

function isiPhone(){
    return (
        //Detect iPhone
        (navigator.platform.indexOf("iPhone") != -1) ||
        //Detect iPod
        (navigator.platform.indexOf("iPod") != -1)
    );
}
