$(document).ready(function () {
    /* Homepage */
    $('.hero #slideshow')
		.after('<div id="controls"></div>')
		.cycle({
		    fx: 'fade',
		    pager: '#controls',
		    timeoutFn: calculateTimeout8
		});

    randomHome1();
    randomHome2();
    randomHome3();

    $('#mpora').click(function (event) {
        $.colorbox({ width: "530px;", height: "370px", inline: true, href: "#mporaVideo" });
    });

    /* Company Page */
    $('.companyRotator #slideshow')
	    .after('<div id="controls"></div>')
	    .cycle({
	        fx: 'fade',
	        pager: '#controls',
	        timeoutFn: calculateTimeout5
	    });


    /* Team Rotator */
    $('.teamRotator #slideshow')
	    .after('<div id="controls"></div>')
	    .cycle({
	        fx: 'fade',
	        pager: '#controls',
	        timeoutFn: calculateTimeout5
	    });

    randomtip2();

    function calculateTimeout8(currElement, nextElement, opts, isFoward) {
        var index = opts.currSlide;
        if (index == 2) {
            return 8000;
        } else {
            return 8000;
        }
    }

    function calculateTimeout5(currElement, nextElement, opts, isFoward) {
        var index = opts.currSlide;
        if (index == 2) {
            return 5000;
        } else {
            return 5000;
        }
    }



    /* Tabs */
    var tabs = $('.tabs a');

    if (tabs.length) {
        if (window.location.hash.length) {
            var rHash = window.location.hash.toLowerCase(),
                currentTab,
                currentPost;

            if (rHash.indexOf('events') >= 0) {
                currentTab = tabs.eq(3);
            } else {
                currentTab = tabs.eq(0);
            }
            $('.tabs .tab').removeClass('onState');
            currentTab.parents('.tab').addClass('onState');

            $('.tabContent').hide();
            currentTab = $(currentTab.attr('href'));
            currentTab.show();


            currentPost = currentTab.find('.blogEntry').eq(rHash.substring(rHash.length-1)-1);
            
            
            window.scrollTo(0, currentPost.offset().top);
        }

        $('.tabs a').click(function (event) {

            var tab = $(this).attr('href');
            $('.tabs .tab').removeClass('onState');
            $(this).parent().addClass('onState');
            $('.tabContent').hide();
            $(tab).show();
            return false;
        });

    }





    /* Products Landing */
    $('ul.productsLanding li:odd').addClass('even');

    /* Accordion */
    if ($('.accordion')) {
        $(".accordion").accordion({
            autoHeight: false,
            navigation: true,
            active: false,
            collapsible: true
        });
    }

    /* P2P Online Dealer */
    $('#p2PBtn').click(function (event) {
        if ($("#p2p-".concat($('#Size').val())).length > 0) {
            $('html, body').animate({ scrollTop: 0 }, 0);
            $.colorbox({ width: "480px;", inline: true, href: "#p2p-".concat($('#Size').val()), top: 100 });
            return false;
        }
    });
});


this.randomHome1 = function () {
    var length = $(".homeAImage li").length;
    var ran = Math.floor(Math.random() * length) + 1;
    $(".homeAImage li:nth-child(" + ran + ")").show();
};

this.randomHome2 = function () {
    var length = $(".homeBImage li").length;
    var ran = Math.floor(Math.random() * length) + 1;
    $(".homeBImage li:nth-child(" + ran + ")").show();
};

this.randomHome3 = function () {
    var length = $(".homeCImage li").length;
    var ran = Math.floor(Math.random() * length) + 1;
    $(".homeCImage li:nth-child(" + ran + ")").show();
};

this.randomtip2 = function () {
    var length = $(".teamFeatured li").length;
    var ran = Math.floor(Math.random() * length) + 1;
    $(".teamFeatured li:nth-child(" + ran + ")").show();
};






/* Country Select Drop Down in Footer  */
/* Updates promotion code to correspond to selected country */

$(".countrySelect").change(function () {
    $.getJSON("/Json/SetPromotionCodeByCountryCode/" + $(".countrySelect option:selected").val(), function (reseult) {

    });

    var formData = "arg=" + $('.countrySelect option:selected').val();
    $.ajax({
        url: '/Json/SetCountryCode',
        type: 'POST',
        dataType: 'json',
        data: formData,
        success: function (d) {
            if (d.result) {
                location.reload();
            } else {
                $('.error-message').text(d.msg).fadeIn('fast').delay(5000).fadeOut('fast');
            }
        },
        error: function (a, b, c) {
            $('.error-message').text(b).fadeIn('fast').delay(5000).fadeOut('fast');
        }
    });

});

