// JQUERY CUSTOM COMMANDS
$(function() {
	
	// mainMenu (temp)
	$('#mainMenu a').click(function() {
		$('#mainMenu li.active').removeClass('active');
		$(this).parent().addClass('active');
		//return false;
	});
	// Layout manipulation (mostly IE)
	$('.ie7 .paging li.item a, .ie8 .paging li.item a').append('<span><\/span>');
	$('.corners, .ie7 .b2bProducts .info, .ie8 .b2bProducts .info, .ie7 .rounded, .ie8 .rounded').append('<div class="nw"><\/div><div class="ne"><\/div><div class="se"><\/div><div class="sw"><\/div>');
	$('.ie7 .listerProducts li.product:first-child').addClass('product-first');
	
	// Fully clickable link
	$('#headerCart, #headerService, .newsBlocks li, div.kpExperts div.expert, ul.listerProducts li.product').click(function(event) {
		if ($(event.target).is(":not('a, input, label')") ) {
			window.location = $(this).find('.head a').attr("href");
		}
	});
	
	// Fully clickable inputs
	$('.element').click(function(event) {
		if ($(event.target).is(":not('a, input, label')") ) {
			$('input:radio', this).attr('checked', 'checked');
		    $('input:radio', this).trigger('click');
		}
	});
	
	// FORMS
	// Focus
	$('input.textfield, textarea.textarea').focus(function() {
		$(this).parent().addClass('focus');
	});
	// Clicking on hint
	$('div.hint').not('div.disabled div.hint').click(function() {
		$(this).parent().addClass('focus').find('input').focus();
	});
	// Clicking on field
	$('div.field, div.area').not('.disabled').click(function() {
		$(this).addClass('focus').find('input').focus();
	});
	$('input.textfield, textarea.textarea').each(function() {
		// Empty filled fields on blur
		$(this).blur(function() {
			$(this).parent().removeClass('focus');
			if ($(this).val() != "") {
				$(this).parent().addClass('filled');
			} else {
				$(this).parent().removeClass('filled');
			};
		});
		// Empty filled fields on load
		if ($(this).val() != "") {
			$(this).parent().addClass('filled');
		} else {
			$(this).parent().removeClass('filled');
		};
	});
	
	/* disabled */
	$('a.disabled').click(function(){
		return false;
	});
	
	// CUSTOM SELECT
	// Show on click
	$("div.customSelect div.selected").click(function() {
		$(this).parents('div.customSelect').addClass("customSelectDown").parents("div.selector").css({ position: "relative", zIndex: "1" }); return false;
	});
	// Hide on mouseout
	$("div.customSelect").mouseleave(function() {
		$(this).removeClass("customSelectDown").parents("div.selector").css({ position: "static", zIndex: "0" });;
	});
		
	// FANCYBOX RESIZE (1/2)
	$('#page_iframe').each(function() {
		fancyResize();
	});

}); // end function

jQuery(window).load(function () {
	// EQUALHEIGHTS within row (1/2)
	equalize();
});

// EQUALHEIGHTS within row (2/2)
function equalize() {
	var currentTallest = 0, currentRowStart = 0, rowDivs = new Array(), $el, offsetTop = 0;
	setTimeout(function(){
		$('.eq').each(function() {
			$el = $(this);
			offset = $(this).offset();
			offsetTop = offset.top;
			//alert(offsetTop);
			if (currentRowStart != offsetTop) {
				// we just came to a new row.  Set all the heights on the completed row
				for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
					rowDivs[currentDiv].css('min-height' , currentTallest);
				}
				// set the variables for the new row
				rowDivs.length = 0; // empty the array
				currentRowStart = offsetTop;
				currentTallest = $el.height();
				rowDivs.push($el);
			} else {
				// another div on the current row.  Add it to the list and check if it's taller
				rowDivs.push($el);
				currentTallest = Math.max(currentTallest, $el.height());
			}
				// do the last row
				for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
				rowDivs[currentDiv].css('min-height' , currentTallest);
			}
		});
	}, 300);
}

// FANCYBOX RESIZE (2/2)
function fb_resize(w, h) {
  if (w > 0 || h > 0) {
    if (w > 0) $('#fancybox-content').css({ width: w+"px"}); // also resize inner div for inline fancybox
    if (h > 0) $('#fancybox-content').css({ height: h+"px"});
    $.fancybox.resize();
  }
}
function fb_close_any() {
  window.top.eval('$.fancybox.close()');
}
function fb_resize_any(w, h) {
  w = (w > 0) ? w : 0;
  h = (h > 0) ? h : 0;
  window.top.eval('fb_resize('+w+','+h+')');
}
// Set maxHeight or contentHeight to fancybox
function fancyResize() {
	setTimeout(function() {
		var windowHeight = ($(window.parent).height() * 0.8);
		var contentHeight = ($('body').height() + 20);
		if (windowHeight > contentHeight) {
			var frameHeight = contentHeight;
		} else {
			var frameHeight = windowHeight;
		}
		var frameHeight = Math.floor(frameHeight);
		//alert(frameHeight);
		fb_resize_any('auto',frameHeight);
	}, 500);
}


// Resize mainframe
function resizeMainframe() {
	var contentHeight = $('#content').height();
	var mainHeight = $('#main').height();
	if ( contentHeight > mainHeight ) {
		$('#main .mainFrame .fM').height(contentHeight - 56);
	}
}
