var $j = jQuery.noConflict();

function indexOf(ary, e)
{
	for( var i = 0; i < ary.length; i++ )
	{
		if( ary[i] == e ) return i;
	}

	return -1;
};

var display_movie = function(data)
{
	$j("#video_list").html('');

	$j.each	(data.movies, function(index, movie)
	{
		$j("#video_list").append(
			'<div class="thumb">' +
			movie.name + '<br />' + movie.first_name + ' ' + movie.last_name + '<br />' +
			'	<a class="fancy" title="Play this ad" href="#player_container" rel="' + movie.movie + '" id="movie_id_' + movie.movie_id + '"><img border="0" width="180" height="120" src="' + movie.thumbnail + '" /></a><br />' +
			'	<a title="Select this ad and go to next step" href="target/ad/with/' + movie.movie_id + '" class="orange">select this ad</a>' +
			'</div>'
		);
	});

//	$j(".thumb img").flicker();

	$j("a.fancy").fancybox({
		frameWidth: 720,
		frameHeight: 524,
		hideOnContentClick: false,
		callbackOnStart: function()
		{
		},
		callbackOnShow: function()
		{
			$j("#fancy_player").attr('href', movie_clip);
			$j("#fancy_player").flowplayer("/swf/flowplayer/flowplayer-3.1.5.swf");
		}
	});

	$j("a.fancy").mouseover(function(event)
	{
		movie_clip = $j(this).attr('rel');
		$j("#select_this_ad").attr('href', 'target/ad/with/' + $j(this).attr('id').replace(/movie_id_/, ''));
	});	

	var page_html = '';
	for(var i = 1; i <= data.total_page; i++)
	{
		if (data.page == i)
		{
			page_html += '<a href="/buy/movies/page/' + i + '/keyword/" class="page cur_page">' + i + '</a> ';
		}
		else
		{
			page_html += '<a href="/buy/movies/page/' + i + '/keyword/" class="page">' + i + '</a> ';
		}
	}
	$j("#video_list_pagenation").html('');
	$j("#video_list_pagenation").html(page_html);

	$j("a.page").click(function(event)
	{
		$j.getJSON($j(this).attr('href') + $j("#keyword").val(), display_movie);

		return false;
	});
};

$j(document).ready(function()
{
	var movie_clip = '';

	$j.getJSON("/buy/movies/page/1", display_movie);


	$j("#ad_search_inner").submit(function(event)
	{
		$j.getJSON("/buy/movies/page/1/keyword/" + $j("#keyword").val(), display_movie);

		return false;
	});
	

	$j("#search_vg").click(function(event)
	{
		if( $j("#zip_code").val().length != 5 )
		{
			alert("please enter zip code");
			return;
		}

		$j(this).val('Searching...');
		$j(this).attr('disabled', 'disabled');

		// create market list
		$j.getJSON("https://api.saysme.tv/0ec4c274b60eaf53c0570bed9a2e655d/videographer?zipcode=" + $j("#zip_code").val() + "&format=jsonp&jsonpcallback=?", function(data)
		{
			var html = "";

			if( data.count == 0 )
			{
				alert("No videographer found in the area.");
				$j('#search_vg').val('Search Videographer');
				$j('#search_vg').removeAttr('disabled');
				return;
			}

			$j.each(data.markets, function(i, market)
			{
				html += "<h2>Market: " + market.city_name + "</h2>";
				var show = false;
				var emails = [];

				$j.each(market.cablezones, function(j, cablezone)
				{
					if( cablezone.videographers.length > 0 )
					{
						if( cablezone.recommend == 1 )
						{
							html += '<h3>Your Local Cable Zone - ' + cablezone.cablezone_name + '</h3>';
							html += '<p class="italic">Here are your local videographers:</p>';
							html += '<table class="listings"><tr><th>Name</th><th>Company</th><th>Phone</th><th>Email</th><th>Website</th></tr>';
						}
						else if( show != true )
						{
							html += '<p class="italic">Here are videographers in the general ' + market.city_name + ' area:</p>';
							html += '<table class="listings"><tr><th>Name</th><th>Company</th><th>Phone</th><th>Email</th></tr>';

							show = true;
						}

						$j.each(cablezone.videographers, function(k, videographer)
						{
							if( indexOf(emails, videographer.email) < 0 )
							{
								html += "<tr><td>" + videographer.name + "</td>";
								html += "<td>" + videographer.company_name + "</td>";
								html += "<td>" + videographer.phone + "</td>";
								html += "<td>" + videographer.email + "</td>";
								html += "<td>" + videographer.web + "</td></tr>";
								emails.push(videographer.email);
							}
						});

						if( cablezone.recommend == 1 )
						{
							html += "</table>";
						}
					}
				});

				if( show == true )
				{
					html += "</table>";
				}
			});
			$j("#videographer_result").html(html);
			$j("#videographer_result").show("fast");

			$j('#search_vg').val('Search Videographer');
			$j('#search_vg').removeAttr('disabled');
		});

	});
});