$jQ(function(){

$jQ('.left .right ul').css('marginTop','5px').prev('p').css('marginBottom','0px');

var setWidth = 0;
var i = 0;	
$jQ('.nav .nav').each(function(){
	var listItem = $jQ(this).children('li');
	var listLength = listItem.length;
//	$jQ('h2').append(' Total items equal: ' + listLength);
	listItem.each(function(){
		var span = $jQ(this).find('span');
		var spanWidth = span.width();
		if (spanWidth > setWidth) {
			setWidth = spanWidth;
		}
		i++;
//		$jQ('h2').append(' Count item: ' + i);
	});
	setWidth = setWidth + 16;
	$jQ(this).css('width', setWidth);
	setWidth = 0;
	i = 0;
});
$jQ('.nav li').hover(function(){
	var thisList = $jQ(this).parent('ul');
	var nextList = $jQ(this).find('.nav:eq(0)');
	if (nextList.parent('li').parent('ul').parent('li').length == 0){
		nextList.css('left', '2px');
		} else {
		var nextListLeft = thisList.width();
//		nextListLeft += 16;
		nextList.css('left', nextListLeft);
	}
}, function(){
	$jQ(this).find('.nav:eq(0)').css('left','-9999em');
});
$jQ('.nav li:last-child').css('border','none');

	if ($jQ(".errors").length){
		var errors = $jQ('.errors:eq(0)');
		errors.parent().parent().slideDown();
		errors.parent().parent().children('p:eq(0)').hide();
	}

	$jQ("button[type='reset']").click(function(){
		$jQ(this).siblings().not('button').each(function(){
			var input = $jQ(this);
			if (input.is('select')){
				var option = input.children('option:eq(0)');
				option.val(option.attr('title')).attr("selected", "selected");
				} else {
			input.val(input.attr('title'));
			}
			input.addClass("defaultText").removeClass("clientText");
		});
		return false;
	});

	$jQ('.login').click(function(){
		$jQ('.login_form').slideToggle();
	});


	$jQ('.questions a,.enews a').click(function(){
		var click = $jQ(this);
		var form = click.parent().find('.inner_div');
		var otherForm = $jQ('.inner_div').not(form);
		form.slideToggle();

		if (click.is('.btnReg')){
		click.removeClass('btnReg');
		click.addClass('btnActive');
		click.css({backgroundPosition: '0px -35px'});
		} else {
		click.removeClass('btnActive');
		click.addClass('btnReg');
		click.css({backgroundPosition: '0px 0px'});
		}
		if(otherForm.is(':visible')){
			otherForm.slideToggle();
			otherForm.prev('a').removeClass('btnActive');
			otherForm.prev('a').addClass('btnReg');
			otherForm.prev('a').css({backgroundPosition: '0px 0px'});
		}

	});

	$jQ(".productListing").css("marginRight", "0px");
	$jQ(".productListing:nth-child(3n+3)").css({
		marginRight: '0px'
	});
	$jQ(".productListing:nth-child(3n+1)").css({
		clear: 'both'
	});

	$jQ(".productCategoryListing:even").css("marginRight", "35px").before('<div class="clear"></div>');
	$jQ(".productCategoryListing:odd").css("marginRight", "0px");

	$jQ('.focal').cycle({
		fx: 'fade',
		timeout: 8500
});

	// Carousel
	window.list = $jQ('.cleanerSmarter');
	window.parentlength = 691;

	window.children = list.children().length;
	window.childrenwidth = 240;
	window.childrentotal = children * childrenwidth;
	window.list.css('width', childrentotal);

	$jQ('#prev').click(function(){
		if (!list.is(':animated')) {
			newmargin = list.margin().left+240;
			if(newmargin <= 0) {
				list.animate({marginLeft: newmargin}, 550);
				}
		}

	});

	$jQ("#next").click(function(){
		if (!list.is(':animated')) {
			newmargin = list.margin().left-240;
			if(newmargin + list.width() >= parentlength) {
				list.animate({marginLeft: newmargin}, 550);	
				}
		}

	});

	// Enable File extention icons for Technical page
	$jQ('#technical #content .left a, #free .left .right a').fileIcon({
		path: '_master/images/',
		lineHeight: 15
	});

	//Read More
	var readMore = $jQ("a[title='read_more']");

	readMore.addClass("readMore");

	var next = readMore.parent().next();
	while (next.length != 0 && next[0].nodeName != 'div') {
        	next.hide();
        	next = next.next();
	}

	readMore.click(function(){
//		$jQ(this).toggleClass("active");
		$jQ(this).hide();
		var next = readMore.parent().next();
		while (next.length != 0 && next[0].nodeName != 'div') {
        		next.toggle();
        		next = next.next();
		}
	return false;
	});	

				$jQ('.modals').each(function(){
					$jQ(this).dialog({
						autoOpen: false,
						width: 735,
						modal: true,
						resizable: false
					});					
				});			
				
				$jQ('.anchors').each(function(){
					var current = $jQ(this);
					var hook = current.attr('rel');
					var modal = $jQ('#'+hook);
					current.click(function(){
						modal.dialog('open');
						return false;
					});
				});


});

$jQ(window).load(function () {
// Open the curtains!!!
	$jQ('#content').fadeIn(250);
});
