/**
 * Create a shortcut to jQuery, calling no conflict due to our custom $ function
 * {@link http://docs.jquery.com/Using_jQuery_with_Other_Libraries}
 */
var $j = jQuery.noConflict();


jQuery(document).ready( function () {
	jQuery('#list_menu_6').dropDownMenu({timer: 1500, parentMO: 'parent', childMO: 'child'});
		
	jQuery("#page-search-form #search_query").focus( function () {
        jQuery(this).select();
    });
	
	if (jQuery(".node-product").attr('id') != undefined) {
		jQuery(".node-product #nbw-leftcolumn .product_image").each( function () {
			jQuery(this).click( function () {
				var src = jQuery(this).attr('src');
				jQuery("#product_full_image").fadeOut('fast', function () {
					jQuery(this).attr('src', src);
				}).fadeIn();
			});
		});
	} 
});

/**
 * Function used to show our search results
 * @param int start
 * @param int end
 */
function show_search_results(start, end) {
	var str_html = '';
	//var siteurl = get_abs_path(site_prefix);
	var siteurl = '';
	
	for (var i = start; i < end; i++) {
		str_html += '<div class="box">';
		str_html += '<img class="thumbnail float-left" src="' + siteurl + 'content/products/' + results[i].productImage + '" alt="' + results[i].productName + '" />';
		str_html += '<div class="box-text float-left">';
		str_html += '<h3>' + results[i].productName + '</h3>';
		str_html += '<h4>' + results[i].subtitle + '</h3>';
		if (results[i].productDescription != results[i].productName) {
			str_html += '<p>' + results[i].productDescription + '</p>';
		}
		str_html += '<p><a href="' + siteurl + results[i].filename + '">View Product Page</a></p>';
		str_html += '</div>';
		str_html += '<div class="clear"></div>';
		str_html += '</div>';
	}
	
	document.getElementById('search-contents').innerHTML = str_html;
}


/*
 * Multi-level Drop Down Menu 3.0
 * April 17, 2010
 * Corey Hart @ http://www.codenothing.com
 */ 
(function( $, window, undefined ){
    // Needed for IE Compatibility (Closing menus must be done backwards in IE)
    // Ensure that no complications arise from other libraries modifying the 
    // array functionality (and hope that they store the old reverse function into _reverse)
    var el, a = Array.prototype, Reverse = a._reverse || a.reverse;

    // bgiframe is needed to fix z-index problem for IE6 users.
    // For applications that don't have bgiframe plugin installed, create a useless 
    // function that doesn't break the chain
    function emptyfn(){
        return this;
    }

    // Cache common event functions so they aren't instantiated with each event
    function clearSiblings(){
        $( el = this ).children('a').removeClass( $.data( el.parentNode , 'multi-ddm-classname' ) );
    }
    function oldMenus(){
        $( el = this ).hide().siblings('a').removeClass( $.data( el.parentNode.parentNode , 'multi-ddm-classname' ) );
    }

    
    // Expose the drop down menu
    $.fn.dropDownMenu = function( options ) {
        return this.each(function(){
            // Defaults with metadata support
            var $main = $(this), i = 0, $menu, timeout,
                settings = $.extend({
                    timer: 500,
                    parentMO: undefined,
                    childMO: undefined,
                    bgiframe: undefined,
                    levels: []
                }, options || {}, $.metadata ? $main.metadata() : {}),

                // Check on every initiation, so bgiframe can be loaded after this plugin
                bgiframe = $.fn.bgiframe || $.fn.bgIframe || emptyfn;

            // Loop through each level, attach the bgiframe and store it's classname
            $menu = $main.data( 'multi-ddm-classname', settings.levels[ 0 ] || settings.parentMO || settings.childMO || '' );
            while ( $menu.length > 0 ) {
                $menu = bgiframe.call(
                    $menu.find('> li > ul').data( 'multi-ddm-classname', settings.levels[ ++i ] || settings.childMO || '' ),
                    settings.bgiframe
                );
            }

            // Use event delegation to track mouse movement across the menu
            $main.delegate( 'li', 'mouseenter.multi-ddm', function(){
                var self = $( el = this );

                if ( timeout ) {
                    clearTimeout( timeout );
                }

                // Close old menus and remove hover of non-menus
                Reverse.call( self.siblings('li').find('ul:visible') ).each( oldMenus ).end().each( clearSiblings );

                // Open new menu and remove any lingering hover elements
                self.children('a').addClass( $.data( el.parentNode, 'multi-ddm-classname' ) ).siblings('ul').fadeIn()
                    .children('li').each( clearSiblings );
            })
            .bind( 'mouseleave.multi-ddm', function(){
                timeout = setTimeout( closemenu, settings.timer );
            });
    
            // Closes all open menus
            function closemenu(){
                // Clear mouseovers
                $main.find('li').each( clearSiblings );

                // Close Menus backwards for IE Compatibility
                Reverse.call( $main.find('ul:visible') ).fadeOut();

                if ( timeout ) {
                    clearTimeout( timeout );
                }
            }
            
            // Allows user option to close menus by clicking outside the menu on the body
            $( window.document ).bind( 'click.multi-ddm', closemenu );
        });
    };
})( jQuery, window || this );



// enable cycle.js where needed
// loads cycle.js
$j(document).ready(function(){
	if( $j(".cycle").length ){
		$LAB.script(HLI_globals.coreURL + "js/lib/jquery.cycle.all.min.js").wait( function(){
																								 
			$j(".cycle").each( function(index){
				
				if( $j(this).is('[class*="cycle-theme"]') ){
					var classes = $j(this).attr("class").split(" ");
					var i;
					for(i=0;i<classes.length;i++){
						var tst = /cycle-theme/i.test(classes[i]);
						if(	tst ){ 
							$j(this).wrap('<div id="cycle-wrap-' + index + '" class="cycle-wrap ' + classes[i] + '"/>'); 
							break; 
						}
					}
				}else{
					$j(this).wrap('<div id="cycle-wrap-' + index + '" class="cycle-wrap cycle-theme-default"/>');
				}
				$pause = "&nbsp;";
				if( $j(this).hasClass("w-pause") ){
					$pause = $j('<a class="cycle-pause" id="cycle-pause-' + index + '"><span>pause</span></a>').toggle( 
										  	function(e){
												$j(e.target).text("resume").parent('a').addClass('paused').closest('.cycle-wrap').children('.cycle').cycle('pause');
											},
										  	function(e){
												$j(e.target).text("pause").parent('a').removeClass('paused').closest('.cycle-wrap').children('.cycle').cycle('resume',true);
											});
				}
				if( $j(this).hasClass("w-controls") ){
					$prev = $j('<a class="cycle-prev" id="cycle-prev-' + index + '"><span>prev</span></a>');
					$next = $j('<a class="cycle-next" id="cycle-next-' + index + '"><span>next</span></a>');
					$cntrl = $j('<div class="cycle-nav" id="cycle-nav-' + index + '"></div>').append($prev,$pause,$next);
					$j(this).after($cntrl);	
				};
				if( $j(this).hasClass("w-pager") ){
					$j(this).after('<ul id="cycle-pager-' + index + '" class="cycle-pager"></ul>');	
				};
				
				$j(this).cycle({
					next: "#cycle-next-"+index,
					prev: "#cycle-prev-"+index,
					pager: "#cycle-pager-"+index,
					pagerAnchorBuilder: function(idx, slide) {
											return '<li><a href="#"><span>'+(idx+1)+'</span></a></li>';
										}

				});
			
			});
		});
	}
});




