/*
 * global.js
 * Global javascript to be run on every page of the ItalBooks web site.
 * 
 * Copyright Amway
 */

italbooks=window.italbooks || {};

italbooks.common = (function() {
    // private

    function init() {
        //initialize common code
        userAgent();
        carousel();
        fancySelect();
        accordion();
        worldMap();
        textValueSwap();
        exhibitors();
        lookInside();
        swapSpotlight();
    }

    // add a user agent class to the HTML element for easier browser targeting
    function userAgent() {
        var ua = navigator.userAgent;
        var ie = /MSIE/.test(ua) && ua.replace(/.*?MSIE (.).*/, "$1");
        var ff = /Firefox/.test(ua) && ua.replace(/.*?Firefox\/(.)\.(.).*/, "$1_$2");
        var safari = /Safari/.test(ua) && ua.replace(/.*?Version\/(.).*/, "$1");
        var chrome = /Chrome/.test(ua) && ua.replace(/.*?Chrome\/(.).*/, "$1");
        var opera = /Opera/.test(ua) && ua.replace(/.*?Opera\/(.).*/, "$1");
        document.getElementsByTagName("html")[0].className = "js" + (ie ? " ie ie" + ie : ff ? " ff ff" + ff : chrome ? " chrome chrome" + chrome : safari ? " safari safari" + safari : opera ? " opera opera" + opera : "");
    }

    function carousel() {
        $('#carousel').scrollingcarousel();
    }

    function accordion() {
        $("dl.accordion dt a").click(function(e) {
            e.preventDefault();
            var dt = $(this).parent();
            dt.toggleClass('open')
				.siblings().removeClass('open').end()
				.next().toggle().siblings('dd').hide();
            $(this).blur();
        });
        // expand the first by default
        if (showFirstAccordion == true) {
            $('dl.accordion dt:first a').trigger('click');
        } else {
            var showFirstAccordion = false;
        }
    }

    function fancySelect() {
        $('.fancy-select').selectbox();
    }

    function worldMap() {
        var $map = $('#map');
        var imageSrc = {
            americas: 'resources/images/world-map-americas.gif',
            europe: 'resources/images/world-map-europe.gif',
            asia: 'resources/images/world-map-asia.gif'
        }
        imageSrc.current = imageSrc.americas;
        // events
        $('#americas')
			.mouseenter(
				function() {
				    $map.attr('src', imageSrc.americas);
				})
			.mouseleave(
				function() {
				    $map.attr('src', imageSrc.current);
				})
			.click(function(e) {
			    e.preventDefault();
			    $('#map-content-americas').show().siblings('ul').hide();
			    imageSrc.current = imageSrc.americas;
			});
        $('#europe').mouseenter(
			function() {
			    $map.attr('src', imageSrc.europe);
			})
			.mouseleave(
				function() {
				    $map.attr('src', imageSrc.current);
				})
			.click(function(e) {
			    e.preventDefault();
			    $('#map-content-europe').show().siblings('ul').hide();
			    imageSrc.current = imageSrc.europe;
			});
        $('#asia').mouseenter(
			function() {
			    $map.attr('src', imageSrc.asia);
			})
			.mouseleave(
				function() {
				    $map.attr('src', imageSrc.current);
				})
			.click(function(e) {
			    e.preventDefault();
			    $('#map-content-asia').show().siblings('ul').hide();
			    imageSrc.current = imageSrc.asia;
			});
        // default to showing events for Americas
        $('#map-content-americas').show();
        $map.attr('src', imageSrc.americas);
    }

    // toggles the text value in an input field
    function textValueSwap() {
        $('.text-value-swap').each(function() {
            var $input = $(this);
            var origValue = $(this).attr('value');
            $input.focus(function() {
                $input.attr('value', '');
            });
            $input.blur(function() {
                if ($input.attr('value') == '') {
                    $input.attr('value', origValue);
                }
            });
        });
    }

    function exhibitors() {
        $('#view-all-exhibitors').click(function(e) {
            e.preventDefault();
            $('#all-exhibitors').slideDown('slow', function(e) {
                $('.close', $(this)).click(function(e) {
                    e.preventDefault();
                    $('#all-exhibitors').slideUp('slow');
                });
            });
        });
    }

    function lookInside() {
        // hide the gallery images
        $('#look-inside').css('display', 'none');
        // set up trigger
        $('#look-inside-trigger').click(function(e) {
            e.preventDefault();
            $('#look-inside a:first').trigger('click');
        });
    }

	function swapSpotlight() {
		// show the first spotlight on load
		$('.spotlight-tab:first').css({left:'10px'}).addClass('active');
		
		// set up click events and animations
		$('.home-spotlight .module-head ul > li a').click(function(e){
			if($(this).attr('href') != '#') return true;
			e.preventDefault();
			// elements
			var index = $(this).parents('ul').children().index($(this).parent()),
				$container = $(this).parents('div.module').find('div.module-body'),
			 	$tab = $container.children().filter('div.spotlight-tab').eq(index),
			 	$active = $container.children().filter('div.active'),
				position = ($container.children().index($active) > index) ? '+' : '-';
			// if we are at current active, return
			if($container.children().index($active) == index) return;
			// animate and reset previous active tab
			$tab.animate({left:'10px'}).addClass('active');
			$active.animate({left:position+'2000px'},function(){
				// reset
				$(this).removeClass('active')
					.hide()
					.css({left:position+'2000px'})
					.show();				
			});
		});
	}

    return {
        // public
        init: init
    };
})();

jQuery(document).ready(italbooks.common.init);

// set up TopUp presets
TopUp.images_path = "/resources/js/libs/images/top_up/";
TopUp.addPresets({
	"#look-inside a": {
      	group: "images",
      	layout: "dashboard",
      	modal: 1,
		resizable:0,
		shaded:1,	
		effect:'appear'
    }
  });
