﻿/// <reference path="jquery-vsdoc.js" />
//*************************************************************************************
// File     : arbu-functions.js
// Requires : jquery.js (version 1.4.4+)
// Author   : Kyle Weems (ksw)
// Origin   : mindfly.com
// Created  : Nov 19, 2010
// Modified : Aug 03, 2011
//*************************************************************************************

var bannerSliderRunning = false;
var queue = new Array();
var carousel = {};
var video = {};
carousel.vars = {
	difference: 0,
	drag: false
};

$(document).ready(function() {
    banner();
    carousel.func.init();
    video.func.init();
    if ($('#home-rotator').length > 0) var t = setTimeout("homeRotator();", 5000);
    accessoriesSlide();
    servicesSlide();
    if (typeof (window['bg']) != "undefined" && $('#twitter').length > 0) bg.Twitter.buildABadge('ARB_USA', '#twitter', 'tweet tweet-time-and-source in-reply-to');
    shoppingToggle();
    dealerMap();
    boxBlurs();
    showCategoryCarousel();
    //slideToProductCategory();
});

function banner() {
    if ($('#banner').length > 0) {
        $('.buttons li:eq(0)').addClass('current');
        $('.bannerList li:eq(0)').addClass('current');
        $('#banner button').click(function() {
            if (!bannerSliderRunning) {
                bannerSliderRunning = true;
                if ($(this).parent('li').hasClass('current')) {
                    bannerSliderRunning = false;
                } else {
                    $('.buttons .current').removeClass('current');
                    $(this).parent('li').addClass('current');
                    var i = $('.buttons button').index($(this));
                    $('.bannerList li:eq(' + i + ')').addClass('upcoming');
                    $('.bannerList .upcoming').animate({ left: 0 }, 1000, 'swing', function() {
                        $('.bannerList .current').removeClass('current').attr('style', '');
                        if ($('.bannerList .upcoming .box, .bannerList .upcoming .fadein').length > 0) {
                            $('.bannerList .upcoming .box, .bannerList .upcoming .fadein').animate({ opacity: 1 }, 500, function() {
                                $('.bannerList .upcoming').addClass('current').removeClass('upcoming');
                                if ($('.bannerList li.current:first-child').length < 1) $('.bannerList li:first-child').css({ left: '730px' });
                                $('.bannerList .current .box, .bannerList .current .fadein').attr('style', '');
                                bannerSliderRunning = false;
                                runQueue();
                            });
                        } else {
                            $('.bannerList .upcoming').addClass('current').removeClass('upcoming');
                            if ($('.bannerList li.current:first-child').length < 1) $('.bannerList li:first-child').css({ left: '730px' });
                            bannerSliderRunning = false;
                            runQueue();
                        }
                    });
                }
            } else {
                if ($(this).parent('li').hasClass('current') == false) {
                    queue.push($(this));
                }
            }
        });
    }
}

function runQueue() {
    if (queue.length > 0) {
        var next = queue[0];
        queue.shift();
        next.click();
    }
}

carousel.func = {
    init: function() {
        carousel.func.calculatedifference();
        carousel.func.dragbind();
    },
    dragbind: function() {
        $('#meter').live('mousedown', function() { carousel.vars.drag = true; });
        $('html').live('mouseup', function() { carousel.vars.drag = false });
        $('html').live('mousemove', function(e) { carousel.func.move(e); });
        $('#slider').live('click', function(e) { carousel.func.hop(e); });
    },
    move: function(e) {
        if (carousel.vars.drag) {
            var x = ((e.pageX - $('#slider').offset().left) - ($('#meter').outerWidth() / 2));
            if (x < 10) x = 10;
            if (x > 720) x = 720;
            $('#meter').css({ left: x + 'px' });
            var percent = (x - 10) / 692;
            $('.carouselCategories.stickyList li:first-child').css({ marginLeft: (carousel.vars.difference * percent) + 'px' });
        }
    },
    hop: function(e) {
        var c = ((e.pageX - $('#slider').offset().left) - ($('#meter').outerWidth() / 2));
        var m = $('#meter').css('left').split('px')[0] * 1;
        if (c < m) {
            var x = m - 70;
        } else {
            var x = m + 70;
        }
        if (x < 10) x = 10;
        if (x > 720) x = 720;
        $('#meter').css({ left: x + 'px' });
        var percent = (x - 10) / 692;
        $('.carouselCategories.stickyList li:first-child').css({ marginLeft: (carousel.vars.difference * percent) + 'px' });
    },
    calculatedifference: function() {
        var width = 0;
        for (i = 0; i < $('.categories #primary .stickyList li').length; i++) {
            width = width + $('.categories #primary .stickyList li:eq(' + i + ')').outerWidth(true);
        }
        carousel.vars.difference = ($('.categoryWrapper').width() - 20) - width;
    }
}


function homeRotator() {
    if ($('#home-rotator .current').length < 1) {
        $('#home-rotator li:last-child').addClass('current');
    }
    if ($('#home-rotator').length > 0) {
        var i = $('#home-rotator li').index($('#home-rotator .current'));
        if (i > 0) {
            $('#home-rotator .current').animate({ opacity: 0 }, 1000, function () {
                $(this).css({ display: 'none' });
                var i = $('#home-rotator li').index($('#home-rotator .current')) - 1;
                $('#home-rotator .current').removeClass('current');
                if (i < 0) i = $('#home-rotator li').length;
                $('#home-rotator li:eq(' + i + ')').addClass('current');
                var t = setTimeout("homeRotator();", 5000);
            });
        } else {
            $('#home-rotator li:last-child').css({ display: 'block' });
            $('#home-rotator li:last-child').animate({ opacity: 1 }, 1000, function() {
                var i = $('#home-rotator li').index($('#home-rotator .current')) - 1;
                $('#home-rotator li').attr('style', '');
                $('#home-rotator .current').removeClass('current');
                if (i < 0) i = $('#home-rotator li').length;
                $('#home-rotator li:eq(' + i + ')').addClass('current');
                var t = setTimeout("homeRotator();", 5000);
            });
        }
    }
}

function accessoriesSlide() {
    $('.accessories span').live('click', function () {
        $(this).siblings('.slide').slideToggle();
        $(this).toggleClass('active');
    });
}

function servicesSlide() {
    $('.services h4').live('click', function () {
        $(this).siblings('.tableWrapper').slideToggle();
        $(this).toggleClass('active');
    });
}

function shoppingToggle() {
    $('.shopping, #shoppingList .close').live('click', function () {
        $('#shoppingList').slideToggle();
    });
}

video.func = {
    init: function() {
        video.func.bind();
        video.func.footer();
    },
    bind: function() {
        if ($('#video-feed').length > 0) {
            $('#video-feed a').bind('click', function() {
                video.func.popup($(this).attr('id'), $(this).attr('title'), $('#video-feed a').index($(this)));
                return false;
            });
            $('#shroud, #video-popup .close').live('click', function() {
                $('#video-popup').animate({ opacity: 0 }, function() {
                    $('#shroud').animate({ opacity: 0 }, function() {
                        $('#video-popup, #shroud').css('display', 'none');
                    });
                });
            });
            $('#video-popup .next, #video-popup .previous').live('click', function() {
                var n = $('#video-popup .counter .current').html() * 1;
                var m = $('#video-popup .counter .total').html() * 1;
                if ($(this).hasClass('previous')) {
                    n = n - 1;
                    if (n < 1) n = m;
                } else {
                    n = n + 1;
                    if (n > m) n = 1;
                }
                var a = $('#video-feed a:eq(' + (n - 1) + ')');
                video.func.popup(a.attr('id'), a.attr('title'), (n - 1));
            });
        }
    },
    popup: function(id, title, n) {
        if ($('#shroud').length < 1) $('body').append('<div id="shroud"></div>');
        if ($('#video-popup').length < 1) $('body').append('<div id="video-popup"><h3></h3><button type="button" class="previous">previous</button><span class="counter"><span class="current"></span> of <span class="total"></span></span><button type="button" class="next">next</button><button type="button" class="close">x</button></div>');
        $('#video-popup object').remove();
        $('#video-popup').prepend('<object width="480" height="390"><param name="movie" value="http://www.youtube.com/v/' + id + '?fs=1&amp;hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' + id + '?fs=1&amp;hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="390"></embed></object>');
        $('#video-popup h3').html(title);
        $('#video-popup .counter .current').html(n + 1);
        $('#video-popup .counter .total').html($('#video-feed a').length);
        if ($('#video-popup').css('display') != 'block') {
            $('#video-popup').css({ opacity: 0, display: 'block' });
            $('#shroud').css({ width: $(window).width() + 'px', height: $(window).height() + 'px', opacity: 0, display: 'block' });
            $('#shroud').animate({ opacity: 1 }, function() {
                $('#video-popup').animate({ opacity: 1 });
            });
        }
    },
    footer: function() {
        if ($('#footer .videos').length > 0) {
	    if ($('html').hasClass('old-ie')) {
	            bg.Google.youtube.insert_feed_thumbnails('#footer .videos', 'ARB4WD', 1, 4, 'ul');
	    } else {
	            bg.Google.youtube.insert_feed('#footer .videos', 'ARB4WD', 1, 4, 'ul', function() {
	                $('#footer .videos object, #footer .videos embed').attr({ width: '98', height: '60' });
	                $('#footer .videos li:first-child object').attr({ width: '323', height: '195' });
	                $('#footer .videos li:first-child embed').attr({ width: '340', height: '205' });
	            });
	    }
        }
    }
};


function dealerMap() {
    if ($('div[id$="dealerList"] .geo').length > 0) {
        $('.googleMap').css({ minHeight: '237px' }).html('');
        bg.Google.map.loadFromGeo('ol .vcard:first-child .geo', '.googleMap', false, 10, function() { loadDealerMarkers(); });
    }
}

function loadDealerMarkers() {
    var max = jQuery('div[id$="dealerList"] .vcard').length;
    if (max > 26) max = 26;
    for (i = 0; i < max; i++) {
        bg.Google.map.addMarker(bg.Google.map.getLatLngFromGeo('ol .geo:eq(' + i + ')'), jQuery('ol .vcard:eq(' + i + ') .fn.org').text(), i);
    }
}

function boxBlurs() {
    $('.inptZip').focus(function() { if ($(this).attr('value') == 'zip/postal code') $(this).attr('value', ''); });
    $('.inptZip').blur(function() { if ($(this).attr('value') == '') $(this).attr('value', 'zip/postal code'); });
    $('.search input[type="text"]').focus(function() { if ($(this).attr('value') == 'Search ARBUSA.com') $(this).attr('value', ''); });
    $('.search input[type="text"]').blur(function() { if ($(this).attr('value') == '') $(this).attr('value', 'Search ARBUSA.com'); });    
}

function showCategoryCarousel() {
    if ($('.carousel').length > 0) {
        var w = 0;
        for (i = 0; i < $('.carouselCategories li').length; i++) {
            w = w + $('.carouselCategories li:eq(' + i + ')').outerWidth();
        }
        if (w < 880) $('#primary').addClass('no-slider');
    }
}

//function slideToProductCategory() {
//    $('.carouselCategories a').live('click', function() {
//        if (window.location.hash != "#primary") {
//            window.location.hash = "#primary";
//        } else {
//            window.location.hash = "#secondary";
//        }
//    });
//}
