jQuery(document).ready(function($) {
	
	// make all links on the products list page open in a new window
	
	$('.products_list').find('a').click(function(){
		this.target='_blank';
	});
	
	
	// page-item-271 is Latest News, this is used to make it the current layout dynamically.
	// Instead of using jquery, I could just wrap it with a div instead of checking
	if($('.about_news_check').is('.about_news_check')) {
		$('.page-item-10').removeClass('page_item');
		$('.page-item-10').addClass('current_page_item');
		$('.page-item-10 > a').addClass('return_arrow');
	}

	if(window.location.toString() == 'http://www.calleja.com.mt/about-us/latest-news/') {
		$('.page-item-10 > a').addClass('no_cursor');
	}
	
	if(window.location.toString() == 'http://www.calleja.com.mt/products/stock-lighting/') {
		$('.page-item-16 > a').addClass('no_cursor');
	}
	
	if(window.location.toString() == 'http://www.calleja.com.mt/projects/') {
		$('.page-item-23 > a').addClass('no_cursor');
	}
	
	if(window.location.toString() == 'http://www.calleja.com.mt/about-us/recruitment/') {
		$('.page-item-14 > a').addClass('no_cursor');
	}

	if(window.location.toString() == 'http://www.calleja.com.mt/products/product-categories/') {
		$('td > a').click(function(){
			window.open(this.href);
			return false;
		});
	}

	//alert(window.location.toString());
	/*
	if($('.current_page_item > a')) {
		alert($this.value());
		$(this).addClass('no_cursor');
		$(this).hide();
	}
	*/
	
	if($('.recruitment_check').is('.recruitment_check')) {
		$('.page-item-14').removeClass('page_item');
		$('.page-item-14').addClass('current_page_item');
		$('.page-item-14 > a').addClass('return_arrow');
	}

	if($('.products_news_check').is('.products_news_check')) {
		$('.page-item-16').removeClass('page_item');
		$('.page-item-16').addClass('current_page_item');
		$('.page-item-16 > a').addClass('return_arrow');
	}

	
	if($('.projects_news_check').is('.projects_news_check')) {
		$('.page-item-23').removeClass('page_item');
		$('.page-item-23').addClass('current_page_item');
		$('.page-item-23 > a').addClass('return_arrow');
	}
	
	if($('.projects_wepower_check').is('.projects_wepower_check')) {
		$('.page-item-2264').removeClass('page_item');
		$('.page-item-2264').addClass('current_page_item');
		$('.page-item-2264 > a').addClass('return_arrow');
	}
	
	$('table.products_listing').hide();
	$('table.domestic').show();
	
	$('#search_filter_go').click(function() {
		var str = "";

	    $("select.products_list_filter option:selected").each(function () {
			str = $(this).val();
            if(str == '')
			{
			$('table.products_listing').hide();
			$('table.atif').show();
			}
			else	
			{
			$('table.products_listing').hide();
			$('table.'+str+'').show();
			}

		});
		
	});
	
	// Has to be a more specific class to override other styles
	$('.med_col2 > .post:last').addClass('last_post');
	
	// Trying to find the # of selections in a select, index them and then
	// show the appropiate indexed catalogue that matches it
	$('.catalogue').hide();
	var cat_size = $('.catalogue').size();
	
	$('.category_cat > select').change(function() {
		var str = "";
		str = $(this).attr("selectedIndex");
		str = str - 1; // gets rid of the blank filler in beginning
		
		// Disable all selects that are in a div that are not the one that corresponds to the selection that was checked
		// This way only the correct one is sent.
		
		//REmoving doesn't work either....
		//$('.catalogue:eq(0) > select').remove();
		
		// Thought disabling it might stop it from sending input, guess not
		//$('.catalogue:eq(0) > select').attr("disabled", true);
		
		/*
		var stuff = $('.category_cat > select:children').index(this);
		alert(stuff);
		*/
	    $(".category_cat > select option:selected").each(function () {
			
			//After a catalogue is selected, I need to figure a way to not pass the values of the other catalogues
			
			$('.catalogue').hide();
			$('.catalogue:eq('+str+')').show(); // var wont be replaced unless concatenated correctly
			/*
			if (str == 'other') {
				open('http://www.theoneonefour.com/other/','_self')
			}
			else {
				open('http://www.theoneonefour.com/tag/'+str,'_self')
			}
			*/
			//alert(str);
		});
		
	});
	
	// Wouldn't work because the file wasn't called properly in header, due to being case sensitive, when it wasn't before
	$("#screenshots").easySlider({
			numeric: true
	});
});

jQuery(document).ready(function($) {
	
	var options = {
		//beforeSubmit: showRequest,
	    success:    function() { 
	        alert('Your message has been sent!'); 
	    } 
	};
	
	function showRequest(formData, jqForm, options) {
		var queryString = $.param(formData);

		alert('About to submit: \n\n' + queryString);

		return true;
	}

	var name = $("#name"),
		email = $("#email"),
		password = $("#password"),
		allFields = $([]).add(name).add(email).add(password),
		tips = $("#validateTips");

	function updateTips(t) {
		tips.text(t).effect("highlight",{},1500);
	}

	function checkLength(o,n,min,max) {

		if ( o.val().length > max || o.val().length < min ) {
			o.addClass('ui-state-error');
			updateTips("Length of " + n + " must be between "+min+" and "+max+".");
			return false;
		} else {
			return true;
		}

	}

	function checkRegexp(o,regexp,n) {

		if ( !( regexp.test( o.val() ) ) ) {
			o.addClass('ui-state-error');
			updateTips(n);
			return false;
		} else {
			return true;
		}

	}

	$("#dialog").dialog({
		bgiframe: true,
		autoOpen: false,
		height: 478,
		width: 530,
		modal: true,
		buttons: {
			'Send': function() {
				var bValid = true;
				allFields.removeClass('ui-state-error');

				//bValid = bValid && checkLength(name,"username",3,16);
				bValid = bValid && checkLength(email,"email",6,80);
				bValid = bValid && checkLength(email,"email",6,80);

				//bValid = bValid && checkRegexp(name,/^[a-z]([0-9a-z_])+$/i,"Username may consist of a-z, 0-9, underscores, begin with a letter.");
				// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
				bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"eg. ui@jquery.com");

				if (bValid) {
					$('#requestForm').ajaxForm();
					
					$('#requestForm').ajaxSubmit(options);
					$(this).dialog('close');
				}
			},
			Cancel: function() {
				$(this).dialog('close');
			}
		},
		close: function() {
			allFields.val('').removeClass('ui-state-error');
		}
	});

	$(".breadcrumb > a:contains('News')").click(function() {
		open('http://www.calleja.com.mt/about-us/latest-news/','_self');
		return false;
	});
	
	$(".breadcrumb > a:contains('Projects')").click(function() {
		open('http://www.calleja.com.mt/projects/','_self');
		return false;
	});
	
	$(".breadcrumb > a:contains('Products')").click(function() {
		open('http://www.calleja.com.mt/products/','_self');
		return false;
	});


	$('#request-a-catalogue').click(function() {
		$('#dialog').dialog('open');
	})
	.hover(
		function(){ 
			$(this).addClass("request-a-catalogue-hover"); 
		},
		function(){ 
			$(this).removeClass("request-a-catalogue-hover"); 
		}
	);
	
	
	

});