$(document).ready(function(){

	/** Navigation **/
	$('#nav_submitareview').addClass('highlight');

	/** Index page **/
	$('#readmore').click(function(){
		$('#slide').slideToggle('fast');
		if($(this).hasClass('up'))
		{
			$(this).removeClass('up');
			$(this).text('Read more\u2026');
		}
		else
		{
			$(this).addClass('up');
			$(this).text('Read less\u2026');
		}
		return false;
	});

	/** Twitter box **/
	$('#tweet').each(function(){
		$('#tweet').tweet({
			join_text: 'auto',
			username: 'agentright',
			avatar_size: 48,
			count: 2,
			auto_join_text_default: 'we said,',
			auto_join_text_ed: 'we',
			auto_join_text_ing: 'we were',
			auto_join_text_reply: 'we replied',
			auto_join_text_url: 'we were checking out',
			loading_text: 'loading tweets...',
			template: '{text} {time}'
		});
		//$.getScript('//platform.twitter.com/widgets.js');

		$('.twitter-follow-button').attr('data-width','240px');

		(function(){
			var twitterWidgets = document.createElement('script');
			twitterWidgets.type = 'text/javascript';
			twitterWidgets.async = true;
			twitterWidgets.src = '//platform.twitter.com/widgets.js';
			document.getElementsByTagName('head')[0].appendChild(twitterWidgets);
		})();
	});




	/** Search input box functions **/
	$('#search_agent').autocomplete({
		source: app_baseurl+'/search-for-agents/by-agent/',
		minLength: 2,
		select: function(event, ui){ui.item ? $('#search_agent_id').val(ui.item.id) : $('#search_agent_id').val('0')}
	});

	$('#search_town').autocomplete({
		source: app_baseurl+'/search-for-agents/by-town/',
		minLength: 2,
		select: function(event, ui){ui.item ? $('#search_town_id').val(ui.item.id) : $('#search_town_id').val('0')}
	});

	$('#search_branch').autocomplete({
		source: function(request, response){
			$.ajax({
				url: app_baseurl+'/search-for-agents/by-branch/',
				data:{
					term: request.term,
					company:  $('#search_agent_id').val()
				},
				dataType: "json",
				success: function(data){
					response($.map(data, function(item){
						return{
							label: item.value,
							value: item.name,
							id: item.id
						}
					}));
				}
			});
		},
		minLength: 1,
		select: function(event, ui){ui.item ? $('#search_branch_id').val(ui.item.id) : $('#search_branch_id').val('0')}
	});

	/** FAQ page **/
	$('.faq').click(function() {
		$(this).find('.faq_content').slideToggle();
		$(this).find('h2').toggleClass('show');
	});

	/** Submit review page **/
	$('#stars_wrapper').each(function(){
		$('#stars_wrapper').stars({
			cancelShow: false,
			disableValue: false
		});
	});

	var title;
	$(".info_hover").hover(function(){
		var pimg = $(this).children('img');
		title = $(pimg).attr('title');
		var html = '<div class="info_text">'+ title +'</div>';
		$(pimg).attr('title', '');
		$(this).append(html);
	},function(){
		$(".info_text").remove();
		$(this).children('img').attr('title', title);
		title = '';
	});
	$(".info_hover").click(function(){
		return false;
	});

	/** Social media buttons **/
	$('.addthis_button_google_plusone').attr('g:plusone:annotation', 'none').attr('g:plusone:size', 'standard');
	$.getScript("http://s7.addthis.com/js/250/addthis_widget.js#pubid=xa-4ed8c4e66cbeee5e");
	
	/** New window links **/
	$('.new-window').attr('onclick','window.open(this.href);return false;');

});

