﻿
var PauseDuration = 20000;

var SingleItemPauseDuration = 3000;

var ItemAnimationDuration = 1000;

var ItemsPerSet = 5

var NextSet = 0;

var LayoutItems = new Array();

var NewsHeader = '#news-header';

function InitHeader(ALayoutType) {

    if (LayoutTypeIsValid(ALayoutType)) {

        SetItems(ALayoutType);

        for (var ii = 0; ii < ItemsPerSet; ii++) {
            
            if (ii < jQuery(LayoutItems).size()) {

                d = new Date();
                jQuery(NewsHeader).append('<div><img src="' + LayoutItems[ii] + '?' + d.getTime() + '" alt="' + ALayoutType + '" /></div>');
            }
        }

        var timeout = window.setTimeout(SwitchItems, 5000);
    }
}

function SwitchItems() {

    var ItemsToSwitch = new Array();
    var SetItems = new Array();

    jQuery(NewsHeader + ' div img').each(function () {

        var NewItemIndex = -1;

        while (jQuery.inArray(NewItemIndex, SetItems) > -1 || NewItemIndex < 0) {

            NewItemIndex = Math.floor(Math.random() * ItemsPerSet);
        }

        var ItemCount = jQuery(ItemsToSwitch).size();

        ItemsToSwitch[ItemCount] = new Array(2);

        ItemsToSwitch[ItemCount][0] = NewItemIndex;
        ItemsToSwitch[ItemCount][1] = this;

        SetItems[jQuery(SetItems).size()] = NewItemIndex;
    });

    for (var ii = 0; ii < ItemsPerSet; ii++) {

        jQuery(ItemsToSwitch).each(function () {
            if (this[0] == ii) {
                var item = this[1];

                var timeout = window.setTimeout(function () {

                    SetNewItem(item);

                }, (SingleItemPauseDuration * ii));
            }
        });
    }

    SetNextSetIndex();

    var timeout = window.setTimeout(SwitchItems, PauseDuration);
}

function SetNewItem(AItem) {

    var itemIndex = jQuery(AItem).parent().index();
    
    if (jQuery(AItem).is(":visible")) {

        jQuery(AItem).animate({ opacity: 'toggle' }, ItemAnimationDuration, function () {

            var NewItemIndex = itemIndex + (NextSet * ItemsPerSet);

            if (NewItemIndex < jQuery(LayoutItems).size()) {

                d = new Date();
                jQuery(AItem).attr({ src: LayoutItems[NewItemIndex] + '?' + d.getTime() });

                jQuery(AItem).onreadystatechange = function () {
                    if (this.readyState == 'complete') jQuery(AItem).animate({ opacity: 'toggle' }, ItemAnimationDuration);
                }
                jQuery(AItem).onload = jQuery(AItem).animate({ opacity: 'toggle' }, ItemAnimationDuration);               
            }
        });
    }
    else {

        setTimeout(function () { jQuery(AItem).animate({ opacity: 'toggle' }, ItemAnimationDuration); }, SingleItemPauseDuration);
    }
}

function SetNextSetIndex() {

    NextSet += 1;
    if ((NextSet * ItemsPerSet) >= jQuery(LayoutItems).size()) {
        NextSet = 0;
    }
}

function LayoutTypeIsValid(ALayoutType) {

    switch (ALayoutType) {
        case "Žene":
        case "Muškarci":
        case "Korporativni image":
            return true;
        default:
            return false;
    }
}

function SetItems(ALayoutType) {

    switch (ALayoutType) {
        case "Žene":
            LayoutItems = WomanItems;
            break;
        case "Muškarci":
            LayoutItems = ManItems;
            break;
        case "Korporativni image":
            LayoutItems = CorporativeItems;
            break;
    }
}

var WomanItems = new Array();
var WomanPath = "/assets/images/header/zene/";
WomanItems[0] = WomanPath + "1.jpg";
WomanItems[1] = WomanPath + "2.jpg";
WomanItems[2] = WomanPath + "3.jpg";
WomanItems[3] = WomanPath + "4.jpg";
WomanItems[4] = WomanPath + "5.jpg";
WomanItems[5] = WomanPath + "6.jpg";
WomanItems[6] = WomanPath + "7.jpg";
WomanItems[7] = WomanPath + "8.jpg";
WomanItems[8] = WomanPath + "9.jpg";
WomanItems[9] = WomanPath + "10.jpg";

var ManItems = new Array();
var ManPath = "/assets/images/header/muskarci/";
ManItems[0] = ManPath + "1.jpg";
ManItems[1] = ManPath + "2.jpg";
ManItems[2] = ManPath + "3.jpg";
ManItems[3] = ManPath + "4.jpg";
ManItems[4] = ManPath + "5.jpg";
ManItems[5] = ManPath + "6.jpg";
ManItems[6] = ManPath + "7.jpg";
ManItems[7] = ManPath + "8.jpg";
ManItems[8] = ManPath + "9.jpg";
ManItems[9] = ManPath + "10.jpg";

var CorporativeItems = new Array();
var CorporativePath = "/assets/images/header/korporativni-image/";
CorporativeItems[0] = CorporativePath + "1.jpg";
CorporativeItems[1] = CorporativePath + "2.jpg";
CorporativeItems[2] = CorporativePath + "3.jpg";
CorporativeItems[3] = CorporativePath + "4.jpg";
CorporativeItems[4] = CorporativePath + "5.jpg";
CorporativeItems[5] = CorporativePath + "6.jpg";
CorporativeItems[6] = CorporativePath + "7.jpg";
CorporativeItems[7] = CorporativePath + "8.jpg";
CorporativeItems[8] = CorporativePath + "9.jpg";
CorporativeItems[9] = CorporativePath + "10.jpg";
