﻿
/** GLOBAL VARIABLES **/

var picWidth = 1920;
var picHeight = 1112;

var newWidth = 0;
var newHeight = 0;

var currentIndex = 0;
var oldIndex = 0;
var numberOfCases = 5;
var isScrolling = true;
var displayAsSlide = 0;
var pageTitle;


$(document).ready(function() {
    
    // onload, check hash parameter
    checkHashAndDisplayCase();
});

$(window).resize(function() {
    resizeImageSCut();

});


function checkHashAndDisplayCase() {
    
    resizeImageSCut();
    
    if (window.location.hash) {
        
        // hash parameter found
        var hash = window.location.hash.substring(1); //Puts hash in variable, and removes the # character
        displayCase(hash);
    }
   
}

function displayCase(name) {
    
    // get case index by name
    var index = getCaseIndex(name);

    // set page title
    if (pageTitle == null)
    {
        pageTitle = $(document).attr("title");
        $(document).attr("title", pageTitle + " " + name);
    } else {

       $(document).attr("title", pageTitle + " " + name);
    }

    if (displayAsSlide > 0) {
        slideTo(index); 
    }else {
        displayWithNoSlide(index);
    }

    setupNavigationItems(index, name);
}

function setupNavigationItems(index, name) {
  
    $(".menuLink").removeClass("active");
    $(".index_" + index).addClass("active");

    var prevIndex = parseInt(index) - 1;
    var nextIndex = parseInt(index) + 1;

    if (index > 0) {
        $('#casePrevLink').removeClass("transparent");    
        
        var prevCaseName = getCaseName(prevIndex);
        var prevCaseUrl = getCaseUrl(prevCaseName);

        $('#casePrevLink').attr('href', "http://" + prevCaseUrl);
        $('#casePrevLink').attr('title', prevCaseName);

    } else {
        
        // no navigation hide transparen layer to navigation arrow
        $('#casePrevLink').addClass("transparent");
        $('#casePrevLink').removeAttr('title');
    }

    if (index < (numberOfCases - 1))
    {
        $('#caseNextLink').removeClass("transparent");
        
        var nextCaseName = getCaseName(nextIndex);
        var nextCaseUrl = getCaseUrl(nextCaseName);

        $('#caseNextLink').attr('href', "http://" + nextCaseUrl);
        $('#caseNextLink').attr('title', nextCaseName);
    } 
    else {
        
        // no navigation hide transparen layer to navigation arrow
        $('#caseNextLink').addClass("transparent");
        $('#caseNextLink').removeAttr('title');
    }
}

//help method to get index (from a hidden variabel) for a case
function getCaseIndex(name) {
    var caseIndex = -1;

    var splitIndexArr = $('#caseIndex').val().split(";");
    
    for(i = 0; i < splitIndexArr.length; i++){
        
	    var caseItem = splitIndexArr[i].split(":"); 
        
        if (caseItem[0] == name)
        {
            caseIndex = caseItem[1];
            break;
        }
    }

    return caseIndex;
}

function getCaseName(index) {
    var caseName = -1;

    var splitIndexArr = $('#caseIndex').val().split(";");

    for (i = 0; i < splitIndexArr.length; i++) {

        var caseItem = splitIndexArr[i].split(":");

        if (caseItem[1] == index) {
            caseName = caseItem[0];
            break;
        }
    }

    return caseName;
}

function getCaseUrl(name) {
    var caseUrls = '';

    var splitUrlArr = $('#caseUrls').val().split(";");

    for (i = 0; i < splitUrlArr.length; i++) {

        var caseItem = splitUrlArr[i].split(":");

        if (caseItem[0] == name) {
            caseUrls = caseItem[1];
            break;
        }
    }

    return caseUrls;
}



// displays a case with no slide
function displayWithNoSlide(index) {
    
    oldIndex = index;
    isScrolling = true;
    $("#cases").hide();
    $("#cases").fadeIn('slow');
    $("#cases").css({ "left": "-" + newWidth * (index) });
    
    displayAsSlide = 1; // set and next time it will slide

}

// slide to case with index parameter
function slideTo(index) {

    var newIndex = oldIndex - index;
    
    oldIndex = index;
    isScrolling = true;

    $("#cases").animate({ "left": "+=" + newWidth * (newIndex) }, {
            duration: 1000,
            complete: function() {
                isScrolling = false;
            }
        });
}

function resizeImageSCut() {
  
    var navWidth = $(window).width();
    var navHeight = $(window).height();
    var navRatio = navWidth / navHeight;
    var newMarginLeft;
    var infobottom = 154;
    if ($('#main_image').width() > 1) {
        picWidth = $('#main_image').width();
    }
    if ($('#main_image').height() > 1) {
        picHeight = $('#main_image').height();
    }
    picRatio = picWidth / picHeight;

    if (navRatio > picRatio) {
        newHeight = (navWidth / picWidth) * picHeight;
        newWidth = navWidth;
        newLeft = 0 - ((newWidth - navWidth) / 2);
        newMarginLeft = 0;
        infobottom = infobottom + ((newHeight - navHeight) / 2);
    }
    else {
        newHeight = navHeight;
        newWidth = (navHeight / picHeight) * picWidth;
        newLeft = 0;
        newMarginLeft = 0 - (newWidth - navWidth) / 2;
    }

    newTop = 0 - ((newHeight - navHeight) / 2);

    $('#cases').css({ visibility: "visible", display: "block" });
    $('.info').css({ bottom: infobottom });
    $('.case').css({ height: newHeight, width: newWidth });
    $('.caseImage').css({ height: newHeight, width: newWidth, marginLeft: newMarginLeft });
    $('#cases').css({ height: newHeight, width: Math.ceil(newWidth) * numberOfCases });
    $('#cases').css({ top: newTop, left: 0 - (newLeft + (newWidth * (oldIndex))) });

    // only do this on startpage
    if (window.location.pathname == '/') {
        $('.container').css({ height: newHeight, width: Math.ceil(newWidth) * numberOfCases });
        $('.container').css({ top: newTop, left: 0 - (newLeft + (newWidth * (oldIndex))) });
        $('.bgImageStartPage').css({ height: newHeight, width: newWidth, marginLeft: newMarginLeft });
        $('.bgImageStartPage').show();
    }
    
    
}


$(document).ready(function() {
    isScrolling = false;
     numberOfCases = $('.casesnav').children('.menuItem').size();

    $("#Tjanster--Produkter").siblings(".offers_inactive").removeClass("offers_inactive").addClass("offers_active");
    $("#Design").siblings(".design_inactive").removeClass("design_inactive").addClass("design_active");
    $("#Teknik").siblings(".tech_inactive").removeClass("tech_inactive").addClass("tech_active");
    $("#Support").siblings(".support_inactive").removeClass("support_inactive").addClass("support_active");
    $("#Strategi").siblings(".strategy_inactive").removeClass("strategy_inactive").addClass("strategy_active");
    $("#Projektledning").siblings(".projectManagement_inactive").removeClass("projectManagement_inactive").addClass("projectManagement_active");
});
