var introImages = ["/public/img/franchisesite/franchisees/ronplatt_video.jpg",
				   "/public/img/franchisesite/franchisees/valentinos_video.jpg",
				   "/public/img/franchisesite/franchisees/mattduke_video.jpg",
				   "/public/img/franchisesite/franchisees/waters_video.jpg",
				   "/public/img/franchisesite/franchisees/caseyfox_video.jpg",
				   ];

var videos = ["/public/video/ronplatt.mov",
				  "/public/video/valentinos.mov",
				  "/public/video/mattduke.mov",
				  "/public/video/thewaters.mov",
				  "/public/video/caseyfox.mov",
				  ];

$(document).ready(function() {
   	setUpFranchiseeList();
	onFranchiseeOptionClick();

	updateVideoPlayer(introImages[0], videos[0]);
});

function setUpFranchiseeList()
{
	setSelectedFranchisee();

	$("a.video-option").hover(
	function ()
	{
		var $this = $(this);
		removeSelectedState($this);
	},
	function ()
	{
		var $this = $(this);
		setSelectedState($this);
	});

	$(".video-details-container.one").css({"display" : "block"});
}

function setSelectedState($this)
{
	if (!$this.hasClass("selected"))
	{
		$this.css("border-top-color", "#4a4132").next().css("border-top-color", "#4a4132");
		$this.find("span.container").css("color", "#948f86");
		//    $this.find(".arrow, .thumb").removeClass("hover");
		$this.find(".arrow").css({"backgroundPosition" : "0px 0px"})
		$this.find(".thumb").css({"backgroundPosition" : "0px -51px"})

		if ($this.hasClass("last"))
		{
			$this.css("border-bottom-color", "#4a4132");
		}
		setSelectedFranchisee();
	}
}

function removeSelectedState($this)
{
	if (!$this.hasClass("selected"))
	{
		$this.css("border-top-color", "#6a6357").next().css("border-top-color", "#6a6357");
		$this.find("span.container").css("color", "#fff");
		// $this.find(".arrow, .thumb").addClass("hover");

		$this.find(".arrow").css({"backgroundPosition" : "0px -13px"})
		$this.find(".thumb").css({"backgroundPosition" : "0px 0px"})

		if ($this.hasClass("last"))
		{
			$this.css("border-bottom-color", "#6a6357");
		}
	}
}

function setSelectedFranchisee()
{
    $("a.video-option").each(function(e)
	{
    	var $this = $(this);

      	if ($this.hasClass("selected"))
		{
       		$this.css("border-top-color", "#6a6357").next().css("border-top-color", "#6a6357");
        	$this.find("span.container").css("color", "#fff");
        	$this.find(".arrow, .thumb").addClass("hover");

        	if ($this.hasClass("last"))
  			{
        		$this.css("border-bottom-color", "#6a6357");
			}

			var $id = $this.attr('id').match(/\d+$/);
		}
   	});
}

function onFranchiseeOptionClick()
{
    $("a.video-option").click(function(e)
	{
    	var $this = $(this);

      	if ($this.hasClass("selected"))
		{
       		// do nothing
		}
		else
		{
			var name = $this.children('.container').find('span').eq(0).html();			
			pageTracker._trackPageview('/mellow-stories/'+ name);
			
			$("a.video-option").each(function(e)
			{
				if ($(this).hasClass("selected"))
				{
					$(this).removeClass("selected");
					$(this).hover();
				}
			});

			$this.addClass("selected");

			var $id = $(this).attr('id').match(/\d+$/);

			if($id == 1)
			{
				var $newClass = "one";
			}else if( $id == 2)
			{
				var $newClass = "two";
			}
			else if( $id == 3)
			{
				var $newClass = "three";
			}
			else if( $id == 4)
			{
				var $newClass = "four";
			}
			else if( $id == 5)
			{
				var $newClass = "five";
			}

			$(".video-details-container").each(function(e)
			{
				if($(this).css("display") == "block")
				{
					$(this).fadeOut('fast', function(){
						$(".video-details-container." +  $newClass).fadeIn('fast');
					});
				}
			});

			updateVideoPlayer(introImages[$id - 1], videos[$id - 1]);
		}
   	});
}

function updateVideoPlayer(img, video)
{
	$("#videoPlayer").replaceWith('<div id="videoPlayer"></div>');

	var flashvars   = { introImageUrl:img, videoUrl: video, showIntroImage: true };
	var params      = { wmode: "transparent", scale: "noscale", allowScriptAccess: "sameDomain" };
	var attributes  = { id : "videoPlayer" };
	swfobject.embedSWF("/public/flash/franchisesite/videoplayer.swf", "videoPlayer", "387", "217", "9.0", "/public/flash/expressInstall.swf", flashvars, params, attributes);
}