$(document).ready(
	function setupSWF(element, name, fvars) {
		
		// Load the flash object in jquery
		
		
	}
);

$(document).ready(function(){
	// Center the home page entry vertically
	var screen_height = $(this).height();
	var entry_height = 540;
	var top_height = 45;
	//$('#dev_test').html(screen_height);
	if(screen_height > (entry_height + top_height)){
		var mtop = Math.round((screen_height - entry_height)/2) - top_height;
		//alert(mtop);
		$('#home-display #msld').css('marginTop', mtop+'px');
	}
	// Also create a handler for resize of browser window
	$(window).resize(function() {
		var screen_height = $(this).height();
		var entry_height = 540;
		var top_height = 45;
		//$('#dev_test').html(screen_height);
		if(screen_height > (entry_height + top_height)){
			var mtop = Math.round((screen_height - entry_height)/2) - top_height;
			//alert(mtop);
			$('#home-display #msld').css('marginTop', mtop+'px');
		}
		else {
			$('#home-display #msld').css('marginTop', '0px');
		}
	});
	
	$('#house').fadeTo(0,.3, function(){
		//Animation complete
	});
	
	// Slide out for Previous and Next buttons
	$('#lControl')
		.bind('mouseover', function(){
			$(this).animate(
				{marginLeft: '0px'}, 200,function () {
				}
			);
	});
	$('#lControl')
		.bind('mouseleave', function(){
			$(this).animate(
				{marginLeft: '48px'}, 200,function () {
				}
			);
	});
	
	$('#rControl')
		.bind('mouseover', function(){
			$(this).animate(
				{marginLeft: '0px'}, 200,function () {
				}
			);
	});
	$('#rControl')
		.bind('mouseleave', function(){
			$(this).animate(
				{marginLeft: '-48px'}, 200,function () {
				}
			);
	});
	
	
  var slideWidth = 960;   //orginal size 540px
  var slides = $('.slidez');
  
  var numberOfSlides = slides.length;
  var currentPosition = -1;
  var hash_link = location.hash.replace(/#/,'');
  
  var ids = [];
  var types = [];
  var names = [];
  var colors = [];
  var titles = [];
  var pauses = [];
  var anchors = [];
  var fvar_names = [];
  var fvar_str = [];
  var codes = [];
  
  var slide_info, slide_split, id_info, type_info, name_info, color_info, title_info, pause_info, anchor_info, fvar_info, code_info;
  
  
  
  var j=numberOfSlides-1;
  while(j>=0){
	slide_info = $('#home_content_'+j).attr('rel');
	slide_split = slide_info.split("|");
	//alert(slide_info);
	
	id_info = slide_split[0];
	type_info = slide_split[1];
	name_info = slide_split[2];
	color_info = slide_split[3];
	title_info = slide_split[4];
	pause_info = slide_split[5];
	anchor_info = slide_split[6];
	
	code_info = "";
	
	//alert(hp_entry);
	/*type_info = $.ajax({ url: '/11_0812/includes/hpent-util.php', data: {action: 'getEntry', id: id_info, field: 'type'}, type: 'post',async: false }).responseText;
	name_info = $.ajax({ url: '/11_0812/includes/hpent-util.php', data: {action: 'getEntry', id: id_info, field: 'file_name'}, type: 'post',async: false }).responseText;
	color_info = $.ajax({ url: '/11_0812/includes/hpent-util.php', data: {action: 'getEntry', id: id_info, field: 'bgcolor'}, type: 'post',async: false }).responseText;
	title_info = $.ajax({ url: '/11_0812/includes/hpent-util.php', data: {action: 'getEntry', id: id_info, field: 'title'}, type: 'post',async: false }).responseText;
	pause_info = $.ajax({ url: '/11_0812/includes/hpent-util.php', data: {action: 'getEntry', id: id_info, field: 'pause'}, type: 'post',async: false }).responseText;
	anchor_info = $.ajax({ url: '/11_0812/includes/hpent-util.php', data: {action: 'getEntry', id: id_info, field: 'anchor'}, type: 'post',async: false }).responseText;*/
	//slide_split = hp_entry.split("|");
	
	/*type_info = slide_split[2];
	name_info = slide_split[3];
	color_info = slide_split[5];
	title_info = slide_split[1];
	pause_info = slide_split[8];
	anchor_info = slide_split[9];*/
	
	types[j] = type_info;
	names[j] = name_info;
	colors[j] = color_info;
	titles[j] = title_info;
	pauses[j] = pause_info;
	codes[j] = code_info;
	
	// Set the currentPosition to this entry if the anchor link matches
	if(anchor_info == hash_link) currentPosition = j;
	anchors[j] = anchor_info;
	
	fvar_info = $('#home_content_'+j).find('.strSource').attr('rel');
	//fvar_info = slide_split[7];
	fvar_str[j] = fvar_info;
	
	
	if(types[j] == 'swf'){
		$('#home_content_'+j).flash(
			{
				// test_flashvars.swf is the flash document
				swf: names[j],
				data: names[j],
				id: j,
				classid: 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000',
				codebase: 'http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0',
				allowScriptAccess: 'always',
				width: '960',
				height: '540',
				loop: 'false',
				quality: 'high',
				bgcolor: colors[j],
				wmode: 'transparent',
				swLiveConnect: true,
				type: 'application/x-shockwave-flash',
				pluginspage: 'http://www.macromedia.com/go/getflashplayer',
				play: false,
				scale: 'showall',
				hasVersion: 9,
				hasVersionFail: function(options) {
					alert("You must use at least Version 9 of Flash to view this content.");
					return true;	
				},
				expressInstaller: 'expressInstall.swf',
				
				// these arguments will be passed into the flash document
				flashvars: {
					strSource: fvar_str[j]
				}
			}
		);
		/*
		*/
		
	}
	else if(types[j] == 'img'){
		$('#home_content_'+j).prepend('<img src="'+names[j]+'" width="960" height="540" />');
	}
	else if(types[j] == 'html' || types[j] == 'html_video'){
		code_info = $.ajax({ url: '/includes/hpent-util.php', data: {action: 'getEntry', id: id_info, field: 'code'}, type: 'post',async: false }).responseText;
		codes[j] = code_info;
		// If special, we will add the custom HTML code
		$('#home_content_'+j).prepend('<div id="html_'+j+'">'+codes[j]+'</div>');
		//alert(codes[j]);
		
	}
	j--;
  }
	function getFlashMovie(movieName) { 
		var isIE = navigator.appName.indexOf("Microsoft") != -1;
		return (isIE) ? window[movieName] : document[movieName];
		}
		
	function formSend() {
		var text = document.htmlForm.sendField.value;
		getFlashMovie("ExternalInterfaceExample").sendTextToFlash(text);
		}
		
	function getTextFromFlash(str) {
		document.htmlForm.receivedField.value = "From Flash: " + str;
		return str + " received";
		}
		
	// utility..
	function formPlay(index) {
		//var text = document.htmlForm.sendField.value;
		//alert("try play");
		//$('#home_content_'+index).flash(function(){$(this).playVideoFlash()});
		
		$('#home_content_'+index).flash(
			function() {
				this.playVideoFlash();
			}
		);
	}
	function formPause(index) {
		//var text = document.htmlForm.sendField.value;
		//alert("try pause");
		//getFlashMovie(index).pauseVideoFlash();
		//alert('#home_content_'+index);
		$('#home_content_'+index).flash(
			function() {
				this.pauseVideoFlash();
			}
		);
		
	}
	
	// If no anchor tag or no match, the default current position is the latest entry
	if(currentPosition == -1) currentPosition = numberOfSlides-1;
	
	// Initialize the Title of the entry
	$('#controls .control-title').html(titles[currentPosition]);
	
	//formPlay(4);
  /*
  $('#home_content_0').flash(
	function(){
		this.SetVariable('/:strSource', 'rtmp://torque.fcod.llnwd.net/a2292/o21/fkm-fkmagency/utilityv/&movieName=testvide&sharePath=http://www.fkm.com');
	});
*/
  // Remove scrollbar in JS
  $('#sldesContainer').css('overflow', 'hidden');
  $('#sldesContainer').css('position', 'relative');
  $('#sldesContainer').css('width', '960px');
  $('#sldesContainer').css('height', '540px');

  // Wrap all .slides with #slideInner div
  slides
    .wrapAll('<div id="slideInner"></div>')
    // Float left to display horizontally, readjust .slides width
	.css({
      'float' : 'right',
      'width' : slideWidth
    });
	
  // Initialize #slideInner to start at the last (latest) entry
  $("#slideInner").css({marginLeft: slideWidth * -currentPosition});
  
  // Set #slideInner width equal to total width of all slides
  $('#slideInner').css('width', slideWidth * numberOfSlides);

  // Insert controls in the DOM
 // $('#msld')
//    .prepend('<span class="mcontrol" id="lControl">Clicking moves left</span>')
 //   .append('<span class="mcontrol" id="rControl">Clicking moves right</span>');

  
	/* Array of colors, which match the image or video. The last in array is the latest, the first is the oldest */
	$('body').animate({
		  'backgroundColor' : colors[currentPosition]
		});
	//$('body').css('background-color',colors[currentPosition]);
	
  // Create event listeners for .controls clicks
  $('#lControl')
    .bind('click', function(){
		//var rcontrol = $(this).attr('id')=='rControl';
		titleClick(0);
		return false;
  });
  
  $('#rControl')
    .bind('click', function(){
		//var rcontrol = $(this).attr('id')=='rControl';
		titleClick(1);
		return false;
  });
  
// Hide left arrow control on first load
  manageControls(currentPosition, slideWidth, colors);
	//location.hash = anchors[currentPosition];
	
  // Scroll through the titles on homepage
	function titleClick(rcont){
		// stop playing video of current position
		if(types[currentPosition]=="swf" && (pauses[currentPosition]==1)){
			formPause(currentPosition);
		}
		// Reload HTML if we have an html_video
		if(types[currentPosition]=="html_video"){
			$('#html_'+currentPosition).html(codes[currentPosition]);
		}
		/*
		if(types[currentPosition]=="special"){
			wlplayer.stopVideo();
		}
		*/
		
		// Determine new position
		if(rcont==0) {
			currentPosition = currentPosition-1;
		}
		else {
			currentPosition = currentPosition+1;
		}
		
		//alert(currentPosition);
		// Hide / show controls
		manageControls();
		
		location.hash = anchors[currentPosition];
		
		// Fade the background color on change
		$('body').animate({
		  'backgroundColor' : colors[currentPosition]
		});
		//$('body').css('background-color',colors[currentPosition]);
		
		// Move slideInner using margin-left
		$('#slideInner').animate({
		  'marginLeft' : slideWidth*(-currentPosition)
		});
		
		// Change the title of the entry
		//var mdy = titles[currentPosition].split('-');
		//$('#controls .num').html(mdy[0]+'<span class="bull">&bull;</span>'+mdy[1]+'<span class="bull">&bull;</span>'+mdy[2]);
		$('#controls .control-title').html(titles[currentPosition]);
		//alert(location.hash+" and "+currentPosition);
	}

  	// manageControls: Hides and Shows controls depending on currentPosition
  function manageControls(){
	// Now we can start displaying everything 
  $('#home_content_load').css('display','none');
  $('.slidez').css('display','block');
  
	//alert(currentPosition);
	
	//controls left
	if (currentPosition==0) {
		$('#lControl').unbind('click').click(function() {});
		
		// Unbind mouse events and bring the arrow to a 'closed' position
		$('#lControl').unbind('mouseover').click(function() {});
		$('#lControl').unbind('mouseleave').click(function() {});
		$('#lControl').animate({marginLeft: '48px'}, 200);
		
		$('#control-space_l').fadeTo(0,.3, function(){});
		$('#lControl').fadeTo(0,.3, function(){
			//Animation complete 
			$('#lControl').css('cursor','default');
		});
		
		// If there is only one entry, it will equal numberOfSlides-1
		if(currentPosition==numberOfSlides-1){
			$('#rControl').unbind('click').click(function() {});
		
			// Unbind mouse events and bring the arrow to a 'closed' position
			$('#rControl').unbind('mouseover').click(function() {});
			$('#rControl').unbind('mouseleave').click(function() {});
			$('#rControl').animate({marginLeft: '-48px'}, 200);
			
			$('#control-space_r').fadeTo(0,.3, function(){});
			$('#rControl').fadeTo(0,.3, function(){
				//Animation complete 
				$('#rControl').css('cursor','default');
			});
		}
		else {
			// Update the anchor link for left arrow
			$('#rControl').attr('href', '#'+anchors[currentPosition+1]);
		}
	}
	//controls right
	else if (currentPosition==numberOfSlides-1) {
		$('#rControl').unbind('click').click(function() {});
		
		// Unbind mouse events and bring the arrow to a 'closed' position
		$('#rControl').unbind('mouseover').click(function() {});
		$('#rControl').unbind('mouseleave').click(function() {});
		$('#rControl').animate({marginLeft: '-48px'}, 200);
		
		$('#control-space_r').fadeTo(0,.3, function(){});
		$('#rControl').fadeTo(0,.3, function(){
			//Animation complete 
			$('#rControl').css('cursor','default');
		});
		
		// Update the anchor link for left arrow
		$('#lControl').attr('href', '#'+anchors[currentPosition-1]);
	}
	else {
		$('#lControl').unbind('click').click(function() {});
		$('#rControl').unbind('click').click(function() {});
		
		$('#lControl')
			.bind('click', function(){
				//var rcontrol = $(this).attr('id')=='rControl';
				titleClick(0);
				return false;
		  });
		$('#control-space_l').fadeTo(0,1, function(){});
		$('#lControl').fadeTo(0,1, function(){
			//Animation complete 
			$('#lControl').css('cursor','pointer');
		});
		
		$('#rControl')
			.bind('click', function(){
				//var rcontrol = $(this).attr('id')=='rControl';
				titleClick(1);
				return false;
		  });
		$('#control-space_r').fadeTo(0,1, function(){});
		 $('#rControl').fadeTo(0,1, function(){
			//Animation complete 
			$('#rControl').css('cursor','pointer');
		});
		
		// Unbind the mouseover and mouseleave events
		$('#lControl').unbind('mouseover').click(function() {});
		$('#lControl').unbind('mouseleave').click(function() {});
		$('#rControl').unbind('mouseover').click(function() {});
		$('#rControl').unbind('mouseleave').click(function() {});
		
		// Bind the mouse events to our functions
		$('#lControl').bind('mouseover', function(){$(this).animate({marginLeft: '0px'}, 200,function () {});});
		$('#lControl').bind('mouseleave', function(){$(this).animate({marginLeft: '48px'}, 200,function () {});});
		$('#rControl').bind('mouseover', function(){$(this).animate({marginLeft: '0px'}, 200,function () {});});
		$('#rControl').bind('mouseleave', function(){$(this).animate({marginLeft: '-48px'}, 200,function () {});});
	
		// Update anchor links for left and right arrows
		$('#lControl').attr('href', '#'+anchors[currentPosition-1]);
		$('#rControl').attr('href', '#'+anchors[currentPosition+1]);
	}
  }	
});
