/**
 * this function is for simulation the bind() function like in mootools 
 */ 
jQuery.extend({
	  shove: function(fn, object) {
	    return function() {
	      return fn.apply(object, arguments);
	    };
	  }
});

/**
 * show ajax-tabs -> not visible for non-js-users
 */
jQuery(document).ready(function () {
	jQuery('.ajaxload').css('display','block');
});

/**
 * clear tabs with no content
 */
function jquery_nfcrgtabs_clearNonContentTabs(headerSelect,contentSelect) {
	var headers = jQuery(headerSelect);
	var contents = jQuery(contentSelect);
	jQuery(contents).each(function (index) {
		if(jQuery.trim(jQuery(this).html()) == '') {
			jQuery(headers[index]).remove();
			jQuery(contents[index]).remove();
		}
	});
}

/**
 * this function hides all Tabs for nfcrgtabs
 */
function jquery_nfcrgtabs_hideAllTabElements(headerSelect,contentSelect,activeClass) {
	var headers = jQuery(headerSelect);
	var contents = jQuery(contentSelect);
	jQuery(contents).each(function (index) {
		jQuery(this).hide();
	});
	jQuery(headers).each(function (index) {
		jQuery(this).removeClass(activeClass);
	});
}

/**
 * this function shows tab with index i for nfcrgtabs
 */
function jquery_nfcrgtabs_showTabByIndex(headerSelect,contentSelect,activeClass,i) {
	var headers = jQuery(headerSelect);
	var header = (jQuery(headers).length > i) ? jQuery(headers[i]) : jQuery(headers[0]);
	var contents = jQuery(contentSelect);
	var content = (jQuery(contents).length > i) ? jQuery(contents[i]) : jQuery(contents[0]);
	jQuery(header).addClass(activeClass);
	jQuery(content).show();
	
	//ajaxload contents
	if(jQuery(header).hasClass('ajaxload')) {
		jQuery(header).removeClass('ajaxload');
		var headerLink = jQuery(header).children('a').attr('href');
		try {
			jQuery(content).load(headerLink);
		} catch(e) { /* nothing */ }
	}
}

/***jQuery initialization***/
google.setOnLoadCallback(function() {
	/* jQuery - Broken Image Handling */
	(function($) {
		$(document).ready(function(){
			if($('#product-detail-rgtabs')) {
				var headerSelect = '.tab:visible';
				var contentSelect = '.rgtabs-container';
				var activeClass = 'rgtabs-selected';

				jquery_nfcrgtabs_clearNonContentTabs(
					headerSelect,
					contentSelect);

				jquery_nfcrgtabs_hideAllTabElements(
					headerSelect,
					contentSelect,
					activeClass);

				jquery_nfcrgtabs_showTabByIndex(
					headerSelect,
					contentSelect,
					activeClass,
					0);

				var headers = jQuery(headerSelect);
				var contents = jQuery(contentSelect);

				jQuery(headers).each(jQuery.shove(function(index, domElement) {
					jQuery(domElement).click(jQuery.shove(function () {
						jquery_nfcrgtabs_hideAllTabElements(
							this.headerSelect,
							this.contentSelect,
							this.activeClass);
						jquery_nfcrgtabs_showTabByIndex(
								this.headerSelect,
								this.contentSelect,
								this.activeClass,
								this.index);
					},{index:index,
					contents:this.contents,
					headerSelect:this.headerSelect,
					contentSelect:this.contentSelect,
					activeClass:this.activeClass}));
				},{contents:contents,
				headerSelect:headerSelect,
				contentSelect:contentSelect,
				activeClass:activeClass}));
			}
		});
	})(jQuery);
});
