﻿$(document).ready(function () {
    $('a').hoverFadeColor();
    tabs();
    boxes();
    removeBorderOnLinkImage()
    newsOnHomepage();
    pckgsOnHomepage();
    $(".fancyMe").fancybox();

    groupLayer();

    startSlideshow();

});



////all page is loaded
//$(window).ready(function () {
//    startSlideshow();
//});


function groupLayer() {
    $('#motg-toggle1').hover(function () {
        $('#slickbox').fadeIn('fast');
    })
    $('#slickbox').hover(function () {
    }, function () {
        $(this).fadeOut('fast')
    })
}
function newsOnHomepage() {
    $('#zone3 .newsList').cycle({
        fx: 'fade',
        speed: 400,
        timeout: 5000,
        pager: '#packagesPager',
        pagerAnchorBuilder: function (index, el) {
            return '<a href="#">&nbsp;</a>';
        }
    });
    $('#zone3 .newsList a').hover(function () {
        $(this).find('img').stop().fadeTo('fast', 0.2);
        $('#zone3 .newsList').cycle('pause')
    }, function () {
        $(this).find('img').stop().fadeTo('fast', 0.6);
        $('#zone3 .newsList').cycle('resume')
    })
}
function pckgsOnHomepage() {
    $('#pckgsHomePage').cycle({
        fx: 'fade',
        speed: 400,
        timeout: 5000,
        pager: '#packagesPager2',
        pagerAnchorBuilder: function (index, el) {
            return '<a href="#">&nbsp;</a>';
        }
    });
    $('#pckgsHomePage a').hover(function () {
        $(this).find('img').stop().fadeTo('fast', 0.2);
        $('#pckgsHomePage').cycle('pause')
    }, function () {
        $(this).find('img').stop().fadeTo('fast', 0.6);
        $('#pckgsHomePage').cycle('resume')
    })
}
function removeBorderOnLinkImage() {
    var img = $('a').find('img')
    $(img).parent().css('border-bottom', 0)
}
function boxes() {
    $('.sectionsList li a img').hover(function () {
        $(this).stop().fadeTo('fast', 1);
        $(this).parent().parent().find('div').fadeTo('fast', 0);
    }, function () {
        $(this).stop().fadeTo('fast', 0.45);
        $(this).parent().parent().find('div').fadeTo('fast', 1);
    })
}
function tabs() {
    //When page loads...
    $(".tab_content").hide(); //Hide all content
    $("ul.tabs li:first").addClass("active").show(); //Activate first tab
    $(".tab_content:first").show(); //Show first tab content
    //On Click Event
    $("ul.tabs li").click(function () {
        $("ul.tabs li").removeClass("active"); //Remove any "active" class
        $(this).addClass("active"); //Add "active" class to selected tab
        $(".tab_content").hide(); //Hide all tab content
        var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
        $(activeTab).fadeIn(); //Fade in the active ID content
        return false;
    });
}


function startSlideshow() {
    if ($('#slideshow').length > 0) {
        loadSlideshow();
    }
}



//var actions;
//var loadingCounter = 1;
function loadSlideshow() {


    //actions = $('#slideshow img').not(':first').size();
    //$('#int').html(loadingCounter);
    //$('#dec').html(actions + 1);
    $('#slideshow img').delay(500).fadeIn('slow', function () {
        loadImg()
    });
}

function loadImg() {
//    var imgToLoad = new Image();
//    var src = $(img).attr('srcToLoad');
//    $(imgToLoad).load(next(img, src)).attr('src', src);
//    function next(img, src) {
//        $(img).removeAttr('srcToLoad').attr('src', src);
//        loadingCounter += 1;
//        $('#int').html(loadingCounter);
//        if (--actions) return
//        $('#loader').fadeOut('fast', function () {
//            showHideMenu();
//            $('#ssSpacer .prv, #ssSpacer .nxt').fadeIn('fast');
//            startCycle();
//        });
    //    }


    $.post('/ajax/carousel.aspx',
            {
                idMediaNode: $('#slideshow').attr('idMediaNode'),
                contentTypeAlias: $('#slideshow').attr('contentTypeAlias'),
                cultureAlias: $('#slideshow').attr('cultureAlias')
            },
            function (data) {

                //prints images insider the correct div
                $('#slideshow').append(data);
                //remove loading
                $('#loader').fadeOut('fast', function () {
                    showHideMenu();
                    $('#ssSpacer .prv, #ssSpacer .nxt').fadeIn('fast');
                    startCycle();
                });

            })


}





function showHideMenu() {
    $('.prv, .nxt').click(function () {
        if ($('#main').is(':visible')) {
            mainSlideToggle()
        }
    })
    $('#ssSpacer img').addClass('cursorMinus');
    $('#ssSpacer img, #menu').click(function () {
        if ($('#main').is(':visible')) {
            $('#ssSpacer img').removeClass('cursorMinus').addClass('cursorPlus');
            //$('#ssSpacer img').css('cursor', 'url(/images/plus.png), pointer');
            mainSlideToggle()
        } else {
            $('#ssSpacer img').removeClass('cursorPlus').addClass('cursorMinus');
            //$('#ssSpacer img').css('cursor', 'url(/images/minus.png), pointer');
            menuSlideToggle()
        }
    })
    //top, //bottom
    $(document).keydown(function (e) {
        $('#slideshow').cycle('pause');
        if (e.keyCode == 38) {
            if ($('#main').is(':visible')) {
                mainSlideToggle()
            }
            return false;
        }
        if (e.keyCode == 40) {
            if ($('#main').is(':visible') == false) {
                menuSlideToggle()
            }
            return false;
        }
    });
    function mainSlideToggle() {
        $('#main').slideToggle('fast', function () {
            $('#menu').slideToggle('fast')
        });
    }
    function menuSlideToggle() {
        $('#menu').slideToggle('fast', function () {
            $('#main').slideToggle('fast')
        })
    }
}
function startCycle() {
    $('#slideshow').cycle({
        fx: 'fade',
        speed: 1000,
        timeout: 4000,
        sync: true,
        next: '.nxt',
        prev: '.prv',
        after: onAfter,
        before: onBefore,
        containerResize: false,
        slideResize: false
    });
    $('.prv, .nxt, #ssSpacer img').click(function () {
        $('#slideshow').cycle('pause');
    })
    //left, //right
    $(document).keydown(function (e) {
        $('#slideshow').cycle('pause');
        if (e.keyCode == 37) {
            $("#ssSpacer .prv").trigger('click').addClass('trigged');
            return false;
        }
        if (e.keyCode == 39) {
            $("#ssSpacer .nxt").trigger('click').addClass('trigged');
            return false;
        }
    });
    $(document).keyup(function (e) {
        if (e.keyCode == 37) {
            $("#ssSpacer .prv").removeClass('trigged');
            return false;
        }
        if (e.keyCode == 39) {
            $("#ssSpacer .nxt").removeClass('trigged');
            return false;
        }
    })
}
//****EVENTS
$(document).ready(function () {
    $('.photoGallery .items').cycle({
        fx: 'fade',
        speed: 400,
        timeout: 5000,

        next: '.photoGallery .next',
        prev: '.photoGallery .previous'
    })
    $(".photoGallery .next, .photoGallery .previous").click(function () {
        $('.photoGallery .items').cycle('pause');
    })
})
function showTab(tabId) {
    $(".tagsTabs li").attr("class", "")
    $("#tabSelector-" + tabId).attr("class", "selected");
    $(".eventsTab").hide();
    $("#tabs-" + tabId).show();
}





function onAfter(curr, next, opts) {

    var title = $(this).attr('title');
    var link = $(this).attr('link');
    if (title.length > 0) {
        $('#caption').fadeIn('fast', function () {
            if (jQuery.browser.msie) { this.style.removeAttribute("filter") }
        });
        if (link.length > 0) {
            $('#caption p').html('<a href="' + link + '">' + title + '</a>');
        } else {
            $('#caption p').html(title);
        }
    }

}

function onBefore(curr, next, opts) {
    $('#caption').fadeOut('fast', function () {
        $(this).find('p').empty();
    });
}





//EVENTS****
