
//////////////////////////////
$(document).ready(function() {
//////////////////////////////

	// We put corners where needed
	$('.corner').corner();

	// Some eye candy for search form
	$('.leftcol .search form input[type=text]')
		.focus(function() {
			$(this).toggleClass('with_instructions', false);
		})
		.blur(function() {
			if(this.value == '')	$(this).toggleClass('with_instructions', true);
			else					$(this).toggleClass('with_instructions', false);
		});

	// Expanding/collapsing for left navbar LEVEL 1
	$('.leftnav .expander').click(function() {
		if($(this).attr('not_expandable') != 'true') {
			if($(this).hasClass('expanded')) {
				$(this)
					.removeClass('expanded')
					.addClass('collapsed')
					.closest('div').find('.first_level').hide('fast');
			} else {
				$(this)
					.removeClass('collapsed')
					.addClass('expanded')
					.closest('div').find('.first_level').show('fast');
			} // end if
			return true;
		} // end if
	});

	// Expanding/collapsing for left navbar LEVEL 2
	$('.leftnav .l1 .caption').click(function() {
		if($(this).attr('not_expandable') != 'true') {
			if($(this).hasClass('expanded')) {
				$(this)
					.removeClass('expanded')
					.addClass('collapsed')
					.closest('.l1').find('.content').hide('fast');
			} else {
				$(this)
					.removeClass('collapsed')
					.addClass('expanded')
					.closest('.l1').find('.content').show('fast');
			} // end if
			return true;
		} else {
			var link = $(this).find('a').attr('href');
			top.location.href = link;
		} // end if
	});

	// Link fixing for navbar buttons, last level
	$('.leftnav .l2').each(function() {
		var link = $(this).find('a').attr('href');
		$(this).click(function() { top.location.href = link; });
	}); // end each

	// Topnav product support rolldown menu fixing for hovers
	$('.header .product_support_menu a').each(function() {
		var link = $(this).attr('href');
		$(this).closest('div').click(function() { top.location.href = link; });
	}); // end each

	// Topnav product support rolldown menu functionality
	$('.header .topnav .product_support, .header .topnav .product_support_active').hover(function() { $('.header .product_support_menu').show(); });
	$('.header .topnav img').hover(function() { }, function() { $('.header .product_support_menu').hide(); });
	$('.header .product_support_menu').hover(function() {
		$('.header .topnav .product_support').removeClass('product_support').addClass('product_support_active');
		$(this).show();
	}, function() { // hout
		if(!$('.header .topnav .product_support_active').hasClass('is_active')) {
			$('.header .topnav .product_support_active').removeClass('product_support_active').addClass('product_support');
		} // end if
		$(this).hide();
	}); // end hover

	// CSS corrections for compatibility with IE7
	$('.rightcol .gray_segment *:last-child')
		.css('padding-bottom', '0')
		.css('margin-bottom',  '0');

}); // end document.ready function

///////////////////////////////////////////////////////////////////////////////////////////////
var te_width, te_new_width, te_height, te_new_height, te_left, te_new_left, te_top, te_new_top;
///////////////////////////////////////////////////////////////////////////////////////////////
function thumbnail_enlarger(params) {
/////////////////////////////////////
	var item_selector	   = params.item_selector;
	var source_attribute   = params.source_attribute;
	var large_size_ratio   = params.large_size_ratio;
	var include_link       = params.include_link;
	var traslucent_border  = params.traslucent_border? true: false;
	$(document).ready(function() {
		$(item_selector).hover(function() {
			// $('.ghost_image').remove();
			// Let's put a flag on it so it doesn't double-renders
			if($(this).data('hovered') != true) {
				$(this).data('hovered', true);
				// Let's get the origin's coords
				var src    = $(this).attr(source_attribute);
				te_width   = $(this).width();
				te_height  = $(this).height();
				var offset = $(this).offset();
				te_left    = offset.left;
				te_top     = offset.top;
				var href   = $(this).closest('a').attr('href');

				// Let's calculate the origin's aspect ratio
				var aspect = te_height / te_width;

				// Let's enlarge the image dimensions
				te_new_width  = Math.round(te_width * large_size_ratio);
				te_new_height = Math.round(te_new_width * aspect);

				// Let's position the enlarged image
				te_new_left = te_left - Math.round((te_new_width  - te_width) / 2) - 5;
				te_new_top  = te_top  - Math.round((te_new_height - te_height) / 2) - 5;

				// This is to compensate over the border width
				te_left = te_left - 5; te_top = te_top - 5;

				// Let's add the new image on top of body
				var img_classtag = '', new_img_tag = '', aopen = '', aclose = '', divopen = '', divclose = '', img_styles = '', img_width, img_height;
				if(include_link) {
					aopen = '<a href="' + href + '">'; aclose = '</a>';
				} // end if
				if(traslucent_border) {
					divopen      = '<div class="ghost_image traslucent_border" style="' +
						                'position: absolute; display: none; z-index: 100; ' + 
								        'width: ' + (te_width + 20) + '; height: ' + (te_height + 20) + '; ' +
										'background-color: transparent;' +
								   '">';
					divclose     = '</div>'
					img_width    = te_new_width - 20;
					img_height   = te_new_height - 20;
					img_styles   = 'border: none; margin-left: 10px; margin-top: 10px;';
				} else {
					img_classtag = 'class="ghost_image"';
					img_width    = te_width;
					img_height   = te_height;
					img_styles   = 'border: 10px solid White; position: absolute; display: none; z-index: 100;';
				} // end if
				new_img_tag = divopen + aopen
					        + '<img ' + img_classtag + ' width="' + img_width + '" heigth="' + img_height + '" src="' + src + '" ' +
								   'style="' + img_styles + ' width: ' + img_width + '; height: ' + img_height + ';">'
							+ aclose + divclose;
				$('body').prepend(new_img_tag);
				$('.ghost_image')
					.css('left', te_left)
					.css('top', te_top)
					.hover(function() { }, function() { // hout
						// On rolling out, we hide and self-destroy.
						// But first, we'll remove the 'hovered' state from the triggered hover element
						$(item_selector).each(function() { $(this).data('hovered', null); });
						$(this).animate({left: te_left, top: te_top, width: te_width, heigth: te_height, opacity: 0.1}, 100, 'swing', function() { $(this).remove(); });
					})
					// Note: at the end of the animation we re-set the height of the ghost image for compatibility with IE7.
					.animate({left: te_new_left, top: te_new_top, width: te_new_width, heigth: te_new_height, opacity: 1}, 100, 'swing', function() { $(this).css('height', te_new_height); });
			} // end if
		});
	}); // end feature product enlarger
} // end function

