// Auxiliary code for the drop down menu: 
function toggleMenu(el, over) {
	if (over) { jQuery(el).addClass('over'); } 
	else { jQuery(el).removeClass('over'); } 
}

function FixHeb(Str) {
	// Str = Str.replace( '×' , 'X' );
	Str = Str.replace( /(\([\d]+[\s]*[x|X|×][\s]*[\d]+([\s]*[x|X|×][\s]*[\d]+)?([\s]*[a-z]+)?\))/igm , '$1<span class="no">ג</span>' );
	Str = Str.replace( /(([\d]+[\s]*[x|X|×][\s]*[\d]+([\s]*[x|X|×][\s]*[\d]+)?)(([\s]*)([a-z]+))?)/igm , '$6$5$2' );
	/***/
	//Str = Str.replace( /(MB|Kbps|GB|dpi|Mhz|K)([\d]+[\.]?[\/]?[\d]*[\/]?[\d]*)/igm , '$2$1' );
	Str = Str.replace( /(MB|Kbps|GB|dpi|Mhz|K)([\d]+[\.]?[\/|x|X|×]?[\d]*[\/|x|X|×]?[\d]*)/igm , '$2$1' );
	/***/
	Str = Str.replace( /(\([a-z\"\-]+\))/igm , '$1<span class="no">f</span>' );
	Str = Str.replace( /(A[1-9][a-z0-9\-]*)/igm , '$1<span class="no">פ</span>' );
	Str = Str.replace( /([\d]+[\s]+\(.\))/igm , '<span class="no">ע</span>$1' );
	/***/
	// Str = Str.replace( /([\d]+)[\s]+((MB|Kbps|GB|dpi|Mhz|K)[\.]?(?!\D))/igm , '$1<span class="no">v</span>$2' );
	// Str = Str.replace( /([\d]+)(&nbsp;)*([\s]+)((MB|Kbps|GB|dpi|Mhz|K))/igm , '$1<span class="no">v</span>$4' );
	Str = Str.replace( /([\d]+)(&nbsp;)*([\s]+)((MB|Kbps|GB|dpi|Mhz|K))/igm , '$1$4' );
	// Str = Str.replace( /([\d]+[\s]*[x|X][\s]*[\d]+([\s]*[x|X][\s]*[\d]+)?)(&nbsp;)*([\s]+)((MB|Kbps|GB|dpi|Mhz|K))/igm , '$1<span class="no">v</span>$5' );
	// Str = Str.replace( /([\d]+[\s]*[x|X|×][\s]*[\d]+([\s]*[x|X|×][\s]*[\d]+)?)(&nbsp;)*([\s]+)(MB|Kbps|GB|dpi|Mhz|K)/igm , '<span class="no">ת</span>$5 $1' );
	/***/
	Str = Str.replace( /(\()/ , '<span class="no">כ</span>$1');
	Str = Str.replace( /(\))/ , '$1<span class="no">ז</span>');
	/***/
	Str = Str.replace( /(([\s]+)|(\>))([\d]+)/igm , '$1<span class="no">ק</span>$4' );
	// Str = Str.replace( /(([\s]+)|(\>))([\d]+(?!\D))/igm , '$1<span class="no">ק</span>$4' );
	Str = Str.replace( /(^[\d]+)/igm , '<span class="no">ש</span>$1' );
	
	return Str;
}

jQuery().ready(function() {

	// Initialize Drop Down Menu: 
	jQuery('ul.superfish').superfish();

	// Open all external links in new window: 
	// $('a').not('a[href^=<?php bloginfo('url'); ?>]').not('a[href^=#]').not('a[href^=/]').attr('target','_blank');
	
	// Color Side Menu in different shades of blue: 
	for(index=0;index<8;index++) {
		$('#nav_menu-4 >div > ul > li:eq('+index+')').addClass('blue_'+index);
	}

	// Expand and collapse inner categories of side menu: 
	$('.widget_nav_menu li .sub-menu').hide();
	$('.widget_nav_menu li a').click(function() {
		if ($(this).next('ul').length) {
			$(this).next('ul').toggle('slow');
			return false;
		}
	});

	// Fix Hebrew in Paragraphs:
	$('.hentry > p').each(function() {
		// $(this).html(FixHeb($(this).html()));
	});
	
	// Initialize product tables, and add contact button at bottom if missing: 
	if (jQuery('.parent-pageid-35, .parent-pageid-37, .parent-pageid-41, .parent-pageid-43, .parent-pageid-45, .parent-pageid-47, .parent-pageid-49, .parent-pageid-51').length) { 
		$('table', '#inner_stage').attr('cellpadding', '0').attr('cellspacing', '0').attr('border', '0').addClass('specs');
		if ($('.contact_button').length) {
			$('.contact_button').remove();
		}		
		var Subject = encodeURI($('.hentry > h2 span').text());
		$('.hentry').append("<p><a class='contact_button' href='mailto:info@gestetnertec.co.il?subject=\""+Subject+"\"'></a></p>");
	}
	
	// Delete empty/unused lines: 
	$('.specs tr, #compare_table tr').each(function(jIndex, Row) {
		// var RowContent = jQuery.trim($(this).text()); 
		var RowContent = $(Row).text().replace(/(\r\n|\n|\r|\s)/gm,''); // alert(RowContent);
		if (!(RowContent.length) || RowContent=='מאפייןערך') { $(Row).remove(); }
	});

	// Fix Hebrew/English conjuctions: 
	jQuery('.specs td').each(function(sIndex, Cell) {
		var Content = jQuery(Cell).html();
		if ( (Content.search('Base T') == '-1') && (Content.search('Server 2003') == '-1') && (Content.search('Win 2000') == '-1') && (Content.search('#fff') == '-1') ) {
			jQuery(Cell).html(FixHeb(Content));
		}

		// In addition, use the loop to make sure all cells span only one column at this point: 
		$(Cell).attr('colspan', '1');
	});
	
	// Exapnd cell over two columns if neccessary: 
	jQuery('.specs tr').each(function() { 
		jQuery('td:eq(2)', jQuery(this)).remove(); // Remove third column
		if (!(jQuery('td:eq(1)', jQuery(this)).text().replace(/(\r\n|\n|\r|\s)/gm,''))) { 
			jQuery('td:eq(1)', jQuery(this)).remove();
			// jQuery('td:eq(0)', jQuery(this)).attr('colspan', '2');
		}

		jQuery('td:last-child', this).removeAttr('width');

		if (!(jQuery('td:eq(1)', jQuery(this)).length)) {
			jQuery('td:eq(0)', jQuery(this)).attr('colspan', '2');
		} else {
			// Set width of right column in product tables: 
			jQuery('td:first-child', this).width(168).attr('width', '168');
		}
	});
	
	// Style headers inside table: 
	$('.specs td strong').parent().css('padding-top', '15px').next('td').css('padding-top', '15px');

	// Hide line breaks inside VAR elements: 
	$('var').next('br').hide();
	
	// Add an empty column for padding at right side of table: 
	var i = 0;
	$('#compare_table tr').each(function() {
		if (i) $(this).prepend('<td class="table_filler"></td>');
		else $(this).prepend('<th class="table_filler"></th>');
		i++;
	});

	// Change height of thumbnail image: 
	$('.image_padding').height(function() {
		var height = $(this).parent().height();
		/*
		var height = 0;
		$(this).parent().children().not('.image_padding').each(function(index, El) {
			height = height + $(El).height();
		});
		*/
		
		var SelfHeight = $(this).height();

		if ($('table' ,$(this).parent() ).not('.specs').length) {
			var TempHeight1 = 0;
			 $('table' ,$(this).parent()).not('.specs').prevAll().each(function(index, El) {
				TempHeight1 = TempHeight1 + $(El).height();
			 });
			 if (TempHeight1 < height) height = TempHeight1;
		}

		if ( $('.full' ,$(this).parent() ).length ) {
			var TempHeight2 = 0;
			$('.full' ,$(this).parent() ).prevAll().each(function(index, El) { 
				TempHeight2 = TempHeight2 + $(El).height();
			});
			if (TempHeight2 < height) height = TempHeight2;
		}

		var Adjust = height-(SelfHeight);
		if (Adjust <= 0) { Adjust = 0; }
		else { 
			if ($('.specs').length) {
				SpecsHeight = $('.specs').height();
				Adjust = (Adjust-SpecsHeight)/601*760 + SpecsHeight;
			} else Adjust = Adjust/601*760; 
		}
		height = Adjust + SelfHeight;
		if (height<150) height=150;
		return height;
	});

});

