function ToggleAdvancedSearch()
{
	jQuery('#content').toggleClass('advanced');
	var form = document.forms['searchform'];
	form.elements['simple'].value = (form.elements['simple'].value == "1") ? "0" : "1";
}

function ToggleAbout()
{
	ToggleSmokeLayer();
	var oAbout = jQuery('.about');
	oAbout.toggle();
	if (oAbout.is(':visible'))
	{
		document.getElementById('content').scrollIntoView();
		oAbout.css('left', (jQuery('.main').offset().left + 188) + 'px');
		oAbout.css('top', 100 + 'px');
	}
}
function ToggleMainDescription()
{
	jQuery('#description').toggle(100);
}

function ToggleDescription(oToggle)
{
	// Get the selected description
	var SelectedDescription = jQuery(oToggle).next('.context');
	if (SelectedDescription.length==0) {
		SelectedDescription = jQuery(oToggle).parent().parent().children('.context');
	}
	// Get the visibility of the selected description
	var DescriptionVisible = SelectedDescription.is(':visible');
	
	// Hide all the descriptions
	jQuery('.context').hide(100);
	jQuery('.context').parent().css('height','auto')
	
	// Show the selected description (if not visible yet)
	if (!DescriptionVisible)
	{
		if (jQuery.browser.msie) {
			SelectedDescription.show();
			SelectedDescription.parent().css('height', SelectedDescription.height()+SelectedDescription.parent().height);
		} else {
			SelectedDescription.show(100);
		}
	}
}

function ToggleDescriptionHome(oToggle)
{
	// Get the selected description
	var SelectedDescription = jQuery(oToggle).next('.context');
	if (SelectedDescription.length==0) {
		SelectedDescription = jQuery(oToggle).parent().parent().children('.context');
	}
	// Get the visibility of the selected description
	var DescriptionVisible = SelectedDescription.is(':visible');
	
	// Hide all the descriptions
	jQuery('.context').hide(100);
	jQuery('.context').parent().css('height','auto')
	
	// Show the selected description (if not visible yet)
	if (!DescriptionVisible)
	{
		if (jQuery.browser.msie) {
			SelectedDescription.show();
			SelectedDescription.parent().css('height', SelectedDescription.height()+10);
		} else {
			SelectedDescription.show(100);
		}
	}
}

function ToggleDescriptionFilter(oToggle)
{

	// Get the selected description
	var SelectedDescription = jQuery(oToggle).parent().parent().parent().find('.context');

	// Get the visibility of the selected description
	var DescriptionVisible = SelectedDescription.is(':visible');
	
	// Hide all the descriptions
	jQuery('.context').hide(100);
	jQuery('.context').parent().css('height','auto')
	
	// Show the selected description (if not visible yet)
	if (!DescriptionVisible)
	{
		if (jQuery.browser.msie) {
			SelectedDescription.show();
			SelectedDescription.parent().css('height', SelectedDescription.height()+30);
		} else {
			SelectedDescription.show(100);
		}
	}
}

function SetZIndexes() {
	// Set the z-indexes of the div elements in the name column
	var Divs = jQuery('table').find('.name > div');
	var iCount = Divs.length;
	Divs.each(function(index) {
		jQuery(this).css('z-index', '' + (iCount - index));
	});
}

function EnlargePictures(oButton, iViewerIndex)
{
	var Pictures = jQuery('#picture');
	var SelectedIndex = -1;
	Pictures.find('img').each(function(index) {
		if (!jQuery(this).is('.hidden'))
		{
			SelectedIndex = index;
		}
	});
	ToggleSmokeLayer();
	TogglePhotoViewer(SelectedIndex, iViewerIndex);
}

function ShowLargePicture(oThumb)
{	
	var iPictureIndex = jQuery(oThumb).parent().parent().find('img').index(oThumb);
	var PicturesPlaceHolder = jQuery('#picture')
	PicturesPlaceHolder.children('img').addClass('hidden');
	jQuery(PicturesPlaceHolder.children('img').get(iPictureIndex)).removeClass('hidden');
	
	// Set the clicked thumb active
	jQuery(oThumb).parent().parent().find('div').removeClass('active');
	jQuery(oThumb).parent().toggleClass('active');
}

function ShowLargePictureInPopup(oThumb){
	var iPictureIndex = jQuery(oThumb).parent().parent().find('img').index(oThumb);
	var PicturesPlaceHolder = jQuery(oThumb).parent().parent().prev();
	PicturesPlaceHolder.children('img').addClass('hidden');
	jQuery(PicturesPlaceHolder.children('img').get(iPictureIndex)).removeClass('hidden');
	
	// Set the clicked thumb active
	jQuery(oThumb).parent().parent().find('div').removeClass('active');
	jQuery(oThumb).parent().toggleClass('active');	
}

function EnlargeMovies(oButton, iViewerIndex)
{
	ToggleSmokeLayer();
	ToggleMovieViewer(null, iViewerIndex);
}

function ShowMovie(oThumb)
{
	var iPictureIndex = jQuery(oThumb).parent().parent().find('img').index(oThumb);
	var PicturesPlaceHolder = jQuery(oThumb).parent().parent().prev();
	PicturesPlaceHolder.children('span').addClass('hidden');
	jQuery(PicturesPlaceHolder.children('span').get(iPictureIndex)).removeClass('hidden');
	
	// Set the clicked thumb active
	jQuery(oThumb).parent().parent().find('div').removeClass('active');
	jQuery(oThumb).parent().toggleClass('active');
}

/*function FilterSearchResult(oCheckBox)
{
	var filterClass = oCheckBox.id.split('_')[0];
	var oFilterContentCells = jQuery('#results').find('.' + filterClass).filter(':contains(' + oCheckBox.value + ')');
	var oFilterRows = oFilterContentCells.parent();
	
	if (oCheckBox.checked)
	{
		// Disable filter
		// Remove filter attribute on row for current filter
		oFilterRows.each(
						 function(index) {
							 jQuery(this).attr('filter', (jQuery(this).attr('filter') == null ? '' : jQuery(this).attr('filter').replace(filterClass, '')));
						 });
		// Show the cells in the rows that have no filter and are not hidden
		oFilterRows.filter('tr[@filter=]').children().not('.hidden').show();
	}
	else
	{
		// Enable filter
		// Set filter attribute on row
		oFilterRows.each(
						 function(index) {
							 jQuery(this).attr('filter', (jQuery(this).attr('filter') == null ? '' : jQuery(this).attr('filter')) + filterClass);
						 });
		// Hide the cells
		oFilterRows.children().hide();
	}
	
	if (jQuery('#results').find('td:visible').length > 0)
	{
		// Show table and compare links
		jQuery('#results').show();
		jQuery('.selectandcompare').show();
	}
	else
	{
		// Hide table and compare links
		jQuery('#results').hide();
		jQuery('.selectandcompare').hide();
	}
}*/
var comparisonActive = false;
function CompareProducts(base,dom_checkbox,new_window)
{	
	try {
		
		if(typeof dom_checkbox == 'undefined') dom_checkbox = '#results';
		if(typeof new_window == 'undefined') new_window = false;
		
		if (comparisonActive == false) {
			comparisonActive = true;
			var CheckBoxes = jQuery(dom_checkbox).parent().find('input[@name=product]');
			var Products = base;
			var Submit = false;
			var numSelected = 0;
			for (var i = 0; i < CheckBoxes.length; i++)
			{
				if (CheckBoxes[i].checked || CheckBoxes[i].type == 'hidden')
				{
					Products += CheckBoxes[i].value;
					Submit = true;
					numSelected++;
				}
			}
			if (numSelected > 12) {
				alert("Er kunnen maar 12 producten tegelijk worden vergeleken. U heeft op het moment " + numSelected + " producten geselecteerd.");
				return false;
			}
			if (Submit){
				
				if(new_window){
					window.open(Products,'','width=1040,height=600,scrollbars=yes,toolbar=yes,location=yes');
					return false;
				}else{
					 document.location.href=Products;
				}
			} 
			comparisonActive = false;
		}
	} catch (e) {
		comparisonActive = false;
	}
}

function CompareShops(base,dom_checkbox,new_window)
{
	try {
		
		if(typeof dom_checkbox == 'undefined') dom_checkbox = '#results';
		if(typeof new_window == 'undefined') new_window = false;
		
		var productId = jQuery('#span-product-id').attr('rel')
		
		if (comparisonActive == false) {
			var CheckBoxes = jQuery(dom_checkbox).find('input[@type=checkbox]');
			var Shops = base;
			var Submit = false;
			var numSelected = 0;
			for (var i = 0; i < CheckBoxes.length; i++)
			{
				if (CheckBoxes[i].checked)
				{
					Shops += CheckBoxes[i].value;
					Submit = true;
					numSelected++;
				}
			}
			if (numSelected > 12) {
				alert("Er kunnen maar 12 winkels tegelijk worden vergeleken. U heeft op het moment " + numSelected + " winkels geselecteerd.");
				return false;
			}
			
			Shops = Shops + "/p/"+productId;
			
			if (Submit){
				if(new_window){
					window.open(Shops,'','width=1040,height=600,scrollbars=yes,toolbar=yes,location=yes');
					return false;
				}else{
					 document.location.href=Shops;
				}
			}
			comparisonActive = false;
		}
	} catch (e) {
		comparisonActive = false;
	}
}

function HideToolTip(oCloseButton)
{
	jQuery(oCloseButton).parent().parent().parent().hide();
}

function ShowToolTip(oLink)
{
	oLink = jQuery(oLink);
	// Get the tooltip title
	var Title = oLink.attr('title');
	// Get the tooltip text
	var Text = oLink.parent().find('p').html();
	
	// Don't show empty tooltips
	if (Text == "" || Text == "null") return;
	
	// Get the tooltip
	var ToolTip = jQuery('.tooltip');
	// Set the tooltip title
	ToolTip.children('.top').find('span').text(Title);
	// Set the tooltip text
	ToolTip.children('.middle').children('p').html(Text);
	// Set the tooltip's position
	ToolTip.css('left', oLink.position().left + 15 + 'px');
	ToolTip.css('top', oLink.position().top + oLink.outerHeight() + 175 + 'px');
	// Show the tooltip
	ToolTip.show();
}

function SetShowProfileMessage(oButton)
{
	if (jQuery(oButton).parent().children('input').get(0).checked)
	{
		// Set cookie to prevent page from being shown again
		SetCookie('showprofilemessage', 'false' , 60, '/Selectivo');
	}
}

function SetCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
		( ( expires ) ? ';expires='+expires_date.toGMTString() : '' )
		( ( path ) ? ';path=' + path : '' ) +
		( ( domain ) ? ';domain=' + domain : '' ) +
		( ( secure ) ? ';secure' : '' );
}

function ToggleProfile(oButton)
{
	oButton = jQuery(oButton);
	// Set the (in)active state of the button
	oButton.toggleClass('active');
	// Show/hide the profile if present
	if (jQuery(oButton).parent().find('.profile').length > 0)
	{
		jQuery(oButton.parent().find('.profile').get(0)).toggle();
	}
}

function ToggleSmokeLayer()
{
	var SmokeLayer = jQuery('.smoke');
	SmokeLayer.toggle();
	if (SmokeLayer.is(':visible'))
	{
		SmokeLayer.css('height', jQuery('.main').height());
		if (jQuery.browser.msie && parseFloat(jQuery.browser.version) <= 6)
		{
			if (jQuery(SmokeLayer.children('iframe').length == 0))
			{
				SmokeLayer.html('<iframe></iframe>');
			}
			var oIFrame = jQuery(SmokeLayer.children('iframe').get(0));
			oIFrame.css('height', jQuery('.main').height());
		}
	}
}

function TogglePhotoViewer(SelectedIndex, iViewerIndex)
{
	var PhotoViewer = jQuery('.photoviewer');
	if (iViewerIndex != null)
	{
		PhotoViewer = jQuery('#photoviewer' + iViewerIndex);
	}
	PhotoViewer.toggle();
	if (PhotoViewer.is(':visible') && SelectedIndex != null)
	{	
		// Position the photoviewer
		PhotoViewer.css('left', (jQuery('.main').offset().left + 250) + 'px');
		PhotoViewer.css('top', 172 + jQuery(document).scrollTop() + 'px');
		var PhotoViewerContent = jQuery(PhotoViewer.children('.back').get(0));
		// Show the correct photo
		var PicturesPlaceHolder = jQuery(PhotoViewerContent.children('.middle').get(0));
		PicturesPlaceHolder.children('img').addClass('hidden');
		jQuery(PicturesPlaceHolder.children('img').get(0)).removeClass('hidden');
		// Set the correct thumb active
		var ThumbsPlaceHolder = PhotoViewerContent.children('.bottom');
		ThumbsPlaceHolder.children('.thumb').removeClass('active');
		jQuery(ThumbsPlaceHolder.children('.thumb').get(0)).addClass('active');
	}
}

function ToggleMovieViewer(SelectedIndex, iViewerIndex)
{
	var MovieViewer = jQuery('.movieviewer');
	if (iViewerIndex != null)
	{
		MovieViewer = jQuery('#movieviewer' + iViewerIndex);
	}
	MovieViewer.toggle();
	if (MovieViewer.is(':visible') && SelectedIndex != null)
	{	

		// Position the videoviewer
		MovieViewer.css('left', '3000px');
		MovieViewer.css('top', 172 + jQuery(document).scrollTop() + 'px');
		var MovieViewerContent = jQuery(MovieViewer.children('.back').get(0));
		// Show the correct video
		var MoviesPlaceHolder = jQuery(MovieViewerContent.children('.middle').get(0));
		MoviesPlaceHolder.children('span').addClass('hidden');
		jQuery(MoviesPlaceHolder.children('span').get(0)).removeClass('hidden');
		// Set the correct thumb active
		var ThumbsPlaceHolder = MovieViewerContent.children('.bottom');
		ThumbsPlaceHolder.children('.thumb').removeClass('active');
		jQuery(ThumbsPlaceHolder.children('.thumb').get(0)).addClass('active');
	}
}

function FocusTextBox(oTextBox, sDefaultValue)
{
	if (oTextBox.value.length == 0)
	{
		oTextBox.value = sDefaultValue;
	}
	else if (oTextBox.value == sDefaultValue)
	{
		oTextBox.value = '';
	}
	
}

/**** Remove item (product or store) from comparison view ****/
function FilterItems(oItemCheckBox)
{
	var oItemCheckBoxes = jQuery('.item .general input');
	// Check if the selected item is the last item
	if (oItemCheckBoxes.length == 1)
	{
		// Cancel action
		oItemCheckBox.checked = true;
		return false;
	}
	if (!oItemCheckBox.checked)
	{
		oItemCheckBox = jQuery(oItemCheckBox);
		var oItem = oItemCheckBox.parent().parent();
		var iItemIndex = oItemCheckBox.attr('id').substring(4);
		var oThumb = jQuery('#thumb_' + iItemIndex).parent();

		RemoveFromCarousel(oItem, oThumb, iItemIndex);
		
		// Recalculate and set the height of the specs
		SetSpecsHeight();
	}
}

/**** Set the height of the specs on the comparison pages ****/
function SetSpecsHeight()
{
	// Initialize the spec heights to 'auto'
	jQuery('.specs li').css('height', 'auto');
	jQuery('.item li').css('height', 'auto');
	
	// Get the number of specs
	var Specs = jQuery('.specs > ul');
	var iSpecCount = jQuery(Specs.get(0)).children('li').length;
	// Get the items
	var Items = jQuery('.item');
	// Get the number of items
	var iItemCount = Items.length;
	// Get the number of item rows and rest items
	var iItemRestCount = iItemCount % 4;
	var iItemRowCount = (iItemCount - iItemRestCount) / 4;
	// Define the default spec height
	var iDefaultSpecHeight = 16;
	// Create and initialize an array to hold the spec heights
	var iSpecHeights = new Array(iSpecCount);
	for (var i = 0; i < iSpecHeights.length; i++)
	{
		iSpecHeights[i] = jQuery(jQuery(Specs.get(0)).children('li').get(i)).innerHeight() - 5;//iDefaultSpecHeight;
	}
	
	// Loop through the row items
	for (var iItemIndex = 0; iItemIndex < (iItemRowCount * 4); iItemIndex++)
	{
		var Item = jQuery(Items.get(iItemIndex));
		var ItemSpecValues = Item.find('ul > li');
		ItemSpecValues.each(function(iItemSpecValueIndex) {
			var iHeight = jQuery(this).innerHeight() - 5;
			if (iHeight > iSpecHeights[iItemSpecValueIndex])
			{
				iSpecHeights[iItemSpecValueIndex] = iHeight;
			}
		});
		if ((iItemIndex + 1) % 4 == 0)
		{
			for (var i = 0; i < 4; i++)
			{
				jQuery(Items.get(iItemIndex - i)).find('ul > li').each(function(iIndex) {
					jQuery(this).css('height', iSpecHeights[iIndex] + 'px');
				});
			}
			jQuery(Specs.get(((iItemIndex + 1) / 4) - 1)).children('li').each(function(iIndex) {
				jQuery(this).css('height', iSpecHeights[iIndex] + 'px');
			});
			
			for (var i = 0; i < iSpecHeights.length; i++)
			{
				iSpecHeights[i] = iDefaultSpecHeight;
			}
		}
	}
	// Loop through the rest items
	for (var iItemIndex = (iItemRowCount * 4); iItemIndex < iItemCount; iItemIndex++)
	{
		var Item = jQuery(Items.get(iItemIndex));
		var ItemSpecValues = Item.find('ul > li');
		ItemSpecValues.each(function(iItemSpecValueIndex) {
			var iHeight = jQuery(this).innerHeight() - 5;
			if (iHeight > iSpecHeights[iItemSpecValueIndex])
			{
				iSpecHeights[iItemSpecValueIndex] = iHeight;
			}
		});
		if ((iItemIndex + 1) == iItemCount)
		{
			for (var i = 0; i < iItemRestCount; i++)
			{
				jQuery(Items.get(iItemIndex - i)).find('ul > li').each(function(iIndex) {
					jQuery(this).css('height', iSpecHeights[iIndex] + 'px');
				});
			}
			jQuery(Specs.get(iItemRowCount)).children('li').each(function(iIndex) {
				jQuery(this).css('height', iSpecHeights[iIndex] + 'px');
			});
		}
	}
	
	HighlightSameSpecs();
}

/**** Highlight the spec rows where the specs have the same value ****/
function HighlightSameSpecs()
{
	// Remove any 'same' classes
	jQuery('ul > li').removeClass('same');
	
	// Get the items
	var Items = jQuery('.item');
	// Get the number of specs
	var Specs = jQuery('.specs > ul');
	var iSpecCount = jQuery(Specs.get(0)).children('li').length;
	var sSpecValue = null;
	
	for (var i = 0; i < iSpecCount; i++)
	{
		var bSpecIsSame = true;
		Items.each(function (iItemIndex) {
			var sCurrentSpecValue = jQuery(jQuery(this).find('ul > li').get(i)).html();
			if (sSpecValue == null)
			{
				sSpecValue = sCurrentSpecValue
			}
			if (sSpecValue != sCurrentSpecValue)
			{
				bSpecIsSame = false;
			}
		});
		if (bSpecIsSame)
		{
			jQuery('ul').each(function () {
				jQuery(jQuery(this).children('li').get(i)).addClass('same');
			});
		}
		sSpecValue = null;
	}
}

function ShowColorPicker(oButton)
{
	jQuery('.colorpalette').css('top', (jQuery(oButton).position().top - 100) + 'px');
	jQuery('.colorpalette').css('left', (jQuery(oButton).position().left + 38) + 'px');
	jQuery('.colorpalette').show();
}

function HideColorPicker()
{
	jQuery('.colorpalette').hide();
}

function HoverColor(oColor)
{
	oColor = jQuery(oColor);
	oColor.parent().next('.currentcolor').text(oColor.attr('title'));
}

function ChooseColor(oColor)
{
	oColor = jQuery(oColor);
	var sColor = getHexColor(oColor.css('background-color'));
	jQuery('.colorpicker > .color').css('background', sColor);
	jQuery('#color').val(sColor);
	HideColorPicker();
}

function getHexColor(sBrowserColor)
{
	var htmlColor = 'ffffff';
	// Check if the color is a RGB code
	if (sBrowserColor.indexOf('rgb') == 0)
	{
		var aRGB = sBrowserColor.substring(sBrowserColor.indexOf('(') + 1, sBrowserColor.indexOf(')')).split(',');
		// Normal color (Red, Green, Blue)
		if (aRGB.length == 3)
		{
			htmlColor = this.decimalToHex(parseInt(aRGB[0],10)) + this.decimalToHex(parseInt(aRGB[1],10)) + this.decimalToHex(parseInt(aRGB[2],10));
		}
		// Safari transparent color (Red, Green, Blue, Alpha)
		if (aRGB.length == 4)
		{
			if (parseInt(aRGB[3],10) == 0) // Transparent
			{
				htmlColor = 'transparent';
			}
			else
			{
				htmlColor = this.decimalToHex(parseInt(aRGB[0],10)) + this.decimalToHex(parseInt(aRGB[1],10)) + this.decimalToHex(parseInt(aRGB[2],10));
			}
		}
	}
	else
	{
		htmlColor = sBrowserColor;
	}
	
	// Check if the color contains a #
	if (htmlColor.indexOf('#') == -1)
	{
		htmlColor = "#" + htmlColor;
	}
	
	return htmlColor.toLowerCase();
}

function decimalToHex(decimal)
{
	var hex = decimal.toString(16);
	if (hex.length == 1)
	{
		hex = '0' + hex;
	}
	return hex;
}

function ShowAllOptions(id,showAll){
	
	var ident = 'ul#'+id+'_other'
	var showMoreLink = 'li#'+id+'_showMoreLink';
	var showLessLink = 'li#'+id+'_showLessLink';	
	
	if(showAll){
		jQuery(ident).show();
		jQuery(showMoreLink).hide();
		jQuery(showLessLink).show();	
	}else{
		jQuery(ident).hide();
		jQuery(showMoreLink).show();
		jQuery(showLessLink).hide();		
	}
}

function ShowFromTill(oLink)
{
	oLink = jQuery(oLink);
	oLink.next().show();
	oLink.hide();
}
function setSearchClass(style)
{
	var currTabElem = document.getElementById('searchterm');
	if(currTabElem)
	{
		currTabElem.className = style;
	}
}

function updateSortOptions(formElement, sortColumn) {
	var arrVal = new Array(0);
	if(formElement.value.length > 0) {
		var arrVal = formElement.value.split(",");
	}
	
	var addToEnd = true;
	if(arrVal.length > 0) {
		for(var i = 0;i < arrVal.length;i++) {
			if(arrVal[i] == sortColumn) {
				arrVal[i] = -arrVal[i];
				addToEnd = false;
				break;
			} else if(-arrVal[i] == sortColumn) {
				arrVal.splice(i, 1);
				addToEnd = false;
				break;
			}
		}
	}
	
	if(addToEnd) {
		arrVal[arrVal.length] = sortColumn;	
	}
	
	formElement.value = arrVal.join(",");
}


function applyCorrectImageDimensions(image, maxWidth, maxHeight){	
	
	if (jQuery(image).width() < jQuery(image).height()){ //landscape
    	image.css('width','144px');    
	}
	else{ //portrait		
   		image.css('height','128px');
	}
}