// Email validation
function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(emailAddress);
}

function validate( emailAddress )
{
	if( isValidEmailAddress( emailAddress ) )
	{
		return true;
	}
	else
	{
		alert( 'Please enter an email address in the box and then click sign up' );
		return false;
	}
	
}

Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

function mycarousel_initCallback(carousel, state) {
    jQuery('#mycarousel-next').bind('click', function() {
        carousel.next();
        return false;
    });

    jQuery('#mycarousel-prev').bind('click', function() {
        carousel.prev();
        return false;
    });


};

function round_length( num, rnd ) {
	if( num % rnd != 0 )
	{
		var mod = Math.floor(num / rnd);
		num = rnd * ( mod+1 );
	}

	return num;
}

jQuery(document).ready(function() {

	var recents_length = round_length( recent_products.length, 6 );

    jQuery("#mycarousel").jcarousel({

	    size: recents_length,
	    itemLoadCallback: {onBeforeAnimation: mycarousel_itemLoadCallback},


        scroll: 6,
		animation: 'slow',
        initCallback: mycarousel_initCallback,
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null
    });

});

function chooseColour( selectedtype )
{
  document.manufacturers_form.manufacturers_id.value = selectedtype ;
  document.manufacturers_form.submit() ;
}



function mycarousel_itemLoadCallback(carousel, state)
{
	var old_first = carousel.first;
    for (var i = carousel.first; i <= carousel.last; i++) {
        if (carousel.has(i)) {
            continue;
        }

        if (i > recent_products.length) {
            break;
        }

		//alert( 'called' + recent_products['id'+recent_products[i-1]].link );
        carousel.add(i, mycarousel_getItemHTML( recent_products['id'+recent_products[i-1]]));
    }

    jQuery('.delete_item').bind('click', function() {
		// Need to work out which item we are
		$items = $( '.delete_item' );
		for( $i = 0 ;  $i < $items.length ; $i++)
		{
			if( $(this).attr('id') == $($items[$i]).attr('id') )
			{
				break;
			}
		}
		
		if( old_first == recent_products.length )
		{
			recent_products.remove( $i );
			carousel.reset();
			carousel.scroll( old_first, false );
			old_first -= 6;
			carousel.scroll( old_first );
			carousel.size( round_length( recent_products.length, 6 ) );
		}
		else
		{
			recent_products.remove( $i );
			carousel.size( round_length( recent_products.length, 6 ) );
			carousel.reset();
			carousel.scroll( old_first, false );
		}
		
		// delete the item from the cookie as well
		$cookie_items = $.cookie( 'recent_products' ).split('|');
		$cookie_items.remove( $i );
		$cookie_string = $cookie_items.join('|');
		$.cookie( 'recent_products', $cookie_string );
		
        return false;
    });


};

/**
 * Item html creation helper.
 */
function mycarousel_getItemHTML(item)
{
	content = '';
	content += '<table cellspacing="0" cellpadding="0">' + "\n";

	content += '<tr height="17">' + "\n";
	content += '<td width="140" align="center" rowspan="4"><a href="' + item.link + '">' + item.image + '</a></td>' + "\n";
	content += '<td></td>';
	content += '</tr>';

	content += '<tr height="33">';
	content += '<td><a class="delete_item" id="del_' + item.index + '" href="#">x</a></td>';
	content += '</tr>';

	content += '<tr height="33">';
	content += '<td width="20" valign="bottom"><a href="' + item.link + '" class="recent_info"><b>i</b></a>';
	content += '</tr>' + "\n";

	content += '<tr height="17">';
	content += '<td></td>';
	content += '</tr>';

	content += '</table>' + "\n";	
	
    return content;
};

