var searchTabs, accordionID, regionID, sectionID;

function init()
	{
	jQuery( '.rubric, .gt' ).corner();
	setTabs();
	setSearch();
	setToolTip();
	//setupZoom();
	setCursor();
	}

function setCursor()
	{
	jQuery( "#ajaxLoading" ).bind
 		(
 		"ajaxSend",
 		function( event )
 			{
			doc = jQuery( window );
			e = jQuery( this );

			topPos = ( doc.height() - e.height() ) + doc.scrollTop() - 30 + "px";
			leftPos = ( doc.width() - e.width() ) + doc.scrollLeft() - 40 + "px";

			jQuery( this ).css( { top: topPos, left:leftPos } ).fadeIn();
			}
		).bind(
		"ajaxComplete",
		function()
			{
			jQuery( this ).fadeOut();
			}
		).corner().hide();
	}

function getDocs()
	{
	secureCode = jQuery( '#secureCode' ).val()

	buttons =
		{
	    Ok: function()
	    	{
	        jQuery( '#secureCode' ).focus();
			jQuery( this ).dialog( "close" );
	    	}
		}

	if( !secureCode )
		{
		setDialog( JS2, "", buttons, "icon-32-stop" ).dialog( "open" );
		}
	else
		{
		jQuery.ajax
			(
				{
				cache: false,
				type: "POST",
				url: controller,
				data: { task: "getDocs", secureCode: secureCode },
				success: showDocs
				}
			);
		}
	}

function showDocs( data )
	{
	jQuery( '#secureCode' ).val( '', '' );
	refreshCaptcha();

	icon = ''
	options = {};

	is_error = data.search( '{error}' ) != -1;

	if( is_error )
		{
		data = data.replace( '{error}', '' );
		icon = 'icon-32-stop';
		}
	else options [ 'title' ] = JS3;

	setDialog( data, options, '', icon ).dialog( "open" ).find( 'a' ).blur();
	setToolTip();
	}

function setRegion()
	{
	jQuery( '#region' ).change
		(
		function( event )
			{
			jQuery( "#city" ).load( httpRoot + 'index.php?option=com_tender&tmpl=component&format=raw&task=city&rid=' + this.value );
			}
		)
	}

function setToolTip()
	{
	jQuery( "*:not(form)[name!=title][id!=title][for!=title][class!=title][title][class!=lightbox]" ).tooltip
		(
			{
			track: true,
		    showURL: false,
		    opacity: 0.9,
		    fixPNG: true,
		    top: -25,
		    left: 10
			}
		);
	}

function setAccordion()
	{
	jQuery( "#" + accordionID ).accordion
		(
			{
			collapsible: true,
			autoHeight: false
			}
		);

	if( regionID )
		{
		scrollTarget = jQuery( "#r" + regionID );
		scrollTarget.click();
		jQuery( "html, body" ).animate( { scrollTop: scrollTarget.offset().top - 230 }, 500 );
		}

	if( sectionID )
		{
		scrollTarget = jQuery( "#s" + sectionID );
		scrollTarget.click();
		jQuery( "html, body" ).animate( { scrollTop: scrollTarget.offset().top - 180 }, 500 );
		}
	}

function setSearch()
	{
	jQuery( "#searchtab" ).click
		(
		function()
			{
			searchTab = jQuery( "#searchtabs" );

			jQuery.cookie( 'searchtab', ( searchTab.css( 'display' ) == 'block' ? 'none' : 'block' ) );

			if( searchTab.css( 'display' ) == 'block' )
				{
				searchTab.fadeOut( 300 );
				}
			else searchTab.fadeIn( 800 );

			jQuery( "#searchtab a" ).blur();
			}
		)

	jQuery( "input[name=searchSubmit]" ).click( fsubmit );
	jQuery( "input[name=searchword]" ).keypress( fsubmit );
	}

function fsubmit( event, ui )
	{
	if( event.type == 'keypress' && event.keyCode != 13 )return;

	tab = jQuery( "#searchtabs div.ui-tabs-panel" )[ searchTabs.data( 'selected.tabs' ) ].id;

	option = 'com_tender';
	task = 'tender';
	method = 'get';

	switch( tab )
		{
		case 'firmsearch':
		task = 'firm';
		break;
		case 'sitesearch':
		option = 'com_search';
		task = 'search';
		method = 'post';
		break;
		}

	form = jQuery( "form[name=search]" );

	jQuery( "input[name=activetab]" ).val( tab );
	jQuery( "input[name=option]" ).val( option );
	jQuery( "input[name=task]" ).val( task );

	form.attr( 'method', method );
	form.submit();
	}

function setTabs()
	{
	searchTabs = jQuery( "#searchtabs" );
	firmTabs = jQuery( "#firmtabs" );
	tenderTabs = jQuery( "#tendertabs" );

	jQuery
		(
		function()
			{
			searchTabs.tabs(
				{
				cookie: { expires: 30 },
				fx: { opacity: 'toggle', duration: 300 }
				}
			);
			firmTabs.tabs(
				{
				cookie: { expires: 30 },
				fx: { opacity: 'toggle', duration: 300 }
				}
			);
			tenderTabs.tabs(
				{
				cookie: { expires: 30 },
				fx: { opacity: 'toggle', duration: 300 }
				}
			);
			}
		);

	searchTabs.bind( 'tabsshow', setEnabled );
	setEnabled();
	}

function setEnabled()
	{
	s = searchTabs.data( 'selected.tabs' );

	for( i = 0; i < searchTabs.tabs( 'length' ); i++ )
		{
		e = jQuery( "#searchtabs div.ui-tabs-panel" )[ i ];
		jQuery( e ).find( 'input, select' ).attr( 'disabled', i != s );
		};
	}

function setDialog( dialogHTML, dialogOptions, dialogButtons, dialogIcon )
	{
	if( !isObject( dialogButtons ) )
		{
		dialogButtons = {};
		dialogButtons[ "Ok" ] = function() { jQuery( this ).dialog( "close" ); };
		}
	//else dialogButtons[ "Cancel" ] = function() { jQuery( this ).dialog( "close" ); };

	options =
		{
		title:JS1,
		autoOpen: false,
		modal: true,
		width:400,
		buttons: dialogButtons,
		closeOnEscape:true,
		open:function( event, ui )
			{
			jQuery( this ).find( 'input' ).blur();

			try
				{
				dialogOnOpen( event, ui );
				}
			catch( e ){ return true; };
			},
		beforeclose:function( event, ui )
			{
			jQuery( this ).dialog( 'destroy' );
			jQuery( this ).remove();
			}
		}

	if( isObject( dialogOptions ) )
		{
		for( i in dialogOptions )
			{
			options[ i ] = dialogOptions[ i ];
			}
		}

	dialogIcon = dialogIcon ? dialogIcon : 'icon-32-info';

	dialogBody = jQuery( '<div>' ).attr( { 'class': 'dialog ' + dialogIcon } );

	if( isObject( dialogHTML ) )
		{
		for( i in dialogHTML )
			{
			dialogBody.append( dialogHTML[ i ] );
			}
		}
	else dialogBody.append( dialogHTML );

	return jQuery( dialogBody  ).dialog( options );
	}

function isObject( obj )
	{
	return ( obj != null && typeof( obj ) == 'object' );
	}

function text( string )
	{
	return document.createTextNode( string );
	}

function add( data )
	{
	var is_error = data.search( '{error}' ) != -1;

	if( is_error )
		{
		data = data.replace( '{error}', '' );
		setDialog( data, '', '', "icon-32-stop" ).dialog( "open" );
		return false;
		}

	setDialog( data, { width: 500 }, '', 'icon-32-question' ).dialog( 'open' );
	}


function extract( e )
	{
	POSTData = {};

	collection = isObject( e ) ? jQuery( e ).find( ":input" ) : jQuery( "#" + e ).find( ":input" );

	for( i = 0; i < collection.length; i++ )
		{
		checked = collection[ i ].checked;
		type = collection[ i ].getAttribute( 'type' );

		if( !checked && ( type == 'checkbox' || type == 'radio' ) )continue;

		if( collection[ i ].name.search( /^.+?\[\]$/ ) != -1 )
			{
			if( !POSTData[ collection[ i ].name ] )POSTData[ collection[ i ].name ] = [];

			POSTData[ collection[ i ].name ].push( collection[ i ].value );
			}
		else POSTData[ collection[ i ].name ] = collection[ i ].value;
		}

	return POSTData;
	}

