$(function() {
	$('.field-holder').live('click', function() {
		$(this).find('input').focus();
	});
	
	 $('.field').focus(function() {
        if(this.title==this.value) {
            this.value = '';
        }
    }).blur(function(){
        if(this.value=='') {
            this.value = this.title;
        }
    });
        
    $('.product-details .tabs .nav a').live('click', function() {
    	var href = $(this).attr('href');
    	var tabs = $(this).parents('.tabs:eq(0)').find('.tab');
    	tabs.not($(href)).hide();
    	$(href).show();
    	$('.product-details .tabs .nav a').not($(this)).removeClass('active');
    	$(this).addClass('active');
    	return false;
    });
    
    $('a[rel*=facebox]').facebox();
    
    $(document).bind('reveal.facebox', function() {	
    	//pageLoaded();
    });
    
    $('#facebox .close').live('click', function() {
    	$(document).trigger('close.facebox');
    	return false;
    });
    
    $('.topbar .topnav li, .productsnav .nav li, .subnav ul li').hover(function() {
    	$('select').blur();
    	if ($(this).find('.dropdown').length) {
    		$(this).addClass('hover');
    		$(this).find('.dropdown').css('visibility', 'visible');
    	};
    }, function() {
    	if ($(this).find('.dropdown').length) {
    		$(this).removeClass('hover');
    		$(this).find('.dropdown').css('visibility', 'hidden');
    	};
    });
    
    $('#teams-dropdown ul').jcarousel({
    	scroll : 1,
    	wrap : 'both',
    	initCallback : teams_dropdown_callback
    });
    
    function teams_dropdown_callback(carousel) {
    	$('#teams-dropdown .slider .btns a').live('click', function() {
    		if ($(this).hasClass('slide-prev')) {
    			carousel.prev();
        		return false;
    		} else {
    			carousel.next();
        		return false;
    		};
    	})
    }
    
    $('.ver-slider').jcarousel({
    	vertical : true,
    	scroll : 10,
    	wrap : 'both',
    	initCallback : vertical_slider_callback
    });
    
    function vertical_slider_callback(carousel) {
    	carousel.container.find('.btns a').live('click', function() {
    		if ($(this).hasClass('slide-up')) {
    			carousel.prev();
        		return false;
    		} else {
    			carousel.next();
        		return false;
    		};
    	});
    }
    
    $('.content-scroller ul, .profiles-slider ul').jcarousel({
    	scroll : 1,
    	wrap : 'both',
    	initCallback : contentScrollerCallback
    });
    
    function contentScrollerCallback(carousel) {
    	
    }
});
function pageLoaded() {
	/* #frame-labels related lines moved to popup_tweaks() in steiner.js so it can be called via AJAX */
	
	$('.topnav > li > a, .productsnav > ul > li > a, .main-btn, .sub-btn, .grey-btn, .mid-btn').each(function() {
		if ($(this).find('span').length) {
			return;
		};
		var text = $(this).html();
		$(this).html('').append('<span></span>').find('span').html(text);
	});
	
	$('.smlink').each(function() {
		if ($(this).parent().is('p') || $(this).hasClass('nojs')) {
			return;
		} else {
			$(this).wrap('<p></p>').removeClass('smlink').parent().addClass('smlink');
		};
	});
	
	$('ul.fix-last, .subnav > ul').each(function() {
		$(this).find('li:last-child').addClass('last');
	});
	
	$('.topnav li').each(function() {
		if ($(this).find('.dropdown').length) {
			$(this).addClass('has-dropdown');
		};
	});
	
	$('.round, .wbox').each(function() {
		function include_html(elem) {
			var text = $(elem).html();
			$(elem).html('').append('<div class="tb"><div class="rb"><div class="bb"><div class="lb"><div class="tlc"><div class="trc"><div class="brc"><div class="blc"><div class="tlc"><div class="cnt"></div></div></div></div></div></div></div></div></div></div>').find('.cnt').html(text);
		};
		
		var inner_rounds = $(this).find('.round, .wbox');
		
		if (inner_rounds) {
			inner_rounds.each(function() {
				include_html($(this));
			});
		};
		include_html($(this));
	});
	
	/*
	 * This piece of code only gets executed if the browser is Internet explorer and is version 7. 
	 * This code fixes the following issue in a HACK
	 * â€œIn Internet Explorer positioned elements generate a new stacking context, 
	 * starting with a z-index value of 0. Therefore z-index doesnâ€™t work correctlyâ€?
	 * 
	 * By assigning DOM elements descending z-indexes seems to fix the problem. 
	 * 
	 * Is there a better way?
	 */
	if(jQuery.browser.msie && jQuery.browser.version == '7.0')
    {
		 $('.sidenav-div div').not('cnt').each
         (
                 function()
                 {
                         $(this).css('zIndex', '1000');

                 }
         );
    }else{
    	/* This is a hack to prevent the Contact Us label from pushed up due to cufon in IE7 */
		 Cufon.replace('.black_no_ie7');
    }
	
	$('div[class*=col-]').each(function() {
		var number = parseInt($(this).attr('class').replace(/.*col-([\d]+).*/, '$1'));
		if (number < 20) {
			return;
		};
		$(this).addClass('width-col').css('width', number);
	});
	
	Cufon.replace('.black');
	
	$('.carousel').jcarousel();
	
	//$('.photo-gallery .small ul').jcarousel();
	
	$('#content').show();
}

/* Onmouseover rendering of the dropdown jcarousels to ease header load time. */
function render_header_dropdown_jcarousels(element){
   if(element.attr('rendered') == 'true'){ return; }

   element.jcarousel({
        vertical : true,
        scroll : 10,
        wrap : 'both',
        initCallback : vertical_slider_callback
    });
    
    element.attr('rendered', 'true');

	function vertical_slider_callback(carousel) {
	    carousel.container.find('.btns a').live('click', function() {
	        if ($(this).hasClass('slide-up')) {
	                carousel.prev();
	                return false;
	        } else {
	                carousel.next();
	                return false;
	        };
	    });
	}
}
