﻿/*Cufon*/
Cufon.replace('#main-menu a, #sub-nav a, .copyright, .cufon, .button span', {
    hover: true
});

Cufon.replace('#google-container h3');




$(function () {


    var deviceAgent = navigator.userAgent.toLowerCase();
    var isIOS = deviceAgent.match(/(iphone|ipod|ipad)/);

    // dropdown




    if (isIOS) {
        $('.dropdown').click(function () {
            $(this).toggleClass('active');
        });
    } else {
        $('.dropdown').hover(function (e) {
            $(this).addClass('active');
            e.preventDefault();
        }, function (e) {
            $(this).removeClass('active');
            e.preventDefault();
        });
    }








    $('#sub-nav a').mousedown(function () {
        if (!$(this).parent().hasClass('dropdown')) {
            $('#sub-nav a').removeClass('active');
            $(this).addClass('active');
        }
        Cufon.refresh();
    });


    $('a#google-icon').click(function (e) {
        e.preventDefault();
        $('#google-container').fadeToggle('fast');
        $(this).toggleClass('active');
    });




    // Homepage rotators

    $('#home-show-rotator .show-rotator-slides').cycle({
        fx: 'fade',
        timeout: 0,
        pager: '.show-rotator-pager'
    });



















    // Home page promo rotator    

    $('#home-promo-rotator .promo-rotator-slides').cycle({
        fx: 'fade',
        pagerAnchorBuilder: function (idx, slide) {
            // return selector string for existing anchor             
            return '.promo-rotator-pager li:eq(' + idx + ') a';
        },
        after: function (currSlideElement, nextSlideElement, options, forwardFlag) {

            $('.promo-rotator-pager li').removeClass('activeSlide');
            $('.promo-rotator-pager li:eq(' + options.currSlide + ')').addClass('activeSlide');
        }
    });

    $('.promo-rotator-pager li').mousedown(function () {
        $('.promo-rotator-pager li').removeClass('activeSlide');
        $(this).addClass('activeSlide');
    });



    var homePromoIndex = 0;

    $(".promo-rotator-prev").click(function () {
        if (homePromoIndex > 0) {
            homePromoIndex--;
            $(".promo-rotator-next").show();
            if (homePromoIndex == 0) {
                $(this).hide();
            }
        } else {
            $(this).hide();
        }
        updatePagerThumbs(homePromoIndex, 'promo-rotator-pager', 6);
    });

    $(".promo-rotator-next").click(function () {
        if (homePromoIndex < $('.promo-rotator-pager li').length - 6) {
            homePromoIndex++;
            $(".promo-rotator-prev").show();
            if (homePromoIndex == $('.promo-rotator-pager li').length - 6) {
                $(this).hide();
            }
        } else {
            $(this).hide();
        }
        updatePagerThumbs(homePromoIndex, 'promo-rotator-pager', 6);
    });
    $(".promo-rotator-prev").hide();

    if ($('.promo-rotator-pager li').length <= 6) {
        $(".promo-rotator-next").hide();
    }

    updatePagerThumbs(homePromoIndex, 'promo-rotator-pager', 6);






















    if ($('.licensing-rotator-slides .slide').length <= 1) {

        $('.licensing-rotator-pager li').addClass('active');
        $('.licensing-rotator-slides .slide').show();

    } else {
        var cyclePowerUp = false;

        $('.licensing-rotator-pager a').click(function (e) {

            e.preventDefault();

            $('.licensing-rotator-pager li').removeClass("active");

            $(this).parent().addClass("active");

            // cycle is created on the first item clicked. 
            if (!cyclePowerUp) {

                // licensing brand icon rotators
                $('#licensing-rotator .licensing-rotator-slides').cycle({
                    fx: 'none',
                    timeout: 0,
                    startingSlide: $('.licensing-rotator-pager a').index(this),
                    pagerAnchorBuilder: function (idx, slide) {
                        // return selector string for existing anchor             
                        return '.licensing-rotator-pager li:eq(' + idx + ') a';
                    }
                });

                cyclePowerUp = true;

            }

        });


        var licensingIndex = 0;

        $(".licensing-rotator-prev").click(function () {
            if (licensingIndex > 0) {
                licensingIndex--;
                $(".licensing-rotator-next").show();
                if (licensingIndex == 0) {
                    $(this).hide();
                }
            } else {
                $(this).hide();
            }
            updatePagerThumbs(licensingIndex, 'licensing-rotator-pager', 6);
        });

        $(".licensing-rotator-next").click(function () {
            if (licensingIndex < $('#licensing-rotator .slide').length - 6) {
                licensingIndex++;
                $(".licensing-rotator-prev").show();
                if (licensingIndex == $('.licensing-rotator-pager li').length - 6) {
                    $(this).hide();
                }
            } else {
                $(this).hide();
            }
            updatePagerThumbs(licensingIndex, 'licensing-rotator-pager', 6);
        });



        updatePagerThumbs(licensingIndex, 'licensing-rotator-pager', 6);


    }

    $(".licensing-rotator-prev").hide();

    if ($('.licensing-rotator-pager li').length <= 6) {
        $(".licensing-rotator-next").hide();
    }




    //show detail gallery images

    $("a[rel=lightbox]").colorbox({
        slideshow: true,
        onLoad: videoPlayerPause
    });






    if ($('.gallery-control-pager li').length <= 3) {
        $('.gallery-control-prev,  .gallery-control-next').hide();
    }
    var galleryIndex = 0;
    $(".gallery-control-prev").click(function () {
        if (galleryIndex > 0) {
            galleryIndex--;
            $(".gallery-control-next").show();
            if (galleryIndex == 0) {
                $(this).hide();
            }
        } else {
            $(this).hide();
        }
        updatePagerThumbs(galleryIndex, 'gallery-control-pager', 3);
    });
    $(".gallery-control-next").click(function () {
        if (galleryIndex < $('.gallery-control-pager li').length - 3) {
            galleryIndex++;
            $(".gallery-control-prev").show();
            if (galleryIndex == $('.gallery-control-pager li').length - 3) {
                $(this).hide();
            }
        } else {
            $(this).hide();
        }
        updatePagerThumbs(galleryIndex, 'gallery-control-pager', 3);
    });
    $(".gallery-control-prev").hide();
    if ($('.gallery-control-pager li').length <= 3) {
        $(".gallery-control-prev").hide();
    }
    updatePagerThumbs(galleryIndex, 'gallery-control-pager', 3);




    $('.gallery-control-pager a').click(function (e) {
        if (!$(this).hasClass("cboxElement")) {
            // action when light box isn't avalible.
            e.preventDefault()
            $("#show-hero img").attr('src', $(this).attr('href'));
        }
        $('.gallery-control-pager li').removeClass("activeSlide");
        $(this).parent().addClass("activeSlide");
    });








    // new item pagination 


    var num_entries = $('#news-items-source div.news').length;
    $("#news-items-source").hide();
    // Create content inside pagination element
    $("#pagination").pagination(num_entries, {
        callback: pageselectCallback,
        items_per_page: 10 // Show only one item per page
    });


});


function pageselectCallback(page_index, jq) {

    var max_elem = 10;

    $('#news-items').empty();

    for (var i = page_index * max_elem; i < (page_index + 1) * max_elem; i++) {

        $('#news-items').append($('#news-items-source div.news:eq(' + i + ')').clone());
    }

    $('#news-items .button').each(function (index) {

        var label = $(this).find("cufontext").html();

        if (label) {
            $(this).find("span").empty().html('<span class="icon"></span>Download');
        }

    });

    Cufon.refresh();
    return false;
}

function updatePagerThumbs(index, pagerContainerClass, visibleElements) {

    var liIndex = 0;

    $("." + pagerContainerClass + " li").show();

    $("." + pagerContainerClass + " li").each(function () {

        if (liIndex < index || index + visibleElements <= liIndex) {
            $(this).hide();
        }

        liIndex++;

        //console.log(liIndex , index);

    });


}


function videoPlayerPause() {
    if (videoPlayer.isPlaying()) {
        videoPlayer.pause(true);
    }
}
