/* Author: Patrick Carroll, Tremendousness Communications

*/

$(document).ready(function(){
	
	// CURRENT PAGE HIGHLIGHTS
	
	// parse current page url using jquery url parser plug-in
	var url = $.url();
	// store current page name
	var $page = url.attr('file');
	// store current directory
	var $dir = url.segment(-2);
	// store current parent directory
	var $par = url.segment(-3);
	
	// if no page name, highlight home page
	if(!$page) {
		$page = 'index.html';
	}
	// Highlight main nav items
	$('nav#mainNav a').each(function(){
		var $href = $(this).attr('href');
		var $section = $(this).attr('id');
		var $sectionPar = $(this).closest('a').attr('id');
		if ( ($href == $page) || ($href == '') || ($section == $dir) || ($sectionPar == $par) ) {
			$(this).addClass('current');
		} else {
			$(this).removeClass('current');
		}
	}); // end main nav highlight
	
	// Highlight sub nav items
	$('nav.sub li a').each(function(){
		var $href = $(this).attr('href');
		if ( ($href == $page) || ($href == '') ) {
			$(this).addClass('current');
		} else {
			$(this).removeClass('current');
		}
	}); // end main nav highlight
	
	// DISPLAY SUB-LINKS IN MAIN NAV MENU
			
	// Hide navigation sub-menus initially
	$('nav#mainNav ul li ul').hide();
	// display appropriate sub-menu when section is active
	$('nav#mainNav ul li ul').each(function() {
		// create variable to store id of previous element (a)
		var $section = $(this).prev('a').attr('id');
		// if previous element id matches current directory, show the sub-menu
		if ( ($dir == $section) || ($par == $section) ) {
			$(this).show();
			// otherwise, hide the sub-menu
			} else {
				$(this).hide();
			}
		}); // end sub menu display
	
	// end sub-page highlight
	
	// HIDE AWARD ICONS IN ALL SUB-PAGES
	
	function hideAwards() {
		var $page = url.attr('file');
		var $homePage = "home.html";
		if ($page == $homePage) {
			//alert("visible");
			$('#awardsHighlight').css('display', 'block');
		} else {
			//alert("invisible");
			$('#awardsHighlight').css('display', 'none');
			$('#audio').html('');
		};
	};
	
	hideAwards();
	
	// HEADER PHOTO SLIDESHOWS
	
	$('.slideshow').cycle({
		fx: 'fade',
		speed: 2000,
		timeout: 5000
	});
	
	// PEOPLE SCROLLER
	
	$('#people-scroll').cycle({
		fx: 'scrollRight',
		speed: 1000,
		timeout: 0,
		fit: true,
		cleartype: false,
		width: 700,
		height: 220,
		pager: $('#people-scroll-nav'),
	    activePagerClass: 'current'
	});
	
	// TABS
	
	// initialize the tabbed interface
	tabs = $(".tabs").tabs({
		'select': function(event, ui){document.location.hash = ui.panel.id;}, // include default method set to update the url when tabs are clicked (this makes bookmarking possible)
		fx: {opacity: 'toggle'} // sections fade in
	});

	// now start by selecting the tab that corresponds to the url hash
	// if the hash does not match a tab the first tab will be selected by default
	tabs.tabs('select',document.location.hash);
	
	// TOOLTIP
	
	$('.bubble').tooltip({
		track: true,
		fixPNG: true,
		showURL: false,
		fade: 250,
		opacity: 0.9,
		showBody: ' - '
	});
	
	// FANCYBOX

	/* This is basic - uses default settings */
	
	$("#people-scroll a").fancybox({
		width: 600
	});
	
	$(".auto-popup").fancybox().trigger('click');
	
	$(".photo-gallery").fancybox();
	
	// PHOTO GALLERY HEADER COLLAPSE
	
	$('.closeHeader').click(function(){
		$('.slideshow').slideUp('slow');
	});
	
	$('.showHeader').click(function(){
		$('.slideshow').slideDown('slow');
	});
	
	// JPLAYER
	
	$("#jquery_jplayer_1").jPlayer({
        ready: function () {
          $(this).jPlayer("setMedia", {
            mp3: "musicloop01.mp3",
            oga: "musicloop01.ogg"
          }).jPlayer("play");
        },
		ended: function (event) {
  			$(this).jPlayer("play");
		},
        swfPath: "js",
        supplied: "mp3, oga"
	});
	
	// FORM VALIDATION
	
	$('.registration').validate({
		rules: {
			name: "required",
			passport: {
				required: true,
				number: true
			},
			nationality: "required",
			email: {
				required: true,
				email: true
			},
			address: "required",
			phone: {
				required: true,
				number: true
			},
			"property[]": {
				required: true,
				minlength: 1
			},
			fax: "number",
			lead_type: "required"
		},
		messages: {
			name: "Please enter your name.",
			lastname: "Please enter your lastname.",
			passport: {
				required: "Please enter your passport or IC number.",
				number: "This field can contain numbers only."
			},
			nationality: "Please enter your nationality.",
			email: {
				required: "Please enter your email address.",
				email: "Please enter a valid email address."
			},
			address: "Please enter your mailing address.",
			phone: {
				required: "Please enter your contact number.",
				number: "This field can contain numbers only."
			},
			"property[]": "Please select at least one.",
			fax: "This field can contain numbers only.",
			lead_type: "Please select one."
		},
		errorPlacement: function(error, element) {
            if ( element.is(":radio") ) {
               error.insertAfter( element.parent() );
            }
            else { // This is the default behavior of the script for all fields
               error.insertAfter( element );
            }
        }
	});

}); // end document ready






















