
function initAjaxForm(){
	$('form.ajaxForm').ajaxForm(function(strHtml){
		if(strHtml.indexOf('Error|') > -1){
			$('#error_content').html(strHtml.substring(6));
			centerDiv($('#error_box'));
			$('#error_box').fadeIn();
		}else{
			$('#existingWrap').html(strHtml);
			hideForm();
			$('#frmDisplayOrder').ajaxForm();
		}
	});
		
}

function initAjaxPanelForm(){
	$('form.ajaxPanelForm').ajaxForm(function(strHtml){
		if(strHtml.indexOf('Error|') > -1){
			$('#error_content').html(strHtml.substring(6));
			centerDiv($('#error_box'));
			$('#error_box').fadeIn();
		}else{
			$('#panel_form').slideUp(function(){
				$('#content_wrap').load('content.page.php?p='+strHtml,function(){
					$('#content_wrap').slideDown();
				});
			});
		}
	});
}






function initColorPicker(){
	$('#frmColourScheme input.picker').ColorPicker({
		livePreview : true,
		onSubmit: function(hsb, hex, rgb, el) {
			$(el).val('#'+hex);
			$(el).ColorPickerHide();
			$(el).css('background','#'+hex);
		},
		onBeforeShow: function () {
			$(this).ColorPickerSetColor(this.value);
		},
		onChange: function(hsb, hex, rgb, el, rgbTotal){
			if(rgbTotal > 400){
				$(el).css('color','#000');
			}else{
				$(el).css('color','#fff');
			}
			$(el).css('background','#'+hex);
			$(el).val('#'+hex);
		}
	}).bind('keyup', function(){
		$(this).ColorPickerSetColor(this.value);
		$(this).css('background',$(this).val());
	});

}

function hideForm(){
	$('#formWrap').slideUp(function(){
		$('#existingWrap').slideDown();
	}).html('');
}

function centerDiv(obj){
	
	var width = $(obj).width();
	var height = $(obj).height();
	
	var marginLeftPx = (width/2) * -1;
	var marginTopPx = (height/2) * -1;
	
	$(obj).css({
		marginLeft 	:	marginLeftPx+'px',
		marginTop 	:	marginTopPx+'px',
		left		:	'50%',
		top			:	'40%'
	});
}

function updatePanelForm(intValue){
	
	if(intValue==2){
		
		$('#selColourLevel').val(2);
		$('#selHeight').val(1);
		$('#panel_height_wrap').hide();
		$('#colour_level_wrap').hide();
		$('#panel_flash_wrap').hide();
		$('#link_url_wrap').show();
		$('#panel_width_wrap').show();
		$('#panel_image_wrap').show();
		$('#panel_gallery_wrap').hide();
		$('#subtitle_wrap').show();
		$('#link_title_wrap').show();
		$('select','#panel_gallery_wrap').val('');
		$('#youtube_url_wrap').hide();
		
	}else if(intValue==5){
		
		$('#selColourLevel').val(2);
		$('#selHeight').val(1);
		$('#panel_height_wrap').hide();
		$('#colour_level_wrap').hide();
		$('#panel_flash_wrap').hide();
		$('#link_url_wrap').show();
		$('#panel_width_wrap').show();
		$('#panel_image_wrap').hide();
		$('#panel_gallery_wrap').show();
		$('#subtitle_wrap').show();
		$('#link_title_wrap').show();
		$('#youtube_url_wrap').hide();
		
	}else if(intValue==3){
		
		$('#selColourLevel').val(1);
		$('#selHeight').val(1);
		$('#panel_height_wrap').hide();
		$('#panel_width_wrap').hide();
		$('#colour_level_wrap').hide();
		$('#link_url_wrap').hide();
		$('#panel_image_wrap').hide();
		$('#panel_flash_wrap').hide();
		$('#panel_gallery_wrap').hide();
		$('#subtitle_wrap').show();
		$('#link_title_wrap').show();
		$('select','#panel_gallery_wrap').val('');
		$('#youtube_url_wrap').hide();
		
	}else if(intValue==4){
		
		$('#selHeight').val(1);
		$('#panel_height_wrap').hide();
		$('#panel_width_wrap').hide();
		$('#colour_level_wrap').show();
		$('#link_url_wrap').show();
		$('#panel_image_wrap').show();
		$('#panel_gallery_wrap').hide();
		$('#subtitle_wrap').show();
		$('#panel_flash_wrap').hide();
		$('#link_title_wrap').hide();
		$('select','#panel_gallery_wrap').val('');
		$('#youtube_url_wrap').hide();
		
	}else if(intValue==6){
		
		$('#panel_width_wrap').show();
		$('#panel_height_wrap').show();
		$('#colour_level_wrap').show();
		$('#link_url_wrap').hide();
		$('#panel_image_wrap').hide();
		$('#panel_gallery_wrap').hide();
		$('#subtitle_wrap').hide();
		$('#link_title_wrap').hide();
		$('#panel_flash_wrap').show();
		$('select','#panel_gallery_wrap').val('');
		$('#youtube_url_wrap').hide();
		
	}else if(intValue==7){
		
		$('#selColourLevel').val(2);
		$('#selHeight').val(1);
		$('#panel_height_wrap').hide();
		$('#colour_level_wrap').hide();
		$('#panel_flash_wrap').hide();
		$('#link_url_wrap').show();
		$('#panel_width_wrap').hide();
		$('#panel_image_wrap').hide();
		$('#panel_gallery_wrap').hide();
		$('#subtitle_wrap').hide();
		$('#link_title_wrap').show();
		$('#youtube_url_wrap').show();
		$('select','#panel_gallery_wrap').val('');
		
	}else if(intValue==8){
		
		$('#selHeight').val(1);
		$('#panel_height_wrap').hide();
		$('#colour_level_wrap').hide();
		$('#panel_flash_wrap').hide();
		$('#link_url_wrap').hide();
		$('#panel_width_wrap').show();
		$('#panel_image_wrap').hide();
		$('#panel_gallery_wrap').hide();
		$('#subtitle_wrap').hide();
		$('#link_title_wrap').hide();
		$('#youtube_url_wrap').hide();
		$('select','#panel_gallery_wrap').val('');
		
	}else{
	
		$('#panel_width_wrap').hide();
		$('#panel_height_wrap').hide();
		$('#colour_level_wrap').hide();
		$('#link_url_wrap').hide();
		$('#panel_image_wrap').hide();
		$('#panel_gallery_wrap').hide();
		$('#subtitle_wrap').hide();
		$('#link_title_wrap').hide();
		$('#panel_flash_wrap').hide();
		$('#youtube_url_wrap').hide();
		
	}
	
}

var currentFormIndex = 0;

function initAjaxImageUploadForm(){
	
	$('form.frmAddImage').ajaxForm(function(strHtml){
		
		var frm = $('form.frmAddImage:eq('+currentFormIndex+')');
		
		$('h3.status',frm).html('Complete');
		
		$('div.fieldset_inner',frm).html(strHtml);
		currentFormIndex++;
		
		if(currentFormIndex < $('form.frmAddImage').length){
			SubmitForm(currentFormIndex);
		}else{
			currentFormIndex = 0;
			$('#form_buttons').hide();
			$('#form_complete_buttons').show();
		}
				
	});
}

function SubmitForm(formIndex){
	var frm = $('form.frmAddImage:eq('+formIndex+')');
	$('h3.status',frm).html('Uploading...');
	$(frm).submit();
}


jQuery.fn.swap = function(b){
    b = jQuery(b)[0];
    var a = this[0];
    var t = a.parentNode.insertBefore(document.createTextNode(''), a);
    b.parentNode.insertBefore(a, b);
    t.parentNode.insertBefore(b, t);
    t.parentNode.removeChild(t);
    return this;
};

maxValue = function(array) 
{ 
	mxm = array[0]; 
	for (i=0; i<array.length; i++) 
	{ 
		if (array[i]>mxm) 
		{ 
			mxm = array[i];
		}
	} 
	return mxm;
};

function showGoogleMap(mapLong, mapLat, pointerClick){
	
	var map = new GMap(document.getElementById("googleMap"));
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
	map.centerAndZoom(new GPoint(mapLong, mapLat), 3);
	map.setMapType(G_HYBRID_MAP);
	gMarker = new GMarker(map.getCenter());
	map.addOverlay(gMarker);
	
	if(pointerClick == 'yes'){
		GEvent.addListener(map, 'click', function(overlay, point){
			map.clearOverlays();
			$('#coord_long').val(point.toUrlValue(9).split(',')[1]);
			$('#coord_lat').val(point.toUrlValue(9).split(',')[0]);
			map.addOverlay(new GMarker(point));
		});
	}

	$('#find_postcode').click(function(){
		
		var value = $(this).val();
		
		$(this).val('Searching...').attr('disabled','disabled');
		
		var postcode = $('#map_postcode').val();
		$.get('p/admin.getlonglat.php?postcode='+postcode,function(longlat){
			
			if(longlat != 'failed'){
				
				var coord_long = longlat.substr(0,longlat.indexOf(','));
				var coord_lat = longlat.substr(parseInt(longlat.indexOf(',')+1));
				$('#coord_long').val(coord_long);
				$('#coord_lat').val(coord_lat);
				
				
				var pointer = new GLatLng(coord_lat, coord_long);
				map.panTo(pointer);
				map.clearOverlays();
				map.addOverlay(new GMarker(pointer));
				
				
				
			}else{
				alert('Error. Postcode not found.');
			}
			
			$('#find_postcode').val(value).attr('disabled','');
		});
		
	});
	
}

function opaqueBackgrounds(){
	if($.browser.msie){
		$('div.panel_type2_image_wrap span').css({
			background:'transparent',
			filter:'progid:DXImageTransform.Microsoft.gradient(startColorstr=#99FFFFFF,endColorstr=#99FFFFFF)',
			zoom:1
		});
	}else{
		$('div.panel_type2_image_wrap span').css('background','rgba(255,255,255,0.6)');
	}
}

function resetRowStyles(){
	if($('tr.row_style2').length > 0){
		$('tbody tr:even').attr('class','row_style2');
		$('tbody tr:odd').attr('class','row_style1');
	}
}