$(document).ready(function() {

	jQuery.each(jQuery.browser, function(i) {
		if ($.browser.safari) {
			$('#contentHolder').css("min-height", "1000px");
		}
	});
	$("#pageflip").hover(function() { // On hover...
				$("#pageflip img , .msg_block").stop().animate( { // Animate
																	// and
																	// expand
																	// the image
																	// and the
																	// msg_block
																	// (Width +
																	// height)
							width : '307px',
							height : '319px'
						}, 500);
			}, function() {
				$("#pageflip img").stop() // On hover out, go back to original
											// size 50x52
						.animate( {
							width : '135px',
							height : '148px'
						}, 220);
				$(".msg_block").stop() // On hover out, go back to original
										// size 50x50
						.animate( {
							width : '135px',
							height : '142px'
						}, 200); // Note this one retracts a bit faster (to
									// prevent glitching in IE)
		});

	$('#brand_input').focus();

	var hashCode = getHashCode(); // retrive hash value
		// submit hash value if it exist
		if (hashCode != "" && hashCode != null) {
			$('#brand_input').val(hashCode);
			// document.title = hashCode + ' - TweetFeel Twitter Sentiment -
			// Twitter Search with Feelings about anything popular.';
			brandSubmit();
		}

		$('#brand_input_form').submit(brandSubmit);
		$('#pause').click(function() {
			var param = $(this).attr('param');
			if (param == '0') {
				pause();
			} else if (param == '1') {
				resume();
			}
		});
		$('#gg').mouseenter(function() {
			$('#gg_span').show();
		});
		$('#gg').mouseleave(function() {
			$('#gg_span').hide();
		});
		$('.topic').live('click', function() {
			var topic = $(this).text();
			$('#brand_input').val(topic);
			$('#brand_input_form').submit();

		});
		getTrendingTopics();
	});

var at = new Atwitude();

function brandSubmit(event) {
	at.addCurrent();
	$('#info').empty();

	if (event != null) {
		event.preventDefault();
	}

	var brand_name = $('#brand_input').val();
	brand = jQuery.trim(brand_name);
	// If brand is twitter username, remove leading @
	if (brand.charAt(0) == '@') {
		brand = brand.substring(1);
	}
	if (brand == '') {
		$('#loader').hide();
		$('#info')
				.html(
						'<p class="splash_text">Please include a brand to search for.</p>');
		return 0;
	}
	if (pageTracker) {
		pageTracker._trackPageview('/keyword/' + brand);
	}
	at.showLoader();
	$('#pause').show();

	at.setBrand(brand);
	at.resetSearch();
	doSearch(at.getCurrent());

	// unique url
	var input = $('#brand_input').val().replace(/ /g, "_");
	// document.title = $('#brand_input').val() + ' - TweetFeel Twitter
	// Sentiment - Twitter Search with Feelings about anything popular.';
	document.title = 'What do tweeple think about ' + $('#brand_input').val() + '? Check out Tweetfeel twitter sentiment here ';
	location.hash = input;
	var niceUrl = new String(location);
	niceUrl = niceUrl.replace("#", "%23");
	$('#addthis').attr(
			'href',
			'http://api.addthis.com/oexchange/0.8/forward/twitter/offer?title='
					+ document.title + '&url=' + niceUrl
					+ '&username=xa-4bbb6c8f7d9d7636');

	return 1;
}

function pause() {
	if (at.hasBrand()) {
		$('#pause').attr('param', '-1');
		$('#pause').attr('alt', 'Resume Stream');
		$('#pause').attr('title', 'Resume Stream');
		$('#pause').attr('src', '/assets/images/play.jpg');
		at.pauseStream();
		setTimeout("$('#pause').attr('param', '1');", 2000);
	}
}
function resume() {
	if (at.hasBrand()) {
		$('#pause').attr('param', '-1');
		$('#pause').attr('src', '/assets/images/pause.jpg');
		$('#pause').attr('alt', 'Pause Stream');
		$('#pause').attr('title', 'Pause Stream');
		at.resumeStream();
		setTimeout("$('#pause').attr('param', '0');", 2000);
	}
}

function doSearch(curr) {
	at.startSearch(curr);
}

function getTrendingTopics() {
	$
			.getJSON(
					'./trends.php',
					function(data) {
						jQuery
								.each(
										data.trends,
										function(i, trend) {
											if (trend.name.charAt(0) != '#') {
												$('#trends ul')
														.append(
																'<li><a class="topic" href="javascript:;">' + trend.name + '</a></li>');
											}
										});
					});
}

function getHashCode() {
	var strHref = window.location.href;
	if (strHref.indexOf("#") > -1) {
		var strQueryString = strHref.substr(strHref.indexOf("#") + 1)
				.toLowerCase();
		return strQueryString.replace(/_/g, " ").replace(/%27/g, "'").replace(
				/%22/g, "\"");
	}

}
