
function getUrlParam(param)
{
	var loc = window.location.toString().split('#')[0];	
	var params = loc.split('?')[1].split('&');
	for(var i = 0; i < params.length; i++)
	{
		if(params[i].split('=')[0].toLowerCase() == param.toLowerCase())
		{
			return decodeURIComponent(params[i].split('=')[1]);	
		}
	}
	return null;
}

function sortRig(param)
{
	switch(param.toLowerCase().replace(/\++/gi,' ')){
		case 'title':
			$('#rig-list').BCWebAppSort({
				q_elements:	'li.rig-item',
				q_field: '.head .col2 h2', 
                q_extrafield: '.head .col3 p'	
			});
			break;
		case 'rig id':
			$('#rig-list').BCWebAppSort({
				q_elements:	'li.rig-item',
				q_field: '.head .col3 p',
				sortDataType: 'number'
			});
			break;
		case 'type':
			$('#rig-list').BCWebAppSort({
				q_elements:	'li.rig-item',
				q_field: '.head .col4 p', 
                q_extrafield: '.head .col3 p'	
			});
			break;
		case 'carrier':
			$('#rig-list').BCWebAppSort({
				q_elements:	'li.rig-item',
				q_field: '.head .col5 p', 
                q_extrafield: '.head .col3 p'	
			});
			break;
		case 'model':
			$('#rig-list').BCWebAppSort({
				q_elements:	'li.rig-item',
				q_field: '.head .col6 p', 
                q_extrafield: '.head .col3 p'	
			});
			break;
		case 'rego':
			$('#rig-list').BCWebAppSort({
				q_elements:	'li.rig-item',
				q_field: '.head .col7 p', 
                q_extrafield: '.head .col3 p'	
			});
			break;
		default:
	}
		
}


$(document).ready(function(){


//News rotator	
	var panEffect = "easeInOutQuad";
	var panDuration = 2000;
	var panInterval = 3000;
	
	if(jQuery.fn.NewsRotator)
	{
		$('.news-rotator').NewsRotator({
			interval: 8000,
			items_query: '.items',
			item_query: '.item',
			image_query: '.image',
			orientation: 'horizontal',
			current_class: 'current',
			duration: 1000,
			easing: "easeInOutExpo"
			/*
			beforeStart: function(){//Before starting the rotation
				setTimeout(function(){
					var distance = (parseInt($('.news-rotator').find('.current img')[0].height) - parseInt($('.news-rotator').innerHeight()));
					distance = distance > 0 ? -(distance) : 0;
					$('.news-rotator').find('.current .image').animate({
						marginTop: distance											   
					},panDuration,panEffect);
				},panInterval);
			},
			afterSlide: function(){
				setTimeout(function(){
					var distance = (parseInt($('.news-rotator').find('.current img')[0].height) - parseInt($('.news-rotator').innerHeight()));
					distance = distance > 0 ? -(distance) : 0;
					$('.news-rotator').find('.current .image').animate({
						marginTop: distance											   
					},panDuration,panEffect);
				},panInterval);
			}
			*/
		});	
	}
	
	
	
//Rig list sort

	var RigSortParams = ['- select -','Title','Rig ID','Type','Carrier','Model','Rego'];
	
	if($('#sort-rigs').length > 0)//If placeholder for rig-list sort is on page
	{
		var oForm = $('<form></form>',{//Create form
			action: '/drilling-equipment-sort'				  
		});
		
		$('<label for="rig-cats">sort rigs by:</label>').appendTo(oForm);//Add label to form
		var oSelect = $('<select id="rig-cats" name="rig-cats"></select>').change(function(){
			var action = $(this).parent().attr('action').split('?')[0] + '?sort_type=' + encodeURIComponent($(this).val());																				   			$(this).parent().submit();
		});
		$(RigSortParams).each(function($key,$value){
			$('<option></option>',{
				name: $value,
				text: $value
			}).appendTo(oSelect);									
		});	
		$(oSelect).appendTo(oForm);
		$(oForm).appendTo('#sort-rigs');
	}
	
	if($('#sort-rigs-form').length > 0)//If placeholder for rig-list-form sort is on page
	{
		var oForm = $('<form></form>');//Create form
		$('<label for="rig-cats">sort rigs by:</label>').appendTo(oForm);//Create label and append it to form
		var oSelect = $('<select id="rig-cats" name="rig-cats"></select>').change(function(){//Create select and attach change event
			sortRig($(this).val());
		});
		$(RigSortParams).each(function($key,$value){
			$('<option></option>',{
				name: $value,
				text: $value
			}).appendTo(oSelect);									
		});	
		$(oSelect).appendTo(oForm);
		$(oForm).appendTo('#sort-rigs-form');
		
		var param = getUrlParam('rig-cats');
		if(param)
		{
			$(oSelect).val(param.replace('+',' '));
			sortRig(param);
		}
	}
	
//Rig sort second set of pagination links

var pag2 = $('#pagination2');
var pag1 = $('#webapp8829pagination');
if(pag1.length > 0 && pag2.length > 0)
{
	$(pag1).clone().removeAttr('id').appendTo(pag2);
}


//Enquiry form inputs
	
	if(jQuery.fn.TextInputClearer)
	{
		
		var vElements = ['#CAT_Search','#FullName','#EmailAddress','#WorkPhone','#Company',
		   				 '#WorkAddress','#WorkCity','#WorkState','#WorkZip', 
		                 '#WorkCountry','#CAT_Custom_134997','#CaptchaV2','#CAT_Custom_140045','#CAT_Custom_140059',
		                 '#CAT_Custom_140274','#CAT_Custom_140276','#CAT_Custom_141477','#CAT_Custom_168351'];
		
		
		var eDrillingBooking = ['#db-form #FirstName', '#db-form #LastName', '#db-form #CAT_Custom_67503',
								'#db-form #CAT_Custom_65341', '#db-form #WorkPhone', '#db-form #EmailAddress', '#db-form #CAT_Custom_65342',
								'#db-form #CAT_Custom_65343', '#db-form #CAT_Custom_65343', '#db-form #CAT_Custom_65344', '#db-form #CAT_Custom_65345',
								'#db-form #CAT_Custom_65347', '#db-form #CAT_Custom_65348', '#db-form #CAT_Custom_65349', '#db-form #CAT_Custom_65350',
								'#db-form #CAT_Custom_65351', '#db-form #CAT_Custom_65354', '#db-form #CAT_Custom_65356', '#db-form #CAT_Custom_65358',
								'#db-form #CAT_Custom_65364', '#db-form #CAT_Custom_65366', '#db-form #CAT_Custom_65367', '#db-form #CAT_Custom_65368',
								'#db-form #CAT_Custom_65369', '#db-form #CAT_Custom_65371', '#db-form #CAT_Custom_65375', '#db-form #CAT_Custom_65380',
								'#db-form #CAT_Custom_65384', '#db-form #CAT_Custom_65387', '#db-form #CAT_Custom_65388', '#db-form #CAT_Custom_65389',
								'#db-form #CAT_Custom_65390', '#db-form #CAT_Custom_65392', '#db-form #CAT_Custom_65394', '#db-form #CaptchaV2'];
		
		jQuery.merge(vElements,eDrillingBooking);
		
		$(vElements.join(',')).TextInputClearer({
			blurSuccess: function(){
				$(this).css({
					'font-style': 'normal',
					color: '#535E6F'
				});	
			},
			blurFail: function(){
				$(this).css({
					'font-style': 'italic',
					color: '#8594AE'
				});	
			}
		});
	}
	
	
//Search form validation

	$('#search').submit(function($e){
		var sVal = $('#CAT_Search').val();
		var sRel = $('#CAT_Search').attr('rel');
		
		if(!sVal || $.trim(sVal.toLowerCase()) == sRel.toLowerCase())
		{
			$e.preventDefault();
			alert('You must enter a valid search term.');
		}
	});
	

//General enquiry form - Enquiry Source field

if($('#CAT_Custom_140046').length > 0)//if hiddend field for Enquiry source exists
{
	var section = '';
	var subsection = '';
	$('#nav_796749').children().each(function(){//Loop through sections
		if($(this).attr('class').indexOf('-active') != -1)//If this is the current section
		{
			section = $(this).find('a:first').text();
			
			$(this).find('li').each(function(){
				if($(this).attr('class').indexOf('active') != -1)
				{
					subsection	= $(this).find('a:first').text();
				}
			});
		}
	}); 	
	
	$('#CAT_Custom_140046').val('General enquiry from ' + section + ' - ' + subsection);
}


//Rig list detail table styling
var trClasses = 'odd';
$('.equip-detail-left table tbody').children().each(function($key,$value){
	if($(this).children().eq(0).is('th'))
	{
		trClasses = 'odd'			
	} else {
		$(this).addClass(trClasses);
		trClasses = (trClasses == 'odd') ? 'even' : 'odd';	
	}
});

//Print button in rig list detail

$('a.print').click(function($e){
	$e.preventDefault();
	window.print();
});

	
/*	
//Enquiry form

	
	$('#CAT_Custom_137747').parent().css('display','none');//Hide drop down with enquiry type
	if($('#nav_796749 .drilling').hasClass('drilling-active'))//If drilling serives is the current section
	{
		
		var eTypes = ['General Enquiry'];
		$('#nav_797502 a').each(function($key,$value){//Get subsection items
			eTypes.push($(this).text());								 
		});
		
		var selected = $('#nav_797502 li.selected').text();//Get current subsection
		
		$('#CAT_Custom_137747 option').remove();//Remove options from
		$('<option value=" ">-- Enquiry Type --</option>').appendTo('#CAT_Custom_137747');
		for(var i = 0; i < eTypes.length; i++)
		{
			var current = (selected && selected == eTypes[i]) ? 'selected="selected"' : '';
			$('<option value="' + eTypes[i] + '" ' + current + '>' + eTypes[i] +'</option>').appendTo('#CAT_Custom_137747');		
		}
		
	}
	
	
//Heading for enquiry form
	var curSub = ($('#nav_797502 li.selected').length > 0 && $('#nav_797502 li.selected').text()) ? $('#nav_797502 li.selected').text() : null;
	if(curSub)
	{
		if(curSub == 'Site Investigation Drilling')$('.sidebar h3').text('Make a Site Investigation Drilling Booking');	
	}
*/	
});




















