﻿$(document).ready(function () {
    //Set background image (if exists);
    var pic_real_width, pic_real_height;
    $(".illustration img").each(function () {
        var image = $(this);
        if (image != null && image.attr("src") != undefined) {
            $("<img />") // Make in memory copy of image to avoid css issues
                    .attr({ src: $(image).attr("src") + '?random=' + (new Date()).getTime() })//fix for i.e.
                    .load(function () {
                        pic_real_width = this.width;   // Note: $(this).width() will not
                        pic_real_height = this.height; // work for in memory images.
                        if ($('.content.threeCol').length) {
                            //fix styles according to image dimentions
                            $('.container').attr('style',
                                'background-image:url("' + image.attr("src") + '");' +
                                'background-repeat:no-repeat;' +
                                'background-position: right 110px;'
                            );
                            $('div.rightContent').attr('style', 'padding-bottom:' + pic_real_height + 'px;');
                        }
                        else if ($('.content.oneCol.twosubs').length) {
                            $('.container').attr('style',
                                'background-image:url("' + image.attr("src") + '");' +
                                'background-repeat:no-repeat;' +
                                'background-position:85% bottom;'
                            );
                            $('div.rightContent').attr('style', 'padding-bottom:' + pic_real_height + 'px;');
                        }
                        else {
                            //fix styles according to image dimentions
                            $('.container').attr('style',
                                'background-image:url("' + image.attr("src") + '");' +
                                'background-repeat:no-repeat;' +
                                'background-position:bottom right;'
                            );
                            $('div.rightContent').attr('style', 'padding-bottom:' + pic_real_height + 'px;');
                        }
                    });
        }
    });
    $('.illustration').remove();



    //gets selected tab and div refrences
    var selectedTabLink = $('.whoareyou ul li a').first();
    var divId = selectedTabLink.attr('href');

    //pick out content
    var slideShow = $('#slideshowContainer').html();
    var divContent = $(divId).html();

    //remove selected look from tab
    $('.whoareyou ul li').removeClass('ui-tabs-selected');

    //remove custom look on content tab
    $(divId).removeClass('competenceContent');

    //put & style slidesow in to tab div
    $(divId).html(slideShow);
    $('#slideshowContainer').remove();
    $(divId).addClass('slideshowContainerTab');
    $(divId).removeClass('ui-tabs-panel');


    /*restore content div to original look and content
    when someone navigates the roles*/
    $('.ui-state-default a').bind('click', function () {
        $(divId).html(divContent);
        $(divId).removeClass('slideshowContainerTab');
        $(divId).addClass('ui-tabs-panel');
    });


    //adapt content in slideshow for content without image
    $('#slideshow ul li .slideshowimageandlink').each(function (i) {
        var kids = $(this).children();
        var siblings = $(this).siblings();
        var src = "";
        for (var counter = 0; counter < kids.length; counter++) {
            src = kids[counter].src;
            if (src == "" || src == undefined) {
                //                        if ($(siblings[0]).hasClass('hasImage')) {
                //                            $(siblings[0]).removeClass('hasImage');
                //                        }
            }
            else {
                $(siblings[0]).addClass('hasImage');
            }
        }

    });



    /* hook up slideshow functionality */
    var len = $("#slideshow li").length;
    var x = 0;
    //$("#slideshow #total").text(len);
    $("#slideshow li:eq(0)").addClass("current");
    $("#slideshow li:eq(0)").fadeIn('medium');
    $("#slideshow li").each(function () {
        $(this).attr('rel', x);
        x++
    });
    //$("#next").click(function () {
    $(".next").click(function () {
        var current = $("#slideshow .current");
        var next = parseFloat(current.attr('rel')) + 1;
        if (next == len) {
            next = 0;
        }
        //$("#num").text(parseFloat(next) + 1);
        current.fadeOut('medium', function callback() {
            current.removeClass('current');
            $("#slideshow li").each(function () {
                if ($(this).attr('rel') == next) {
                    $(this).fadeIn('medium');
                    $(this).addClass('current');
                }
            });
        });

    });
});
