// JavaScript Document
//Keep track of selected section

var playerID = 0;

function handlePlayerStateChange (state) {
  var videoContainer = $('#carousel');
  //alert(state);
  switch (state) {
    case 1: 
	  // Playing
	 break;
	case 3:
      // Video has begun playing/buffering
      videoContainer.cycle('pause');
      break;
    case 2:
	  // Paused
	  videoContainer.cycle('pause');
      break;
    case 0: 
      // Video has  ended
      $('.youtubecont').css('left', '-1000px');
	  videoContainer.cycle('resume');
      break;
  }
}

function onYouTubePlayerReady(id){
  //Add it to the list of players for later pausing/removing etc.
  
  //console.log("Player id = "+id);
  
  player = document.getElementById(id);
  
  if (player.addEventListener) {  
    player.addEventListener('onStateChange', 'handlePlayerStateChange');
  }
  else {
	player.attachEvent('onStateChange', 'handlePlayerStateChange');
  }
  //Now play the video
  var ua = $.browser;
  if ( ua.msie && ( ua.version.slice(0,3) == "8.0" || ua.version.slice(0,3) == "9.0" ) ) {
    $('.youtubecont').css('left','150px'); } else {
    $('.youtubecont').css('left','0px');
  }
  player.playVideo();
  playerID = player;
  //console.log("playerID = "+playerID+" AND player= "+player);
}

$(document).ready(function() {
	//Set up the three thumbnail buttons with their data attributes for tracking the panel
	$('#b_Contemporary').data('sectiontype', 1);
	if (current_section == 1)
	{
		$("#b_Contemporary").addClass("selected");
	}
	$('#b_Classical').data('sectiontype', 2);
	if (current_section == 2)
	{
		$("#b_Classical").addClass("selected");
	}
	$('#b_Traditional').data('sectiontype', 3);
	if (current_section == 3)
	{
		$("#b_Traditional").addClass("selected");
	}
	$('#b_About').data('sectiontype', 'about');
	if (current_section == 'about')
	{
		$("#b_About").addClass("selected");
	}
	$('#b_Awards').data('sectiontype', 'awards');
	if (current_section == 4)
	{
		$("#b_Awards").addClass("selected");
	}
	$('#b_News').data('sectiontype', 'news');
	if (current_section == 'news')
	{
		$("#b_News").addClass("selected");
	}
	$('#b_Contact').data('sectiontype', 'contact');
	if (current_section == 'contact')
	{
		$("#b_Contact").addClass("selected");
	}
	
	var panelvis = false;
	var origtop = $('#menutint').css('top');
	//Add the logo into the #menupanel div
	var ua = $.browser;
	if ( ua.msie && ua.version.slice(0,3) == "6.0" ) {
		$("#menupanel").prepend("<a href='/index.php' style='border: 0;'><img class='logo' id='logo' src='/img/logo.gif' class='Des Ewing Residential Architects'/></a>");
	} else {
		$("#menupanel").prepend("<a href='/index.php' style='border: 0;'><img class='logo' id='logo' src='/img/logo.png' class='Des Ewing Residential Architects'/></a>");
	}
	
	//Main slide show (home and proprties)
	if ($('#carousel').length)
	{
		$('#carousel').cycle({
		fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		timeout: 6000
		});
	}
	//News and Awards slides
	if ($('#carousel_news').length)
	{
		$('#carousel_news').cycle({
		fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		timeout: 6000
		});
	}
	
	//Add hover styles to mainmenu - property thumbs
	$("a.thumbbuttons").hover(function(){
		if (current_section != $(this).data('sectiontype'))
		{
			//Not the current section - so show a highlight
			$(this).removeClass("normal").addClass("hover");
		}
	}, function(){
		if (current_section != $(this).data('sectiontype'))
		{
			//Not the current section - so remove highlight 
			$(this).removeClass("hover").addClass("normal");
		}
    });
	//Add hover styles to mainmenu - content items
	$("a.contentbuttons").hover(function(){
		if (current_section != $(this).data('sectiontype'))
		{
			//Not the current section - so show a highlight
			$(this).removeClass("normal").addClass("hover");
		}
	}, function(){
		if (current_section != $(this).data('sectiontype'))
		{
			//Not the current section - so remove highlight 
			$(this).removeClass("hover").addClass("normal");
		}	
    });
	
	
	//Add property and award thumbnail animation
	$('.thumbbuttons').click(function() {
  		//Deal with revealing the panel for property thumbnails
		//If IE6 - need to apply the bg style again:
		var ua = $.browser;
		//Need to check if the submenupanel is already on AND a different button is pressed.
		//First is submenu on
		if (panelvis == true)
		{
			//its visible... has the SAME button been pressed
			if (current_section == $(this).data('sectiontype'))
			{
				//Yes - need to retract the panel
				current_section = 0;
				panelvis = false;
				//Animate it closed
				$('#submenupanel').hide();
				$('#menutint').animate(
					{ top: origtop, height:'50px' },
					'slow',
					function() {
						//Animation complete - nothing
				});
			}
			else
			{
				current_section = $(this).data('sectiontype');
			}
		}
		else
		{
			//its not on - therefore display
			current_section = $(this).data('sectiontype');
			panelvis = true;
			//Animate it open
			$('#menutint').animate(
				{ top: '-139px', height:'90px' },
				'slow',
				function() {
					//Animation complete - show the submenu items
					$('#submenupanel').show();
			});
		}
		/*return false;*/
	});
		
	//Add other content animation
	$('.contentbuttons').click(function() {
		//Deal with revealing the panel for property thumbnails
		//If IE6 - need to apply the bg style again:
		var ua = $.browser;
		//Need to check if the submenupanel is already on AND a different button is pressed.
		//First is submenu on
		if (panelvis == true)
		{
			//its visible... has the SAME button been pressed
			if (current_section == $(this).data('sectiontype'))
			{
				//Yes - need to retract the panel
				current_section = 0;
				panelvis = false;
				//Animate it closed
				$('#submenupanel').hide();
				$('#menutint').animate(
					{ top: origtop, height:'50px' },
					'slow',
					function() {
						//Animation complete - nothing
				});
			}
			else
			{
				current_section = $(this).data('sectiontype');
			}
		}
		else
		{
			//its not on - therefore display
			current_section = $(this).data('sectiontype');
			//Don't want to show the animation for the following types of button:
			if (current_section != 'news' && current_section != 'awards' && current_section != 'contact')
			{
				panelvis = true;
				//Animate it open
				$('#menutint').animate(
					{ top: '-139px', height:'90px' },
					'slow',
					function() {
						//Animation complete - show the submenu items
						$('#submenupanel').show();
				});
			}
		}
		/*return false;*/
	});
	
	//Now see if the "revealmore" item is present - if it is load its reveal function
	if ($('#revealmore').length != 0)
	{
		//Its present Now add what to do if clicked
		$('#revealmore').click(function() {
			//Reveal more of the page description
			//Animate it down
			$('#pagecontent').animate(
				{ height:'480px' },
				'slow',
				function() {
					//Animation complete - show the submenu items
				});
			$('#revealmore').hide();
			/*return false;*/
		});
	}
	
});
/*---------End of page load behaviour------------*/


/*****************************************************************************/
//basic rollover
function ImgSwap(img_name, img_src)
{
	if (document.images) { document[img_name].src = img_src; }
}

//AJAX functionality for thumbs
function sendValue(passedtype,passedstart,passedaction)
{
	$.get(
		"includes/menu_thumbs.php",
		{ type: passedtype, s: passedstart, action: passedaction },
		//function that is called when server returns a value.
		function(responseText)
		{
			$('#submenupanel').html(responseText);
		},
		"html"
	);
}

//AJAX functionality for content menus
function showSubmenu(passedmenu)
{
	if (passedmenu = "about") { output = "<div class=\"aboutmenu\"><p><a href=\"/content.php?id=1\">Who we are</a><br/><a href=\"/content.php?id=2\">Meet Des Ewing</a><br/><a href=\"/content.php?id=3\">Meet the Team</a><!--<br/><a href=\"/content.php?id=4\">Who/What we like</a>--></p></div>"; }
	$('#submenupanel').html(output);
}

/********************************************************************************/
// Contact form
$(function() {
	// load the modal window
	$('.contactform').click(function(){

		// scroll to top
		$('html, body').animate({scrollTop:0}, 'fast');

		// before showing the modal window, reset the form incase of previous use.
		$('.contactsuccess, .contacterror').hide();
		$('form#contactForm').show();

		//show the mask and contact divs
		$('#mask').show().fadeTo('', 0.7);
		$('div#contact').fadeIn();

		// stop the modal link from doing its default action
		return false;
	});

	// close the modal window is close div or mask div are clicked.
	$('div#close, div#mask').click(function() {
		$('div#contact, div#mask').stop().fadeOut('slow');

	});

	// when the Submit button is clicked...
	$('#contactForm').submit(function() {
		//Inputed Strings
		var fullname = $('#FullName').val(),
			email = $('#Email').val(),
			telephone = $('#Telephone').val(),
			comment = $('#Message').val(),
			subscribe = $('#KeepInformed').val();

		//Error Count
		var error_count = 0;

		//Regex Strings
		var email_regex = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
		
		//Clear earlier highlighted errors
		$('input').removeClass('inputerror');
		
			//Test Name
			if(fullname == '') {
				//$('p#err_name').fadeIn();
				$('input#FullName').addClass('inputerror');
				error_count += 1;
			}

			//Test Email
			if(!email_regex.test(email)) {
				//$('p#err_email').fadeIn();
				$('input#Email').addClass('inputerror');
				error_count += 1;
			}

			//Blank Comment?
			if(comment == '') {
				//$('p#err_message').fadeIn();
				$('textarea#Message').addClass('inputerror');
				error_count += 1;
			}

			//No Errors?
			if(error_count === 0) {
				//alert ('FullName=' + fullname + '&amp;Email=' + email + '&amp;Telephone=' + telephone + '&amp;KeepInformed=' + subscribe + '&amp;Message=' + comment);
				$.ajax({
					type: 'post',
					url: '/contactExec.php',
					data: {	FullName	: fullname,
							Email		: email,
							Telephone	: telephone,
							KeepInformed: subscribe,
							Message		: comment },
					error: function() {
						$('#sendError').slideDown('slow');
					},
					success: function () {
						$('form#contactForm').fadeOut('fast');
						$('.contactsuccess').slideDown('slow');
					}
				});
			}

			else {
                //$('.contacterror').show();
            }

		return false;
	});
});

//News story
$(function() {
	// load the modal window
	$('.newsitem_container').click(function(){
		// scroll to top
		$('html, body').animate({scrollTop:0}, 'fast');

		//show the mask and story divs
		$('#newsmask').show().fadeTo('', 0.7);
		$('#newsitem').empty(); //to avoid a flash if content alreay loaded intot he div from previous news story
		$('div#newsstory').fadeIn();

		// stop the modal link from doing its default action
		return false;
	});

	// close the modal window is close div or mask div are clicked.
	$('div#newsclose, div#newsmask').click(function() {
		$('div#newsstory, div#newsmask').stop().fadeOut('slow');

	});
});

function loadNews(passedNewsID)
{
	$('#newsitem').load('/includes/news_item.php?id='+passedNewsID);
}

function loadAwards(passedAwardsID)
{
	$('#newsitem').load('/includes/awards_item.php?id='+passedAwardsID);
}
