jQuery.noConflict();
jQuery(document).ready(function($){

	if ($('#main').hasClass('homepage')) {
		alert('yep');
		$('#main ul.products li:nth-childre(0)').hide();
	}
	
	//Fixes the FireFox 3.6 padding rejection on input elements
	if (jQuery.browser.mozilla && jQuery.browser.version == '1.9.2') {
		$('input').addClass('vPadding');
	}
	
	
	//Loads The Rotator
	$.featureList($("#tabs li"), $("#output li"), {start_item: 0});
	
	
	//Clears inputs when clicked and returning the default values when blured
	//Input text fields have to had a class="clearText"
	//Textareas have to had calsses "clearText" and "textfield" 
	$('.clearText').each(function() {
		var textareaFlag = false;
		var userInputFlag = false;
		var value;
		
		if($(this).hasClass()=="textfield") textareaFlag = true;
		if(!textareaFlag) value = $(this).val();
		else value = $(this).text();
		
		$(this).focus(function() {
			if (!textareaFlag) {
				if ($(this).val()==value) $(this).attr("value", "");
			}
			else  {
				if($(this).text()==value) $(this).empty();
				alert("sadf");
			}				
		});
		
		$(this).keypress(function() {
			userInputFlag = true;
		});
		
		$(this).blur(function() {
			if (!textareaFlag) {
				if (!userInputFlag) $(this).attr("value", value);
			}
			else {
				if(!flag) $(this).text(value);
			}
		});	
	});
	
	
	//Changes the dynamic tabs
	//Dynamic buttons have to had a class="dynamicTab"
	//Dynamic divs have to had a id = dynamicButton.id + "Tab"
	$('a.dynamicTab').click(function() {
		$('a.dynamicTab').each(function() {
			$(this).removeClass('active');
			$('#' + $(this).attr('id') + 'Tab').hide();	
		});
		if(!$(this).parent().parent().hasClass('buttonRotator'))  $(this).addClass('active');
		$('#' + $(this).attr('id') + 'Tab').show();		
	});

	
	//Creates the button's rotator
	//The class='buttonRotator' is needed on the ul element
	var rotator = new Array();
	var rotatorID = new Array();  
	var rotatorSize = $('ul.buttonRotator li:last-child').index();
	var rotatorInit = 0;
	var index;
	
	$('ul.buttonRotator li').click(function() {
		index = $(this).index();			
		
		$('ul.buttonRotator li').each(function() {		
			rotator[rotatorInit] = $(this).children().text();
			rotatorID[rotatorInit++] = $(this).children().attr('id');
		});
				
		$('ul.buttonRotator li').each(function() {
			$(this).children().text(rotator[index]);
			$(this).children().attr('id', rotatorID[index]);
			if(++index > rotatorSize) index = 0;
		});
		
		$('ul.buttonRotator li:first-child').children().addClass('active');
		
		for(var i = 0; i<=rotatorSize; ++i) {
			rotator[i] = $('ul.buttonRotator li:nth-child(' + (i+1) + ')').children().text();
			rotatorID[i] = $('ul.buttonRotator li:nth-child(' + (i+1) + ')').children().attr('id');
		}
	});	
		
	
	//Changes the review stars
	//The class='dynamicRating' is required on the ul elements
	$('ul.dynamicRating li').hover(function() {
		var rating = $(this).index() + 1;
		var currentRated = $(this).parent().parent().attr('id');
		
		for(var i=1; i<=rating; ++i) {
			$('#' + currentRated + ' ul li:nth-child(' + i + ')').addClass('rating');
		}
		
		$(this).click(function() {
			//$(this).parent().siblings(':input').attr('value', $(this).index()+1);
			
			if ( $(this).parent().parent().attr('id') == 'quality' )
				$(this).parent().siblings(':input').attr('value', $(this).index()+1);
			else if ( $(this).parent().parent().attr('id') == 'value' ) 
				$(this).parent().siblings(':input').attr('value', $(this).index()+6);
			else if ( $(this).parent().parent().attr('id') == 'price' ) 
				$(this).parent().siblings(':input').attr('value', $(this).index()+11);
			
			
			$(this).parent().siblings(':input').attr('rating', $(this).index()+1);
			
			for(var i=1; i<=$('#' + currentRated + ' ul li:last-child').index()+1; ++i) {
				$('#' + currentRated + ' ul li:nth-child(' + i + ')').removeClass('rating');
			}
						
			for(var i=1; i<=parseInt($(this).parent().siblings(':input').attr('rating')); ++i) {			
				$('#' + currentRated + ' ul li:nth-child(' + i + ')').addClass('rating');
			}	
		}); 
		
	}, function() {
		var rating = $(this).index() + 1;
		var currentRated = $(this).parent().parent().attr('id');
		
		for(var i=1; i<=rating; ++i) {
			$('#' + currentRated + ' ul li:nth-child(' + i + ')').removeClass('rating');
		}
		
		for(var i=1; i<=parseInt($(this).parent().siblings(':input').attr('rating')); ++i) {			
			$('#' + currentRated + ' ul li:nth-child(' + i + ')').addClass('rating');
		}
	});
	
	
	//Validation
	//The class="validation" is required on the fields and class="submit" on the submit buttons
	//every input should have one of the following classes
	//for names class="name"
	//for numbers class="phone"
	//for emails class="email"
	//var init = new Array();
	var inputValues = new Array(new Array());
	var inputValuesSize = 0;	
	
	$('.validation').each(function() {
		if(!inputValues[inputValuesSize][0]) inputValues[inputValuesSize][0] = $(this).parent().parent().attr('id');
		if (inputValues[inputValuesSize][0] == $(this).parent().parent().attr('id')) inputValues[inputValuesSize][$(this).index() + 1] = $(this).val();
		else {
			inputValues[++inputValuesSize] = new Array();
			inputValues[inputValuesSize][0] = $(this).parent().parent().attr('id');
			inputValues[inputValuesSize][$(this).index() + 1] = $(this).val();
		}
	}); 
	
	$('.submit').click(function() {		
		var validateFlag = false;
		var idInArray;		
	
		for(var i=0; ; ++i) {
			if (inputValues[i][0] == $(this).parent().parent().attr('id')) {
				idInArray = i;
				break;
			}				
		}
		
		$('#' + $(this).parent().parent().attr('id') + ' .validation').each(function() {
			if($(this).val() == inputValues[idInArray][$(this).index()+1] || $(this).val()=="" || validateForm($(this).parent().parent().attr('id'), $(this).index(), $(this).val())) {
				$(this).addClass('notValid');
				validateFlag = true;					
			}			
			else if($(this).hasClass('notValid')) $(this).removeClass('notValid');
		});
		
		if(validateFlag) {
			alert("Please fill in all required fields\nbefore submitting the form.\n\nThank You!");
			
			return false;
		} 
		
		////////////////////////////////////////////////////Only for Tubes Outlet !!!/////////////////////////////////////////////
		if($(this).parent().parent().attr('id') == "instantPriceForm" ) {
			$('#thankYouPopUp').show().vCenter();
			return false;
		} else if ($(this).parent().parent().attr('id') == "miniContactForm" ) {
			$('#contactThankYou').show().vCenter();
			return false;
		}
		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		return true;	
	});
	
	////////////////////////////////////////////////////Only for Tubes Outlet !!!/////////////////////////////////////////////
	//Triggers the ThankYou pop up submit
	$('#thankYouPopUp span.close').click(function() {
		$('#thankYouPopUp').hide().vCenter();
		$('#instantPriceForm').ajaxSubmit();
	});
	$('#contactThankYou span.close').click(function() {
		$('#contactThankYou').hide().vCenter();
		$('#miniContactForm').ajaxSubmit();
	});
	$('body.home #mainInnerNav h2').text('');	$('body.home #mainInnerNav h2').addClass('fix'); //$('#headerSecondNav h2').text('   ');	$('#headerSecondNav h1').text('');

	$('#imageArea').append('<div id="zoomFix"></div>');
	//Image Zoom
	//$('#zoom').addClass('MagicZoomPlus');
	//$('#zoom').append('<img src="'+ $('#image').attr('src') +'" style="" />');	
	/*$('#imageArea ul li a img').mouseover(function() {		
			currentLink = $('#image').attr('src');		
			$('#zoom img').attr('src' , $('#image').attr('src'));
		})
	*/
	})
	
	function validateForm(toValidateParentId, toValidateIndex, toValidateVal) {
	if ( $('#' + toValidateParentId + ' .validation:nth-child(' + (toValidateIndex + 1) + ')') ) {
		if($('#' + toValidateParentId + ' .validation:nth-child(' + (toValidateIndex + 1) + ')').hasClass('name')) {
			var nameExp = /[0-9]/;
			
			if(nameExp.test(toValidateVal)) return true;					
		}
		else if ($('#' + toValidateParentId + ' .validation:nth-child(' + (toValidateIndex + 1) + ')').hasClass('phone')) {
			var phoneExp = /[a-zA-Z]/;		
			
			if(phoneExp.test(toValidateVal)) return true;
		}
		else if ($('#' + toValidateParentId + ' .validation:nth-child(' + (toValidateIndex + 1) + ')').hasClass('email')) {
			var mailExp = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			
			if(!mailExp.test(toValidateVal)) return true;		
		}	
	}
	
	return false;	
}

