/* ---------------- */
/* GLOBAL FUNCTIONS */
/* ---------------- */

var this_domain = "http://www.cbdmarketing.com/"
// Jump to top of the page 
function jumpScroll(){
    window.scroll(0, 0);
}

// Clear input field text
function clearText(theText){
    if (theText.value == theText.defaultValue) 
        theText.value = ""
}

// Reset input field text
function showText(theText){
    if (theText.value == "") 
        theText.value = theText.defaultValue;
}

function loadItem(args){
    var items = ['facebook', 'blogs', 'twitter', 'rss'];
    for (i = 0; i < items.length; i++) {
        if ($('#links_' + items[i]).css('display', 'block')) {
            $('#links_' + items[i]).css('display', 'none');
            $('a.btn_' + items[i] + '_on').removeClass().addClass('btn_' + items[i]);
        }
    }
    $('#links_' + args).slideDown('slow');
    $('#links_' + args).css('display', 'block');
    $('a.btn_' + args).removeClass().addClass('btn_' + args + '_on');
}

$(document).ready(function(){

    /* ------------------------ */
    /* DROP-DOWN MENU FUNCTIONS */
    /* ------------------------ */
    
    // Set menu history variable
    menuHistory = null;
    
    // Assign mouseover and mouseout actions to the nav buttons
    $('div#header_buttons > ul#header_nav li.dropdown_button a').mouseover(function(){
        currentMenuName = $(this).attr('name');
        // Determine if a previous menu has already been opened by checking to see if menuHistory has a value
        if (menuHistory == null) {
            menuHistory = currentMenuName;
        }
        menu_close();
        menu_open();
        
    }).mouseout(function(){
        menuHistory = currentMenuName;
        menu_timer();
    });
    
    // Set button actions for drop-down menus
    function bindObjects(){
    
        $('.dropdown_services').bind('mouseover', menu_canceltimer);
        $('.dropdown_services').bind('mouseout', menu_timer);
        
        $('.dropdown_strategies').bind('mouseover', menu_canceltimer);
        $('.dropdown_strategies').bind('mouseout', menu_timer);
        
        $('.dropdown_our-work').bind('mouseover', menu_canceltimer);
        $('.dropdown_our-work').bind('mouseout', menu_timer);
        
        $('.dropdown_insights').bind('mouseover', menu_canceltimer);
        $('.dropdown_insights').bind('mouseout', menu_timer);
        
        $('.dropdown_about-cbd').bind('mouseover', menu_canceltimer);
        $('.dropdown_about-cbd').bind('mouseout', menu_timer);
        
        $('.dropdown_button > a').click(function(event){
            menu_close;
        });
    }
    
    // Initiate drop-down menu interactivity
    bindObjects();
    
    
    // Close all menus if a click action occurs
    document.onclick = menu_close;
    
    // Sub menu variables
    var timeOut = 500;
    var closeTimer = 0;
    var subMenuItem = 0;
    
    // Open drop-down menu
    function menu_open(){
        menu_canceltimer();
        menu_close();
        subMenuItem = $('ul .' + currentMenuName + '_items').css('display', 'block');
        $('div#header_buttons > ul.dropdown_' + currentMenuName + ' > li.' + currentMenuName + '_items a.btn_nav_' + currentMenuName + '_on').css('backgroundImage', 'url('+this_domain+'assets/images/nav_' + currentMenuName + '.png)');
        $('div#header_buttons > ul#header_nav > li a.btn_nav_' + currentMenuName + '_on').css('backgroundImage', 'url()');
        $('div#header_buttons > ul#header_nav > li a.btn_nav_' + currentMenuName).css('backgroundImage', 'url()');
    }
    
    // Close drop-down menu
    function menu_close(){
    	var currentMenuName;
        if (menuHistory == null) {
            menuHistory = currentMenuName;
        }
        $('div#header_buttons > ul#header_nav > li a.btn_nav_' + menuHistory + '_on').css('backgroundImage', 'url('+this_domain+'assets/images/nav_' + menuHistory + '.png)');
        $('div#header_buttons > ul#header_nav > li a.btn_nav_' + menuHistory).css('backgroundImage', 'url('+this_domain+'assets/images/nav_' + menuHistory + '.png)');
        if (subMenuItem) {
            subMenuItem.css('display', 'none');
        }
    }
    
    // Start drop-down menu timer
    function menu_timer(){
        closeTimer = window.setTimeout(menu_close, timeOut);
    }
    
    // Stop drop-down menu timer
    function menu_canceltimer(){
        if (closeTimer) {
            window.clearTimeout(closeTimer);
            closeTimer = null;
        }
    }
    
	/* --------------------------- */
    /* CONTENT PLACEMENT FUNCTIONS */
    /* --------------------------- */
	
    $('#news_articles').innerfade({
        animationtype: 'slide',
        speed: 1000,
        timeout: 7000,
        type: 'sequence',
        containerheight: '1em'
    });
	
	$('.dropdown_services li ul li.firstLI:first').html('<div class="top_border"></div>' + $('.dropdown_services li ul li.firstLI:first').html()).css({margin: "-5px 0px 0px 0px"});
	$('.dropdown_insights li ul li.firstLI:first').html('<div class="top_border"></div>' + $('.dropdown_insights li ul li.firstLI:first').html()).css({margin: "-5px 0px 0px 0px"});;
	$('.dropdown_strategies li ul li.firstLI:first').html('<div class="top_border"></div>' + $('.dropdown_strategies li ul li.firstLI:first').html()).css({margin: "-5px 0px 0px 0px"});;
	$('.dropdown_our-work li ul li.firstLI:first').html('<div class="top_border"></div>' + $('.dropdown_our-work li ul li.firstLI:first').html()).css({margin: "-5px 0px 0px 0px"});;
	$('.dropdown_about-cbd li ul li.firstLI:first').html('<div class="top_border"></div>' + $('.dropdown_about-cbd li ul li.firstLI:first').html()).css({margin: "-5px 0px 0px 0px"});;
	
	$('div#header_buttons > ul#header_nav > li:last').html('');
	
	$('#container_leftcolumn_content').css('height', $('#container_middlecolumn_content').height());
		
	/* ------------------------ */
    /* CLIENT OVERLAY FUNCTIONS */
    /* ------------------------ */

	$('div#client_overlay li.overlay_content').bind("mouseover", function(e){
		if($(this).find('ul li').length > 0){
			overlay_open(this);
		}
	});
	$('div#client_overlay li.overlay_content').bind("mouseout", function(e){
		overlay_close();
	});
	
	// Open overlay menu
    function overlay_open(args){
		$(args).find('.overlay_menu').css('display', 'block');
    }
	// Open overlay menu
    function overlay_close(){
		$('div#client_overlay li.overlay_content .overlay_menu').css('display', 'none');
    }
	
	/* --------------------------- */
    /* INFINITE CAROUSEL FUNCTIONS */
    /* --------------------------- */
	
	$('.infiniteCarousel').infiniteCarousel();
	
});
$.fn.infiniteCarousel = function () {

    function repeat(str, num) {
        return new Array( num + 1 ).join( str );
    }
  
    return this.each(function () {
        var $wrapper = $('> div', this).css('overflow', 'hidden'),
            $slider = $wrapper.find('> ul'),
            $items = $slider.find('> li'),
            $single = $items.filter(':first'),
            
            singleWidth = $single.outerWidth(), 
			visible = 1;
            //visible = Math.ceil($wrapper.innerWidth() / singleWidth), // note: doesn't include padding or border
            currentPage = 1,
            pages = Math.ceil($items.length / visible);                        


        // 1. Pad so that 'visible' number will always be seen, otherwise create empty items
        if (($items.length % visible) != 0) {
            $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
            $items = $slider.find('> li');
        }

        // 2. Top and tail the list with 'visible' number of items, top has the last section, and tail has the first
        $items.filter(':first').before($items.slice(- visible).clone().addClass('cloned'));
        $items.filter(':last').after($items.slice(0, visible+2).clone().addClass('cloned'));
        $items = $slider.find('> li'); // reselect
        
        // 3. Set the left position to the first 'real' item
        $wrapper.scrollLeft(singleWidth * visible);
        
        // 4. paging function
        function gotoPage(page) {
			//alert(page);
            var dir = page < currentPage ? -1 : 1,
                n = Math.abs(currentPage - page),
                left = singleWidth * dir * visible * n;
            
            $wrapper.filter(':not(:animated)').animate({
                scrollLeft : '+=' + left
            }, 500, function () {
                if (page == 0) {
                    $wrapper.scrollLeft(singleWidth * visible * pages);
                    page = pages;
                } else if (page > pages) {
                    $wrapper.scrollLeft(singleWidth * visible);
                    // reset back to start position
                    page = 1;
                } 

                currentPage = page;
            });                
            
            return false;
        }
        
        $wrapper.after('<a class="arrow back">&lt;</a><a class="arrow forward">&gt;</a>');
         
        // 5. Bind to the forward and back buttons
        $('a.back', this).click(function () {
            return gotoPage(currentPage - 1);                
        });
        $('.showcase_thumb',this).click(function(event){
			var id = $slider.find('> li').index(this.parentNode);
		   	//alert(id);
		});
		
        $('a.forward', this).click(function () {
            return gotoPage(currentPage + 1);
        });
        // create a public interface to move to a specific page
        $(this).bind('goto', function (event, page) {
			gotoPage(page);
        });
    });  
};
