$(function(){

	if(typeof console === "undefined") {
		console = { log: function() { } };
	}
	
	var $ = jQuery;
	
	var PIS	=	{
	
		flash: function() {
			
			var flashvars = {};

			var params = {
				wmode:"transparent"
			};
			var attributes = {};			
			swfobject.embedSWF(SITE.flash_url, "header-flash", "1224", "332", "9.0.0", "/assets/flash/expressInstall.swf", flashvars, params, attributes);		
		},
	
	
		ui: function(){
			
			//HOME
			$('#home #footer p.first')
				.appendTo('#home #sidebar')
				.find('a')
				.text('Contact Us')
				.attr('href', '/contact-us/general-enquires/');
				
			$('#header ul li').hover(function() {
				$(this).addClass('over');
			}, function() {
				$(this).removeClass('over');
			});
			// QUICK LINKS
			$('#quick-links ul li:last-child').addClass('last');
			// CONTENT LIST "ARROW BULLETS"
			$('#main ul.arrow-bullets li').prepend('&gt; ');
			// PHOTO GALLERIES
			if ($('#photo-thumbs').length > 0) {
				$('#photo-thumbs').jcarousel({ scroll: 5 });
			}
			
			var loadGalleryImage = function(image) {
				$image = $(image).find('a').attr('href');
				$caption = $(image).find('a').attr('title');
				$('#photo-block-visual img').attr('src', $image);
				$('#photo-block p.caption').html($caption);
			};
			
			loadGalleryImage($('#photo-thumbs li:first'));
			
			$('#photo-thumbs li').click(function(e) {
				loadGalleryImage(this);
				e.preventDefault();
			});
			
			// TABBED CONTENT
			if ($('#tabbed-content').length > 0) {
				$( '#tabbed-content' ).tabs();
			}
			// FORMS
			$('#login form td input').wrap('<a href="#" class="checkbox"></a>');
			$('#login form a.checkbox').live('click', function (event) {
				if ($(this).parent().hasClass('active')) {
					$(this).parent().removeClass('active');
					$(this).parent().addClass('off');
					$('#login form .off input').attr('checked', false);
				}
				else {
					$(this).parent().removeClass('off');
					$(this).parent().addClass('active');
					$('#login form .active input').attr('checked', true);
				}
				return false;
			});
			$('p.radio label').wrapInner('<a href="#" class="radio-button"></a>');
			$('a.radio-button').live('click', function (event) {
				$('form p.radio').removeClass('active');
				$('form p.radio label').removeClass('active');
				$('form p.radio input').attr('checked', false);
				$(this).parent().addClass('active');
				$(this).parent().parent().addClass('active');
				$('form p.active input').attr('checked', true);
				return false;
			});
			if ($('select').length > 0) {
				$('select').selectBox();
			}
			// LOGIN AREA
			$('#login table th:nth-child(1)').addClass('td1');
			$('#login table th:nth-child(2)').addClass('td2');
			$('#login table th:nth-child(3)').addClass('td3');
			$('#login table th:nth-child(4)').addClass('td4');
			$('#login table th:nth-child(5)').addClass('td5');
			$('#login table td:nth-child(1)').addClass('td1');
			$('#login table td:nth-child(2)').addClass('td2');
			$('#login table td:nth-child(3)').addClass('td3');
			$('#login table td:nth-child(4)').addClass('td4');
			$('#login table td:nth-child(5)').addClass('td5');
			// CLEARING FLOATS
			$('#main ul').addClass('cleared');		
			$('.cleared').append('<span class="clear"></span>');		
			
			
			if ($('#nav-sub-questions-answers').length > 0) {
				$('#nav-sub-questions-answers a').text('Q & A');
			}
			
			if (SITE.flash_url) {
				this.flash();
			}
			
			//DATEPICKER
			$('#pickup').datepicker({
				showOn: "button",
				buttonImage: "/assets/images/calendar.gif",
				buttonImageOnly: true,
				dateFormat: 'dd/mm/yy' 
			});
			
			//FORM VALIDATION
			$('form').validate({
				required 	: "required",
				email		: {
					email:true
				},
				errorPlacement: function(error, element) {
					return true;
				}, 
				invalidHandler: function(form, validator) {
					$(validator.errorList).each(function(index, ui) {
						if(ui.element.id == "query") {
							$('.selectBox').addClass('error');
						}
					});
				}

			});
			
			$('.jcarousel-skin .cp-edit-link').click(function(event) {
				window.open($(this).find('a').attr('href'));
			});


			
		},
		
		init: function(){
			PIS.ui();
		}
			
	};
	
	PIS.init();

});
