var s, o = new Array(), response_flag = false, url_redirect = ''; var withRatings = true; var eleReqArr = new Array(); var downimage = ''; var updatePositions = false;

// url setup
var main_url = 'http://hiveguru.com/';

$(function(){
	$( '#toggle-project-details' ).click(function(){
		$( '#project-details' ).toggle( 'slow' );
	});

	/*$( 'input[type=submit]' ).click(function(){
		$( this ).attr( 'disabled', 'disabled' );
	}).removeAttr( 'disabled' );
	*/

	$( '.bidder-select-group-members' ).css( { 'display' : 'none' } );

	$( '#select-bidder-group' ).change(function(){
		$( '.bidder-select-group-members' ).css( { 'display' : 'none' } );
		$( '#bidder-select-group-member-' + $( this ).val() ).show();
	});

	$( '#bidder-select-group-member-' + $( '#select-bidder-group' ).val() ).show();

	$( '#apply-grp' ).change(function(){
		var memlist = $( this ).val();
		var refpep = $( '#apply-refer-people' ).val();
		if( refpep == 1 && $( this ).val() != 0 ){
			$.ajax({
				type : "POST",
				url : main_url + "projects/fetchGroupMembersAjax",
				data: 'rand=' + Math.random( 1 ) + '&gid=' + memlist,
				dataType : "json",
				beforeSend : function(){
					$( '#group-members' ).hide();
					$( '#group-members-message' ).fadeIn().html( "<p>loading content.. please wait.. </p>" );
				},
				complete: function( xhr ){
					$( '#group-members-message' ).fadeOut( 'slow' ).html( '' );
					var data = eval( '(' + xhr.responseText.substr( 0, xhr.responseText.lastIndexOf( '<!--' ) ) + ')' );
					$( '#group-members' ).html( '' );
					if( data.response === true ){
						var x = len = 0; var s = ''; var pos = $( '#apply-positions' ).html();
						len = data.members.length;
						s += "<div>&nbsp;</div>"
						if( len > 0 ){
							for( ; x < len; x++ ){
								s += "<div><input type=\"checkbox\" name=\"data[Project][members][]\" value=\"" + data.members[x]['id'] + "\" /> <strong>" + data.members[x]['name'] + "</strong> for <select name=\"data[Project][member_apply_for][]\">" + pos +"</select> position with a bid of $ <input type=\"text\" name=\"data[Project][member_bid][]\" value=\"" + data.members[x]['hourly_rate'] + "\" style=\"width: 20px; border: 1px solid #4096ee; padding: 2px 2px 2px 2px;\" /> and time estimation of <input type=\"text\" name=\"data[Project][member_estimation][]\" value=\"1\" style=\"width: 20px; border: 1px solid #4096ee; padding: 2px 2px 2px 2px;\" /></div>";
							}
						} else{
							s += "<div>This Group has no members.</div>";
						}

						$( '#group-members' ).html( s );
					}
					$( '#group-members' ).fadeIn( 'slow' );
				}
			});
		}
		return false;
	});

	$( '.ajaxCategories' ).change(function(){
		var pri = $( '#primaryprofession' ).val();
		var sec = $( '#secondaryprofession' ).val();
		if( pri !== sec ){
			$.ajax({
				type : "POST",
				url : main_url + "categories/fetchCategoriesAjax",
				data: 'rand=' + Math.random( 1 ) + '&pri=' + pri + '&sec=' + sec,
				dataType : "json",
				beforeSend: function(){
					$( '#checklist' ).hide();
					$( '#message-checklist' ).text( 'loading content.. please wait..' ).show();
				},
				success : function( json ){
					var data = eval( '(' + json + ')' );
					if( data.response == true ){
						// createList( _data.primary_skills );
						alert( data.primary_skills[8] );
					}
				},
				error: function( xhr ){
					// alert( xhr.responseText );
					$( '#message-checklist' ).text( '' ).hide();
					$( '#checklist' ).show();
					var data = eval( '(' + xhr.responseText.substr( 0, xhr.responseText.lastIndexOf( '<!--' ) ) + ')' );
					// alert( data.response );
					withRatings = true;
					var __array = new Array();
					if( data.response === true ){
						createList( array_merge( data.primary_skills, __array ), newArray( data.selected ), data.ratings );
					}
				}
			});
		}
		return false;
	});

	$( '#requirement-browser' ).change(function(){
		var prof = $( this ).val();
		withRatings = false;
		if(
			prof > 0
			&& !isNaN( prof )
		){
			$.ajax({
				type : "POST",
				url : main_url + "categories/fetchProfessionsAjax",
				data: 'rand=' + Math.random( 1 ) + '&prof_id=' + prof,
				dataType : "json",
				beforeSend : function(){
					$( '#checklist' ).hide();
					$( '#message-checklist' ).fadeIn().html( "<p>loading content.. please wait.. </p><img src=\"../../images/38.gif\">" );
				},
				complete: function( xhr ){
					$( '#message-checklist' ).fadeOut( 'slow' ).html( '' );
					var data = eval( '(' + xhr.responseText.substr( 0, xhr.responseText.lastIndexOf( '<!--' ) ) + ')' );
					if( data.response === true ){
						createList( data.skills, new Array(), new Array() );
					}
					$( '#checklist' ).fadeIn( 'slow' );
				}
			});
		}
		return false;
	});

	if( $( '#requirement-browser' ) ){
		var prof = $( '#requirement-browser' ).val();
		withRatings = false;
		if(
			prof > 0
			&& !isNaN( prof )
		){
			$.ajax({
				type : "POST",
				url : main_url + "categories/fetchProfessionsAjax",
				data: 'rand=' + Math.random( 1 ) + '&prof_id=' + prof + '&ele_id=',
				dataType : "json",
				beforeSend : function(){
					$( '#checklist' ).hide();
					$( '#message-checklist' ).fadeIn().html( "<p>loading content.. please wait.. </p><img src=\"../../images/38.gif\">" );
				},
				complete: function( xhr ){
					$( '#message-checklist' ).fadeOut( 'slow' ).html( '' );
					var data = eval( '(' + xhr.responseText.substr( 0, xhr.responseText.lastIndexOf( '<!--' ) ) + ')' );
					if( data.response === true ){
						createList( data.skills, new Array(), new Array() );
					}
					$( '#checklist' ).fadeIn( 'slow' );
					if( $( '#requirement-browser-0' ) ){
						var prof = $( '#requirement-browser-0' ).val();
						withRatings = false;
						if(
							prof > 0
							&& !isNaN( prof )
						){
							$.ajax({
								type : "POST",
								url : main_url + "categories/fetchProfessionsAjax",
								data: 'rand=' + Math.random( 1 ) + '&prof_id=' + prof + '&ele_id=0',
								dataType : "json",
								beforeSend : function(){
									$( '#checklist-0' ).hide();
									$( '#message-checklist-0' ).fadeIn().html( "<p>loading content.. please wait.. </p><img src=\"../../images/38.gif\">" );
								},
								complete: function( xhr ){
									$( '#message-checklist-0' ).fadeOut( 'slow' ).html( '' );
									var data = eval( '(' + xhr.responseText.substr( 0, xhr.responseText.lastIndexOf( '<!--' ) ) + ')' );
									if( data.response === true ){
										createReqList( data.skills, new Array(), new Array(), 0 );
									}
									$( '#checklist-0' ).fadeIn( 'slow' );
								}
							});
						}
					}
				}
			});
		}
	}

	$( '#dialog-box-download' ).dialog({
		bgiframe: true,
		autoOpen: false,
		resizable: false,
		height: 180,
		modal: true,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: {
			'Download File?': function() {
				$(this).dialog( 'close' );
				document.location = $( s ).attr( 'href' );
			},
			Cancel: function() {
				$(this).dialog( 'close' );
			}
		}
	});

	$( '#dialog-box-project-application-requests' ).dialog({
		bgiframe: true,
		autoOpen: false,
		resizable: false,
		height: 300,
		width: 500,
		modal: true,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: {
			Cancel: function() {
				$(this).dialog( 'close' );
			}
		}
	});

	$( '#dialog-box-add-skill' ).dialog({
		bgiframe: true,
		autoOpen: false,
		resizable: false,
		height: 180,
		width: 400,
		modal: true,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: {
			Ok : function(){
				$( '#addnewskill' ).submit();
				$(this).dialog( 'close' );
			},
			Cancel: function() {
				$(this).dialog( 'close' );
			}
		}
	});

	$( '#dialog-box-project-form' ).dialog({
		bgiframe: true,
		autoOpen: false,
		resizable: false,
		width: 700,
		height: 500,
		modal: true,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: {
			'Submit': function() {
				// $(this).dialog( 'close' );
				$( 'form:second' ).submit();
			},
			Cancel: function() {
				$(this).dialog( 'close' );
			}
		}
	});

	$( '#dialog-box-project-application-form' ).dialog({
		bgiframe: true,
		autoOpen: false,
		resizable: false,
		width: 450,
		height: 400,
		modal: true,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: {
			'Submit': function() {
				$(this).dialog( 'close' );
				$( 'form:last' ).submit();
			},
			Cancel: function() {
				$(this).dialog( 'close' );
			}
		}
	});

	$( '#dialog-box-bid' ).dialog({
		bgiframe: true,
		autoOpen: false,
		resizable: false,
		height: 400,
		width: 650,
		modal: true,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: {
			Ok : function() {
				// $(this).dialog( 'close' );
				// document.location = $( s ).attr( 'href' );
				/*var pid = $( '#pid' );
				var uid = $( '#uid' );
				var bid = $( '#bid' );
				var mess = $( '#message' );
				var bag = $( '#bid_as_group' );
				var grp = $( '#group' );
				var tf = $( '#estimated_timeframe' );

				if(
					bid.val().length > 0
					&& !isNaN( bid.val() )
				){
					if(
						pid.val().length > 0
						&& !isNaN( pid.val() )
						&& uid.val().length > 0
						&& !isNaN( uid.val() )
						&& tf.val().length > 0
						&& !isNaN( tf.val() ) 
					){
						var c = bag.val();
						var g_c = ( grp.val() ) ? grp.val() : 0;
						$( 'div#bidding-form' ).hide();
						$( 'div#message-bidding' ).html( "<p>sending request to the server.. please wait.. </p><img src=\"../../images/38.gif\">" ).fadeIn( 'slow' );

						$.ajax({
							type : "POST",
							url : main_url + "projects/addBidAjax",
							data: 'rand=' + Math.random( 1 ) + '&uid=' + uid.val() + '&pid=' + pid.val() + '&bid=' + bid.val() + '&mess=' + mess.val() + '&bag=' + c + '&grp=' + g_c + '&tf=' + tf.val(),
							dataType : "json",
							complete: function( xhr ){
								var data = eval( '(' + xhr.responseText.substr( 0, xhr.responseText.lastIndexOf( '<!--' ) ) + ')' );
								response_flag = data.response;
								$( '#dialog-box-bid' ).dialog( 'close' );
								$( 'div#bidding-form' ).show();
								bid.val( '' );
								mess.val( '' );
								$( 'div#message-bidding' ).html( '' ).hide();
								$( '#dialog-box-response-message' ).text( data.message ).dialog( 'open' );
							}
						});
					} else{
						alert( 'Form Error occured.' );
						$( this ).dialog( 'close' );
					}
				} else{
					alert( 'Bid must be a number.' );
				}*/
				// $(this).dialog( 'close' );
				$( 'form:second' ).submit();
				$(this).dialog( 'close' );
			},
			Cancel: function() {
				$(this).dialog( 'close' );
			}
		}
	});

	$( '#dialog-box-requirement' ).dialog({
		bgiframe: true,
		autoOpen: false,
		height: 300,
		width: 550,
		modal: true,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: {
			'Add Requirement' : function(){
				$( '#checklist input' ).each(function(i){
					o += ( this.checked ) ? this.value + "," : '';
				});
				// alert( o.substring( 0, ( o.length - 1 ) ) );
				var hr = $( '#hourly_rate' );
				var he = $( '#hours_estimation' );
				var id = $( '#project_id_professions' );
				var rb = $( '#requirement-browser' );
				var co = $( '#country' );
				var ci = $( '#city' );
				var pos = $( '#position' );
				var rol = $( '#roles' );
				if(
					hr.val().length > 0
					&& !isNaN( hr.val() )
					&& he.val().length > 0
					&& !isNaN( he.val() )
					&& rb.val().length > 0
					&& !isNaN( rb.val() )
					&& co.val().length > 0
					&& ci.val().length > 0
					&& o.length > 0
					&& !isNaN( id.val() )
					&& id.val() > 0
					&& pos.val().length > 0
					&& id.val().length > 0
				){
					$( 'div#profession-form' ).hide();
					$( 'div#message-professions' ).html( "<p>sending request to the server.. please wait.. </p><img src=\"../../images/38.gif\">" ).fadeIn( 'slow' );

					$.ajax({
						type : "POST",
						url : main_url + "projects/addRequirementAjax",
						data: 'rand=' + Math.random( 1 ) + '&skills=' + o + '&hr=' + hr.val() + '&he=' + he.val() + '&pid=' + id.val() + '&rb=' + rb.val() + '&co=' + co.val() + '&ci=' + ci.val() + '&position=' + pos.val() + '&roles=' + rol.val(),
						dataType : "json",
						complete: function( xhr ){
							var data = eval( '(' + xhr.responseText.substr( 0, xhr.responseText.lastIndexOf( '<!' ) ) + ')' );
							response_flag = data.response;
							$( '#dialog-box-requirement' ).dialog( 'close' );
							$( 'div#profession-form' ).show();
							$( 'div#message-professions' ).html( '' ).hide();
							$( '#dialog-box-response-message' ).text( data.message ).dialog( 'open' );
						}
					});
				} else{
					alert( 'Some fields are empty or you did not select any skill in the list.' );
				}
			},
			'Cancel' : function(){
				$( 'div#profession-form' ).show();
				$( 'div#message-professions' ).html( '' ).hide();
				$(this).dialog( 'close' );
				if( $( '#dialog-box-project-form' ) ){
					$( '#dialog-box-project-form' ).dialog( 'open' );
				}
			}
		}
	});

	$( '#dialog-box' ).dialog({
		bgiframe: true,
		autoOpen: false,
		resizable: false,
		height: 160,
		modal: true,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: {
			'Delete selected item?': function() {
				$(this).dialog( 'close' );
				document.location = $( s ).attr( 'href' );
			},
			Cancel: function() {
				$(this).dialog( 'close' );
			}
		}
	});
	

	$( '#dialog-box-response-message' ).dialog({
		bgiframe: true,
		autoOpen: false,
		resizable: false,
		height: 140,
		modal: true,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: {
			Ok: function() {
				$(this).dialog( 'close' );
				if( response_flag ){
					// document.location = window.href;
					document.location = $( '#my-url' ).text();
					// document.location = 'projects';
				}
			}
		}
	});

	$( '#dialog-box-project-invitation' ).dialog({
		bgiframe: true,
		autoOpen: false,
		resizable: false,
		width: 450,
		height: 100,
		modal: true,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: {
			Ok: function() {
				$(this).dialog( 'close' );
			}
		}
	});

	$( '#dialog-box-project-permissions' ).dialog({
		bgiframe: true,
		autoOpen: false,
		resizable: false,
		width: 450,
		height: 300,
		modal: true,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: {
			"Save" : function() {
				var aUl = ( $( '#AllowUpload' ).is(':checked') ) ? 1 : 0;
				var aDl = ( $( '#AllowDownload' ).is(':checked') ) ? 1 : 0;
				var uid = $( '#uid' );
				var ltype = $( '#link_type' );
				var type = $( '#type' );

				$( '#dialog-box-project-permissions' ).dialog( 'close' );
				$( '#dialog-box-processing' ).text( 'processing request.. please wait..' ).dialog( 'open' );
				$.ajax({
					type : "POST",
					url : main_url + "projects/savePermissionsAjax",
					data: 'rand=' + Math.random( 1 ) + '&AllowUpload=' + aUl + '&AllowDownload=' + aDl + '&uid=' + uid.val() + '&ltype=' + ltype.val() + '&type=' + type.val(),
					dataType : "json",
					complete: function( xhr ){
						var data = eval( '(' + xhr.responseText.substr( 0, xhr.responseText.lastIndexOf( '<!--' ) ) + ')' );
						response_flag = data.response;
						$( '#dialog-box-processing' ).dialog( 'close' );
						$( '#dialog-box-response-message' ).text( data.message ).dialog( 'open' );
					}
				});
			},
			Cancel : function(){
				$( '#project-permissions-form' ).hide();
				$(this).dialog( 'close' );
			}
		}
	});

	$( '#dialog-box-grp-project-invitation' ).dialog({
		bgiframe: true,
		autoOpen: false,
		resizable: false,
		width: 450,
		height: 100,
		modal: true,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: {
			Ok: function() {
				$(this).dialog( 'close' );
			}
		}
	});

	$( '#dialog-box-processing' ).dialog({
		bgiframe: true,
		autoOpen: false,
		resizable: false,
		height: 50,
		modal: true,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		}
	});

	$( '#dialog-box-accept-decline-project-invitation' ).dialog({
		bgiframe: true,
		autoOpen: false,
		resizable: false,
		width: 450,
		height: 100,
		modal: true,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: {
			Close: function() {
				$(this).dialog( 'close' );
			},
			"Decline": function() {
				$(this).dialog( 'close' );
				accdecProjectInvite( 0 );
			},
			"Accept": function() {
				$(this).dialog( 'close' );
				accdecProjectInvite( 1 );
			}
		}
	});

	$( '#dialog-box-grp-accept-decline-project-invitation' ).dialog({
		bgiframe: true,
		autoOpen: false,
		resizable: false,
		width: 450,
		height: 100,
		modal: true,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: {
			Close: function() {
				$(this).dialog( 'close' );
			},
			"Decline": function() {
				$(this).dialog( 'close' );
				accdecProjectInvite( 0 );
			},
			"Accept": function() {
				$(this).dialog( 'close' );
				accdecProjectInvite( 1 );
			}
		}
	});

	$( '#dialog-box-project-details' ).dialog({
		bgiframe: true,
		autoOpen: false,
		resizable: true,
		height: 400,
		width: 600,
		modal: true,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: {
			Ok: function() {
				$(this).dialog( 'close' );
			}
		}
	});

	$( '#dialog-box-assign-user-position' ).dialog({
		bgiframe: true,
		autoOpen: false,
		resizable: true,
		height: 100,
		modal: true,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: {
			Ok: function() {
				$(this).dialog( 'close' );
				$( '#dialog-box-project-invite' ).text( 'Sending request to the server.. Please wait..' ).dialog( 'open' );
				var url = $( '#buffer_url' ).val().replace( /#/g, '' ).split( '+' );
				var pos = $( '#position_select' ).val();
				$.ajax({
					type : "POST",
					url : main_url + "projects/sendProjectInviteAjax",
					data: 'rand=' + Math.random( 1 ) + '&pid=' + url[0] + '&uid=' + url[1] + '&type=' + url[2] + '&pname=' + url[3] + '&pos=' + pos,
					dataType : "json",
					complete: function( xhr ){
						var data = eval( '(' + xhr.responseText.substr( 0, xhr.responseText.lastIndexOf( '<!--' ) ) + ')' );
						response_flag = data.response;
						$( '#dialog-box-project-invite' ).dialog( 'close' );
						$( '#dialog-box-response-message' ).text( data.message ).dialog( 'open' );
					}
				});
			},
			Close : function(){
				$(this).dialog( 'close' );
			}
		}
	});

	$( '.project-application-requests-open-message' ).each(function(i){
		$(this).click(function(e){
			e.preventDefault();
			$( '#toggle-applications-requests-' + i ).toggle( 'slow' );
		});
	});

	$( '.remove-link' ).each(function(i){
		$(this).click(function(e){
			e.preventDefault();
			s = this;
			$( '#dialog-box' ).dialog( 'open' );
		});
	});

	$( '.accept-decline-project-invitation' ).each(function(i){
		$(this).click(function(e){
			e.preventDefault();
			s = this;
			url_redirect = $(this).attr( 'href' );
			$( '#dialog-box-project-invitation' ).dialog( 'close' );
			$( '#dialog-box-accept-decline-project-invitation' ).dialog( 'open' );
		});
	});

	$( '.grp-accept-decline-project-invitation' ).each(function(i){
		$(this).click(function(e){
			e.preventDefault();
			s = this;
			url_redirect = $(this).attr( 'href' );
			$( '#dialog-box-project-invitation' ).dialog( 'close' );
			$( '#dialog-box-grp-accept-decline-project-invitation' ).dialog( 'open' );
		});
	});

	$( '#project-invitations' ).click(function(){
		$( '#dialog-box-project-invitation' ).dialog( 'open' );
	});

	$( '#grp-project-invitations' ).click(function(){
		$( '#dialog-box-grp-project-invitation' ).dialog( 'open' );
	});

	$( '.download-link' ).each(function(i){
		$(this).click(function(e){
			e.preventDefault();
			s = this;
			$( '#dialog-box-download' ).dialog( 'open' );
		});
	});

	$( '.send-project-invite-link' ).each(function(i){
		$(this).click(function(e){
			e.preventDefault();
			updatePositions = true;
			$( '#buffer_url' ).val( $(this).attr( 'href' ) );
			var url = $(this).attr( 'href' ).replace( /#/g, '' ).split( '+' );
			$( '#dialog-box-project-application-requests' ).dialog( 'close' );
			$( '#position_select' ).html( "<option value=\"0\">loading data..</option>" );
			$( '#dialog-box-assign-user-position' ).dialog( 'open' );
			$.ajax({
				type : "POST",
				url : main_url + "projects/updatePositionsAjax",
				data: 'rand=' + Math.random( 1 ) + '&pid=' + url[0],
				dataType : "json",
				complete: function( xhr ){
					var data = eval( '(' + xhr.responseText.substr( 0, xhr.responseText.lastIndexOf( '<!--' ) ) + ')' );
					var s = '';
					response_flag = data.response;
					
					if( data.response ){
						for( p in data.records ){
							s += "<option value=\"" + data.records[p]['ProjectRequirement']['id'] + "\">" + data.records[p]['ProjectRequirement']['position'] + "</option>";
						}
						$( '#position_select' ).html( s );
					} else{
						$( '#dialog-box-assign-user-position' ).dialog( 'close' );
						$( '#dialog-box-response-message' ).text( data.message ).dialog( 'open' );
					}
				}
			});
		});
		/*
		$(this).click(function(e){
			e.preventDefault();
			s = this;
			// process URL
			var url = $(this).attr( 'href' ).replace( /#/g, '' ).split( '+' );
			$.ajax({
				type : "POST",
				url : main_url + "projects/sendProjectInviteAjax",
				data: 'rand=' + Math.random( 1 ) + '&pid=' + url[0] + '&uid=' + url[1] + '&type=' + url[2] + '&pname=' + url[3],
				dataType : "json",
				beforeSend : function(){
					$( '#dialog-box-project-invite' ).text( 'Sending request to the server.. Please wait..' ).dialog( 'open' );
				},
				complete: function( xhr ){
					var data = eval( '(' + xhr.responseText.substr( 0, xhr.responseText.lastIndexOf( '<!--' ) ) + ')' );
					response_flag = data.response;
					$( '#dialog-box-project-invite' ).dialog( 'close' );
					$( '#dialog-box-response-message' ).text( data.message ).dialog( 'open' );
				}
			});
		});*/
	});

	$( '.project-details-link' ).each(function(i){
		$(this).click(function(e){
			e.preventDefault();
			s = this;
			// $( '#dialog-box-response-message' ).dialog( 'open' );
		});
	});

	$( '.requirement-link' ).each(function(i){
		$(this).click(function(e){
			e.preventDefault();
			s = this;
			$( '#dialog-box-requirement' ).dialog( 'open' );
			if( $( '#dialog-box-project-form' ) ){
				$( '#dialog-box-project-form' ).dialog( 'close' );
			}
		});
	});

	$( '#add-skill-link' ).click(function(e){
		e.preventDefault();
		$( '#dialog-box-add-skill' ).dialog( 'open' );
	});

	$( '.bid-link' ).each(function(i){
		$(this).click(function(e){
			e.preventDefault();
			s = this;
			$( '#dialog-box-bid' ).dialog( 'open' );
		});
	});
	
	$( '.project-permissions-link' ).each(function(i){
		$(this).click(function(e){
			e.preventDefault();
			s = this;
			$( '#dialog-box-project-permissions' ).dialog( 'open' );
			// $( '#project-permissions' ).text( $(this).attr( 'href' ) );
			var url = $(this).attr( 'href' ).replace( /#/g, '' ).split( '+' );
			$( '#project-permissions-form' ).hide();
			$( '#project-permissions' ).text( 'processing request.. please wait..' ).show();

			$.ajax({
				type : "POST",
				url : main_url + "projects/fetchPermissionsAjax",
				data: 'rand=' + Math.random( 1 ) + '&uid=' + url[0] + '&link_type=' + url[1] + '&type=' + url[2],
				dataType : "json",
				complete: function( xhr ){
					var data = eval( '(' + xhr.responseText.substr( 0, xhr.responseText.lastIndexOf( '<!--' ) ) + ')' );
					response_flag = data.response;
					var ret = data.data_return;
					var len = data.data_return.length;
					if( len > 0 ){
						var html = '';

						for( var x = 0; x < len; x++ ){
							html += "<input type=\"checkbox\" id=\"" + ret[x]['Configuration']['field'] + "\" " + ( ( ret[x]['Configuration']['value'] == 1 ) ? 'checked' : '' ) + " /> " + ret[x]['Configuration']['field'] + "<br />";
						}
	
						html += "<input type=\"hidden\" id=\"uid\" value=\"" + url[0] + "\" />";
						html += "<input type=\"hidden\" id=\"link_type\" value=\"" + url[1] + "\" />";
						html += "<input type=\"hidden\" id=\"type\" value=\"" + url[2] + "\" />";

						$( '#project-permissions' ).hide();
						$( '#project-permissions-form' ).html( html ).show( 'slow' );
					} else{
						response_flag = false;
						$( '#dialog-box-project-permissions' ).dialog( 'close' );
						$( '#dialog-box-response-message' ).text( 'No Permission has been set for this User / Group.' ).dialog( 'open' );
					}
				}
			});
		});
	});

	$( '.project-details-link' ).each(function(i){
		$(this).click(function(e){
			e.preventDefault();
			$( '#details-content' ).html( $( '#project-details-' + i ).html() );
			$( '#dialog-box-project-details' ).dialog( 'open' );
		});
	});

	$( '.bidding-options' ).each(function(i){
		$( this ).click(function(e){
			e.preventDefault();
			$( '#toggle-options-' + i ).toggle( 'slow' );
		});
	});

	$( '.project-application-link' ).each(function(i){
		$( this ).click(function(e){
			e.preventDefault();
			$( '#dialog-box-bid' ).dialog( 'open' );
			var __url = $( this ).attr( 'href' ).replace( /#/g, '' ).split( '+' );
			var pid = __url[0];
			var eleid = __url[2];
			var oid = __url[1];

			// create options
			var n = new Array();
			var x = len = 0;
			var opporlist = $( '#opporlist-' + eleid ).val().split( ',' );
			$( '#apply-positions' ).text( '' );
			$( '#lpid' ).val( pid );
			$( '#luid' ).val( oid );
			for( len = opporlist.length; x < len; x++ ){
				if( !in_array( opporlist[x], n ) ){
					n.push( opporlist[x] );
					$( '#apply-positions' ).append( '<option value=\'' + opporlist[x] + '\'>' + opporlist[x] +'</option>' );
				}
			}
		});
	});

	$( '#project-form-link-opener' ).click(function(){
		$( '#dialog-box-project-form' ).dialog( 'open' );
	});

	$( '#project-application-requests-link' ).click(function(){
		$( '#dialog-box-project-application-requests' ).dialog( 'open' );
	});

	$( '.toggle-resource-information' ).each(function(i){
		$( this ).click(function(){
			$( '#resource-information-' + i ).toggle( 'slow' );
			var s = $( 'img#image-toggle-' + i );
			if( downimage.length == 0 ){
				downimage = s.attr( 'src' );
			}
			s.attr( 'src', ( s.attr( 'src' ).lastIndexOf( 'arrow_down.png' ) > -1 ) ? s.attr( 'src' ).substr( 0, s.attr( 'src' ).length - s.attr( 'src' ).lastIndexOf( 'arrow_down.png' ) ) + 'img/arrow_up.png' : downimage );
		});
	});

	$( '.remove-selected' ).click(function(e){
		var type = $( this ).attr( 'id' ).replace( 'remove-selected-', '' )
		var b = $( '.checkbox-message-' + type );
		var c = 0;
		e.preventDefault();
		b.each(function(i){
			if( $( this ).is( ':checked' ) ){
				c++;
			}
		});
		if( c == 0 ){
			response_flag = false;
			$( '#dialog-box-response-message' ).text( 'You should check at least one message to be able to execute this request.' ).dialog( 'open' );
		} else{
			var ids = '';
			b.each(function(i){
				if( $( this ).is( ':checked' ) ){
					ids += $( this ).val();
					ids += ( b.length > ( i + 1 ) ) ? '-' : '';
				}
			});
			s = $( this );
			s.attr( 'href', s.attr( 'href' ) + '/' + type + '/' + ids );
			$( '#dialog-box' ).dialog( 'open' );
		}
	});

	$( '#bid_for_a_position' ).change(function(){
		if(
			$( '#bid_for_a_position' ).val() == 1
			&& $( '#apply-positions option' ).length > 0
		){
			$( '#bid-for-a-position' ).hide();
		} else if(
			$( '#bid_for_a_position' ).val() == 0
			&& $( '#apply-positions option' ).length > 0
		){
			if( $( '#bid-for-a-position' ).css( 'display' ) == 'none' ){
				$( '#bid-for-a-position' ).show();
			}
		}
	});

	if(
		$( '#bid_for_a_position' ).val() == 0
		&& $( '#apply-positions option' ).length > 0
	){
		$( '#bid-for-a-position' ).show();
	} else if(
		$( '#bid_for_a_position' ).val() == 1
		&& $( '#apply-positions option' ).length > 0
	){
		$( '#bid-for-a-position' ).hide();
	} else{
		$( '#bid-for-a-position' ).hide();
	}

	$( "#add-skill-slider" ).slider({
		range: "max",
		min: 0,
		max: 10,
		value: 0,
		slide: function( event, ui ) {
			$( "#add-skill-input" ).val( ui.value );
			$( "#add-skill-span-rate" ).text( ui.value );
		}
	}).css( { 'width' : '100%' } );

	$( '#skill-selection-select' ).change(function(){
		if( $( this ).val() == 0 ){
			$( '#custom-skill' ).val( '' );
			$( '#custom-skill-cont' ).show();
		} else{
			$( '#custom-skill' ).val( $( this ).find( 'option' ).filter( ':selected' ).text() );
			$( '#custom-skill-cont' ).hide();
		}
	});

	if( $( '#estimated-project-price' ) ){
		computeProjectPrice();
	}
});

function checkUncheckAll( d, prefix, suffix ){
	var s = $( '.' + prefix + suffix ), c = 0;
	if( $( d ).attr( 'id' ).indexOf( 'checkall' ) > -1 ){
		s.each(function(i){
			this.checked = ( $( d ).is( ':checked' ) ) ? true : false;	
		});
	} else{
		s.each(function(i){
			if( $( this ).is( ':checked' ) ){
				c++;
			}
		});
		var suff = suffix.replace( /[^a-zA-Z0-9]/g, '' );
		$( '#' + suff + '-checkall' ).each(function(){
			if( c == 0 ){
				this.checked = false;
			} else if( c == s.length ){
				this.checked = true;
			}
		});
	}
}

function currentlyChecked( d ){
	var __id = $( d ).attr( 'id' );
	var num = __id.replace( /[^0-9]/g, '' );
	if( $( d ).is( ':checked' ) ){
		if( $( '#with-dates-no-' + num ).css( 'display' ) == 'none' ){
			$( 'select[name=\'data[PersonalEmploymentInfo][no_start_date][' + num + '][month]\'] option[value=\'' + $( 'select[name=\'data[PersonalEmploymentInfo][start_date][' + num + '][month]\']' ).val() + '\']' ).attr( 'selected', 'selected' );
			$( 'select[name=\'data[PersonalEmploymentInfo][no_start_date][' + num + '][year]\'] option[value=\'' + $( 'select[name=\'data[PersonalEmploymentInfo][start_date][' + num + '][year]\']' ).val() + '\']' ).attr( 'selected', 'selected' );
			$( '#with-dates-' + num ).css( { 'display' : 'none' } );
			$( '#with-dates-no-' + num ).css( { 'display' : 'block' } );
		}
	} else{
		if( $( '#with-dates-no-' + num ).css( 'display' ) == 'block' ){
			$( 'select[name=\'data[PersonalEmploymentInfo][start_date][' + num + '][month]\'] option[value=\'' + $( 'select[name=\'data[PersonalEmploymentInfo][no_start_date][' + num + '][month]\']' ).val() + '\']' ).attr( 'selected', 'selected' );
			$( 'select[name=\'data[PersonalEmploymentInfo][start_date][' + num + '][year]\'] option[value=\'' + $( 'select[name=\'data[PersonalEmploymentInfo][no_start_date][' + num + '][year]\']' ).val() + '\']' ).attr( 'selected', 'selected' );
			$( '#with-dates-' + num ).css( { 'display' : 'block' } );
			$( '#with-dates-no-' + num ).css( { 'display' : 'none' } );
		}
	}
}

function accdecProjectInvite( d ){
	var __s = url_redirect.replace( /#/g, '' ).split( '+' );

	$( '#dialog-box-processing' ).text( 'Processing Request.. Please Wait..' ).dialog( 'open' );

	$.ajax({
		type : "POST",
		url : main_url + "projects/accdecProjectInvitationAjax",
		data: 'rand=' + Math.random( 1 ) + '&ad=' + d + '&uid=' + __s[0] + '&pid=' + __s[1] + '&utype=' + __s[2] + '&type=' + __s[3],
		dataType : "json",
		complete: function( xhr ){
			var data = eval( '(' + xhr.responseText.substr( 0, xhr.responseText.lastIndexOf( '<!--' ) ) + ')' );
			response_flag = data.response;
			$( '#dialog-box-processing' ).dialog( 'close' );
			$( '#dialog-box-response-message' ).text( data.message ).dialog( 'open' );
		}
	});
}

function newArray( s2 ){
	var s = new Array();
	for( p in s2 ){
		s.push( p );
	}
	return s;
}

function createList( list, selected, ratings ){
	var d = $( '#checklist' );
	var s = selected_item = '';
	var s_rating = 0;
	var c = 0;
	for( p in list ){
		selected_item = ( in_array( p, selected ) ) ? 'CHECKED' : '';
		s += "<div class='left' style='width: 170px'><input name=\"data[PersonalProfessionalProfile][skills][" + c + "]\" type=\"checkbox\" value=\"" + p + "\" " + selected_item + " /> " + list[p] + "</div>";
		if( withRatings ){
			s += "<div class=\"left\"><div class=\"input select\" style=\"padding-left: 15px;\"><select name=\"data[Rating][rating][" + c + "]\" id=\"select-" + c + "\">";
			s += createOptions( 10 );
			s += "</select></div></div><div style=\"height: 2px; clear: both;\">&nbsp;</div>";
		}
		c++;
	}
	c = 0;
	d.html( s );
	for( p in list ){
		s_rating = ( !isNaN( ratings[p] ) && ratings[p] > -1 ) ? ratings[p] : 0;
		$( 'select#select-' + c ).val( s_rating );
		c++;
	}
}

function createOptions( len ){
	var s = '';
	for( x = 0; x <= len; x++ ){
		s += "<option value=\"" + x + "\">" + x + "</option>";
	}
	return s;
}

function getProfessions( d ){
	var prof = $( d ).val();
	withRatings = false;
	var i = d.id.replace( /[^0-9]/g, '' );

	if(
		prof > 0
		&& !isNaN( prof )
	){
		$.ajax({
			type : "POST",
			url : main_url + "categories/fetchProfessionsAjax",
			data: 'rand=' + Math.random( 1 ) + '&prof_id=' + prof + '&ele_id=' + i,
			dataType : "json",
			beforeSend : function(){
				$( '#checklist-' + i ).hide();
				$( '#message-checklist-' + i ).fadeIn().html( "<p>loading content.. please wait.. </p><img src=\"../../images/38.gif\">" );
			},
			complete: function( xhr ){
				$( '#message-checklist-' + i ).fadeOut( 'slow' ).html( '' );
				var data = eval( '(' + xhr.responseText.substr( 0, xhr.responseText.lastIndexOf( '<!--' ) ) + ')' );
				if( data.response === true ){
					createReqList( data.skills, new Array(), new Array(), i );
				}
				$( '#checklist-' + i ).fadeIn( 'slow' );
			}
		});
	}
	return false;
}

function processPrice( price ){
	price = price.toString();
	var ext = mainP = rev = '';
	if( price.lastIndexOf( '.' ) > -1 ){
		ext = price.substr( price.lastIndexOf( '.' ) + 1 );
		ext = ( ext.length == 1 ) ? ext + '0' : ext;
	}
	mainP = ( price.lastIndexOf( '.' ) > -1 ) ? price.substr( 0, price.lastIndexOf( '.' ) ) : price;
	// reverse str
	rev = strrev( mainP );
	mainP = '';
	var len = rev.length;
	for( var x = 0; x < len; x++ ){
		mainP += rev.charAt( x );
		if(
			( ( x + 1 ) % 3 ) == 0
			&& x > 0
			&& len > ( x + 1 )
		){
			mainP += ',';
		}
	}
	return ( ext != '' ) ? strrev( mainP ) + '.' + ext : strrev( mainP ) + '.00';
}

function strrev( str ){
	var rev = '';
	for( var x = str.length; x >= 0; x-- ){
		if( str.charAt( x ) != '' ){
			rev += str.charAt( x );
		}
	}
	return rev;
}

function computeProjectPrice(){
	var s = $( '.hr-rate' );
	var __s = $( '.hr-est' );
	var x = len = 0;
	var calc = 0;
	for( len = s.length; x < len; x++ ){
		if(
			!isNaN( s[x].value )
			&& !isNaN( __s[x].value )
		){
			calc += ( s[x].value * __s[x].value );
		}
	}
	calc = ( calc.toString().lastIndexOf( '.' ) > -1 ) ? calc.toString().substr( 0, calc.toString().lastIndexOf( '.' ) + 3 ) : calc;
	calc = processPrice( calc );
	$( '#estimated-project-price' ).text( '$' + calc );
	if( $( '#project-budget' ) ){
		$( '#project-budget' ).val( calc );
	}
}

function createReqList( list, selected, ratings, i ){
	var d = $( '#checklist-' + i );
	var s = selected_item = '';
	var s_rating = 0;
	var c = 0;
	for( p in list ){
		selected_item = ( in_array( p, selected ) ) ? 'CHECKED' : '';
		s += "<div class='left' style='width: 170px'><input name=\"data[PersonalProfessionalProfile][skills][" + i + "][" + c + "]\" type=\"checkbox\" value=\"" + p + "\" " + selected_item + " /> " + list[p] + "</div>";
		if( withRatings ){
			s += "<div><div class=\"input select\"><select name=\"data[Rating][rating][" + i + "][" + c + "]\" id=\"select-" + c + "\">";
			s += createOptions( 10 );
			s += "</select></div></div>";
		}
		c++;
	}
	c = 0;
	d.html( s );
	for( p in list ){
		s_rating = ( !isNaN( ratings[p] ) && ratings[p] > -1 ) ? ratings[p] : 0;
		$( 'select#select-' + i + '-' + c ).val( s_rating );
		c++;
	}
}
