
var AJAX_PATH = '/public_panel/includes/ajax/';

$(document).ready(function() {
	
	/**
	 * Header banner rotate
	 */
	if($('#header-banners').length) {
		$('#header-banners').cycle({ 
			fx:    'fade', 
			speed:  2500 
		});
	}

	/**
	 * Profile textareas limit
	 */
	$('.limit-char').each(function() {
		var this_limit = $(this).attr('rel');

		if(this_limit != '') {
			this_limit *= 1;
				
			$(this).jqEasyCounter({
				'maxChars': this_limit,
				'maxCharsWarning': this_limit-10,
				'msgFontSize': '11px',
				'msgFontColor': '#000',
				'msgFontFamily': 'Arial',
				'msgTextAlign': 'left',
				'msgAppendMethod': 'insertAfter'
			});
		}
	});



	/**
	 * Control main menu's submenus
	 */
	$('ul.menu').dropmenu(
		{effect: 'fade', speed: 'fast'}
	);


	/**
	 * Input clear on click, autofill default on blur
	 */
	$('.focus-inp').click(function() {
		if($(this).val() == $(this).attr('rel')) {
			$(this).val('');
		}
	});
	$('.focus-inp').blur(function() {
		if($(this).val() == '') {
			$(this).val($(this).attr('rel'));
		}
	});
			

	/**
	 * Auto expand a collapsed list if it has .collapsed-show-first
	 */
	$('.collapsed-show-first li:first').children('div').show().parent('li').addClass('active');


	/**
	 * Control collapsed lists on homepage
	 */
	$('ul.collapse li a.head').click(function() {
		if($(this).parent('li').hasClass('active')) {
			$(this).parent('li').children('div').slideUp().parent('li').removeClass('active');
		}
		else {
			$('ul.collapse li.active div').slideUp();
			$('ul.collapse li.active').removeClass('active');

			$(this).parent('li').children('div').slideDown().parent('li').addClass('active');
		}

		return false;
	});


	/**
	 * Auto expand a collapsed box on myhome left-hand sidebar .collapsed-show-first
	 */
	$('.myhome-menu li.expandable:first').children('div').show().parent('li').addClass('active');

	/**
	 * Control collapsed box on myhome left-hand sidebar
	 */
	$('ul.myhome-menu li.expandable a.head').click(function() {
		if($(this).parent('li').hasClass('active')) {
			$(this).parent('li').children('div').slideUp().parent('li').removeClass('active');
		}
		else {
			$('ul.myhome-menu li.active div').slideUp();
			$('ul.myhome-menu li.active').removeClass('active');

			$(this).parent('li').children('div').slideDown().parent('li').addClass('active');
		}

		return false;
	});

	/**
	 * Login form
	 */
	$('#frm-login').submit(function() {

		var frm_username = $('#frm_username').val();
		var frm_password = $('#frm_password').val();

		//$('.login-form .error').html('').hide();

		if(frm_username != '' && frm_password != '') {
			$.get(AJAX_PATH + 'login.php', {action: 'login', frm_username: $('#frm_username').val(), frm_password: $('#frm_password').val() }, function(json) {
				if(json.status == true) {
					location.reload();
				}
				else {
					$('.login-form .beige-head').addClass('error').html(json.message);
				}
			}, 'json');
		}
		else {
			$('.login-form .beige-head').addClass('error').html('Please fill in your details');
		}

		return false;
	});

	/**
	 * Filter Media Centre and Events
	 */
	$('.media .filters form').submit(function() {
		var type = $(this).find('[name="type"]').val();
		$(this).find('.btn-submit').hide();
		$(this).find('.loading').show();

		if(type == 'event-details') {
			ajax_script = 'event_list.php';
			html_container = '#events-list';
		}
		else {
			ajax_script = 'media_centre_search.php';
			html_container = '#media-' + type + '-list';
		}

		$.get(AJAX_PATH + ajax_script, $(this).serialize(), function(html) {
			$(html_container).html(html);

			$('.' + type + '-loading').hide();
			$('.' + type + '-btn-submit').show();
		});		
		return false;
	});
});


/**
 * Dummy function
 */
function WI() {
	// nothing
}

/**
 * Member logout
 */
function WBM_memberLogout() {
	$.get(AJAX_PATH + 'login.php', {action: 'logout'}, function(json) {
		if(json.status == true) {
			location.reload();
		}
		else {
			alert('An error occured');
		}
	}, 'json');
}

/**
 * Smooth scroll to the top of the page
 */
function WBM_scrollTop() {
	$('html, body').animate({scrollTop:0}, 'slow');
}

/**
 * Reload captcha image
 */
function WBM_reloadCaptcha() {
	$('#img-captcha').attr('src', $('#img-captcha').attr('src') + Math.random());
}


/**
 * Popup function
 */
function openWindow(url, PageWidth, PageHeight, _scrollbars, _status, _resizable) {
	var popleft = ((document.body.clientWidth - PageWidth) / 2) + window.screenLeft;
	var poptop = ((( document.body.clientHeight - PageHeight) / 2)) + window.screenTop-40;
		
	str = "" + Math.random() + "";
	_unique_id = str.substr(2, str.length);
		
	window.open(url, _unique_id ,"status=" + _status + ",resizable="+_resizable+",scrollbars=" + _scrollbars + ",width=" + PageWidth + ",height=" + PageHeight + ",left=" + popleft + ",top=" + poptop);
}


/**
 * Forgot Password popup
 */
function WBM_forgotPassword() {
	openWindow('/forgot_password.php', '865', '175', 'no', 'no', 'no');
}

/**
 * Email Friend popup
 */
function WBM_emailFriend(id, type) {
	openWindow('/email_friend.php?id=' + id + '&type=' + type, '865', '200', 'no', 'no', 'no');
}


/**
 * Remove from MyWatchList functions
 */
function WBM_confirmRemoveFromWatchlist(id) {
	$('#remove-watchlist-id').val(id);

	$.blockUI({ 
		message: $('#remove-watchlist-modal'),
		css: { 
			backgroundColor: '#000',
			border: '3px solid #B08541',
			padding: '30px 40px'
		}
	}); 
}
function WBM_removeFromWatchlist() {
	id = $('#remove-watchlist-id').val() * 1;

	if(id > 0) {
		$.get(AJAX_PATH + 'remove_watchlist.php', {id: id}, function(html) {
			$('#remove-watchlist-id').val('');
			location.reload();
		});
	}
}
function WBM_cancelRemoveFromWatchlist() {
	$('#remove-watchlist-id').val('');
	$.unblockUI();
}
function WBM_addToWatchlist(id) {
	id = $('#remove-watchlist-id').val() * 1;

	if(id > 0) {
		$.get(AJAX_PATH + 'add_watchlist.php', {id: id}, function(html) {
			location.reload();
		});
	}
}

/**
 * Load Media Centre article content via AJAX
 */
function WBM_mediaArticle(id) {
	$('.announcements-btn-submit').hide();
	$('.announcements-loading').show();

	$.get(AJAX_PATH + 'media_centre_article.php', {id: id}, function(html) {

		$('#media-announcements-list').hide();
		$('#media-announcements-article').html(html).show();
		$('#media-ceo-list').addClass('expanded');

		$('.announcements-loading').hide();
		$('.announcements-btn-submit').show();
	});	
}

/**
 * Close Media Centre article and display back the list
 */
function WBM_closeMediaArticle() {
	$('#media-announcements-article').html('').hide();
	$('#media-announcements-list').show();
	$('#media-ceo-list').removeClass('expanded');

	WBM_scrollTop();
}

/**
 * Load event details
 */
function WBM_eventDetails(id) {
	$('.event-details-btn-submit').hide();
	$('.event-details-loading').show();

	$.get(AJAX_PATH + 'event_details.php', {id: id}, function(json) {

		//$('.event-item').removeClass('highlight');
		//$('#event-item-' + id).addClass('highlight');
		
		$('#event-details-content').html(json.content);

		$('.event-details-loading').hide();
		$('.event-details-btn-submit').show();
	}, 'json');	
}


/**
 * Profile photos handlers
 */
function WBM_profilePhotoOrder(id, direction) {
	$.get(AJAX_PATH + 'profile_photos.php', {action: 'change_order', id: id, direction: direction}, function(html) {
		location.reload();
	});
}

/**
 * Deal - Register Interest
 */
function WBM_registerInterest(id) {
	$.blockUI({ 
		message: 'This will send an email registering your interest to the company and you should receive contact from them within the next 48 hours',
		css: { 
			backgroundColor: '#000000',
			border: '3px solid #B08541',
			padding: '30px 40px',
			color: '#B08541',
			fontSize: '15px'
		}
	}); 

	$.get(AJAX_PATH + 'opportunity.php', {action: 'register_interest', id: id}, function(html) {
		setTimeout('$.unblockUI();', 3000);
	});

}

/**
 * Deal - Download Alert
 */
function WBM_downloadAlert(id) {
	$.get(AJAX_PATH + 'opportunity.php', {action: 'download_alert', id: id}, function(html) {
		// nothing
	});
}


/**
 * Share on Facebook
 */
function facebookShare(url) {
	openWindow('http://www.facebook.com/sharer.php?u=' + url, '580', '320', 'no', 'no', 'no');
}

/**
 * Share on LinkedIn 
 */
function linkedinShare(url, title, summary, source) {
	openWindow('http://www.linkedin.com/shareArticle?mini=true&url=' + url + '&title=' + title + '&summary=' + summary, '650', '400', 'no', 'no', 'no');
}

/**
 * Add/Edit Company Profile board person
 */
function WBM_submitBoardPerson(id) {
	$('.board-response').html('');

	$.get(AJAX_PATH + 'profile_board.php', $('#profile-form').serialize(), function(json) {

		if(json.status == true) {
			$('#board-list').html(json.board_list_html);
			$('.board-response').html('<p class="success">' + json.message + '</p>');
			$('#profile-form .inp').val('');
			$('#person_id').val('');
		}
		else {
			$('.board-response').html('<p class="error">' + json.message + '</p>');
		}

	}, 'json');
}

/**
 * Load board person for edit
 */
function WBM_loadBoardPerson(id) {
	$('.board-response').html('');
	$('#person_id').val(id);

	$.get(AJAX_PATH + 'profile_board.php', {frm_action: 'load', id: id}, function(json) {

		if(json.status == true) {
			$('#frm_name').val(json.frm_name);
			$('#frm_job_title').val(json.frm_job_title);
			$('#frm_qualifications').val(json.frm_qualifications);
			$('#frm_bio_magazine').val(json.frm_bio_magazine);
			$('#frm_bio').val(json.frm_bio);

			WBM_scrollTop();
		}
	}, 'json');
}

/**
 * Order board persons
 */
function WBM_orderBoardPerson(id, ord) {
	$('.board-response').html('');

	$.get(AJAX_PATH + 'profile_board.php', {frm_action: 'order', id: id, ord: ord}, function(json) {

		if(json.status == true) {
			$('#board-list').html(json.board_list_html);
		}
	}, 'json');
}

/**
 * Delete board person
 */
function WBM_deleteBoardPerson(id) {
	$('.board-response').html('');

	$.get(AJAX_PATH + 'profile_board.php', {frm_action: 'delete', id: id}, function(json) {

		if(json.status == true) {
			$('.board-response').html('<p class="success">Member successfully deleted</p>');
			$('#board-list').html(json.board_list_html);

			$('#person_id').val('');
			WBM_scrollTop();
		}
	}, 'json');
}




/**
 * Add/Edit Company Profile research article
 */
function WBM_submitResearchArticle(id) {
	$('.board-response').html('');

	$.get(AJAX_PATH + 'profile_research.php', $('#profile-form').serialize(), function(json) {

		if(json.status == true) {
			$('#research-list').html(json.research_html);
			$('.research-response').html('<p class="success">' + json.message + '</p>');
			$('#profile-form .inp').val('');
			$('#research_id').val('');
		}
		else {
			$('.research-response').html('<p class="error">' + json.message + '</p>');
		}

	}, 'json');
}

/**
 * Load research article for edit
 */
function WBM_loadResearchArticle(id) {
	$('.research-response').html('');
	$('#research_id').val(id);

	$.get(AJAX_PATH + 'profile_research.php', {frm_action: 'load', id: id}, function(json) {

		if(json.status == true) {
			$('#frm_name').val(json.frm_name);
			$('#frm_url').val(json.frm_url);

			WBM_scrollTop();
		}
	}, 'json');
}


/**
 * Delete research article
 */
function WBM_deleteResearchArticle(id) {
	$('.research-response').html('');

	$.get(AJAX_PATH + 'profile_research.php', {frm_action: 'delete', id: id}, function(json) {

		if(json.status == true) {
			$('.research-response').html('<p class="success">Item successfully deleted</p>');
			$('#research-list').html(json.research_html);

			$('#research_id').val('');
			WBM_scrollTop();
		}
	}, 'json');
}
