/*---------------------------
main.js

Artemis Branding
1.250.595.0136
admin@artemisbranding.com
----------------------------*/
var oData;
$(document).ready(function() {
//	var oData;
	var aPhotos = new Array();
	var aGallery = new Array();
	var aThumbnails = new Array();
	var aThumbnailsHover = new Array();
	// Preload Images
	var aImages = new Array('why-we-exist', 'a-look-inside', 'surgical-procedures', 'surgeons', 'your-surgery', 'out-of-towners', 'news');
	$.each(aImages, function(i, val) {
		aPhotos[i] = new Image();
		aPhotos[i].src = '/images/photos/' + val + '.jpg';
    });
	$.preloadCssImages();
	// Get module name
	var jModule = $.url.segment(0);
	// JScroll
	$('.scroll').jScrollPane({arrowSize:8, showArrows:true, scrollbarWidth:6, scrollbarMargin:15});
	// Load Google map
	if (jModule == 'contact-us') {
		loadMap();
	}
	// Highlight selected item
	var nItem = 0;
	var aIndex = new Array(	'home',
							'why-we-exist', 'philosophy-of-care',
							'a-look-inside', 'special-touches', 'accreditation', 'facility-tour',
							'surgical-procedures', 'bariatric-procedures', 'cosmetic-and-reconstuctive-plastic-surgery', 
								'abdominoplasty', 'brachioplasty', 'breast-surgery', 'buttockplasty', 'facial-surgery', 'hand-and-wrist-surgery', 'liposuction', 'post-bariatric-surgery', 'removal-of-lessions-moles-and-cysts', 'thighplasty', 
							'oral-surgery', 'pediatric-dental-procedures', 'general-surgical-procedures', 'orthopedic-procedures', 'neurosurgical-procedures', 'urological-procedures', 'vascular-procedures',
							'surgeons', 'bariatric-surgery', 'cosmetic-and-reconstructive-plastic-surgeons', 'oral-surgeons', 'pediatric-dental', 'general-surgery', 'orthopedic', 'neurosurgical', 'urological', 'vascular-surgery',
							'dentists', 'pediatric', 
							'your-surgery', 'step-one-get-yourself-ready', 'step-two-the-day-of-your-surgery', 'step-three-after-you-leave-now-what', 
							'out-of-towners', 
							'news', 
							'faqs', 
							'contact-us');
	$.each(aIndex, function(i, val) {
		if (jModule == val) {
			nItem = i;
		}
    });
	
	if (nItem == 0) {
		sSection = 'home';
	} else if (nItem >= $.inArray('why-we-exist', aIndex) && nItem <= $.inArray('a-look-inside', aIndex) - 1) {
		sSection = 'why-we-exist';
	} else if (nItem >= $.inArray('a-look-inside', aIndex) && nItem <= $.inArray('surgical-procedures', aIndex) - 1) {
		sSection = 'a-look-inside';
	} else if (nItem >= $.inArray('surgical-procedures', aIndex) && nItem <= $.inArray('surgeons', aIndex) - 1) {
		sSection = 'surgical-procedures';
	} else if (nItem >= $.inArray('surgeons', aIndex) && nItem <= $.inArray('dentists', aIndex) - 1) {
		sSection = 'surgeons';
	} else if (nItem == $.inArray('dentists', aIndex) || nItem == $.inArray('your-surgery', aIndex) - 1) {
		sSection = 'dentists';
	} else if (nItem >= $.inArray('your-surgery', aIndex) && nItem <= $.inArray('out-of-towners', aIndex) - 1) {
		sSection = 'your-surgery';
	} else if (nItem == $.inArray('out-of-towners', aIndex)) {
		sSection = 'out-of-towners';
	} else if (nItem == $.inArray('news', aIndex)) {
		sSection = 'news';
	} else if (nItem == $.inArray('faqs', aIndex)) {
		sSection = 'faqs';
	} else if (nItem == $.inArray('contact-us', aIndex)) {
		sSection = 'contact-us';
	}
	
	if (nItem >= $.inArray('abdominoplasty', aIndex) && nItem <= $.inArray('thighplasty', aIndex)) {
		$('#side-nav .item-cosmetic-and-reconstuctive-plastic-surgery').css({ 'background': "transparent url('/images/side-nav-light-grey.png') bottom right no-repeat" });
	}
	// Change CSS
	if ($('#menu .item-' + sSection + ' a').parent().hasClass('.sub')) {
		$('#menu .item-' + sSection + ' a').css({ 'background': '#8fa9ad' });
	} else {
		$('#menu .item-' + sSection + ' a').css({ 'background': '#29b5cc' });
	}
	$('#side-nav .item-' + sSection + '').next().css({ 'display' : 'block' });
	$('#side-nav .item-' + jModule + '').css({ 'background': "transparent url('/images/side-nav-light-grey.png') bottom right no-repeat" });
	$('#side-nav .item-' + sSection + '').css({ 'background': "transparent url('/images/side-nav-blue-selected.png') bottom right no-repeat", 'color' : '#ffffff' });
	$('#bottom-nav .item-' + sSection + ' a').css({'color': '#3dc4d5' });
	// Hover handlers
	$('#menu a').hover (	
		function(){
			$('#menu a').css({ 'background' : '#3dc4d5' });
			$('#menu .sub a').css({ 'background' : '#96b3b7' });
			if ($(this).parent().hasClass('.sub')) {
				$(this).css({ 'background' : '#8fa9ad' });
			} else {
				$(this).css({ 'background' : '#29b5cc' });
			}
		}
	)
	$('#menu').mouseleave (	
		function(){
			$('#menu a').css({ 'background' : '#3dc4d5' });
			$('#menu .sub a').css({ 'background' : '#96b3b7' });
			if ($('#menu .item-' + sSection + ' a').parent().hasClass('.sub')) {
				$('#menu .item-' + sSection + ' a').css({ 'background': '#8fa9ad' });
			} else {
				$('#menu .item-' + sSection + ' a').css({ 'background': '#29b5cc' });
			}
		}
	)
	$('#side-nav ul a').hover (	
		function(){
			$('#side-nav ul a').css({ 'background' : "transparent url('/images/side-nav-dark-grey.png') bottom right no-repeat" });
			$(this).css({ 'background': "transparent url('/images/side-nav-light-grey.png') bottom right no-repeat" });
		}
	)
	$('#side-nav ul a').mouseleave (	
		function(){
			$('#side-nav ul a').css({ 'background' : "transparent url('/images/side-nav-dark-grey.png') bottom right no-repeat" });
			$('#side-nav .item-' + jModule + '').css({ 'background': "transparent url('/images/side-nav-light-grey.png') bottom right no-repeat" });
			$('#side-nav .item-' + sSection + '').css({ 'background': "transparent url('/images/side-nav-blue-selected.png') bottom right no-repeat", 'color' : '#ffffff' });
			if (nItem >= $.inArray('abdominoplasty', aIndex) && nItem <= $.inArray('thighplasty', aIndex)) {
				$('#side-nav .item-cosmetic-and-reconstuctive-plastic-surgery').css({ 'background': "transparent url('/images/side-nav-light-grey.png') bottom right no-repeat" });
			}
		}
	)
	$('#bottom-nav a').hover (	
		function(){
			$('#bottom-nav a').css({ 'color' : '#aabcc0' });
			$(this).css({ 'color' : '#3dc4d5' });
		}
	)
	$('#bottom-nav').mouseleave (	
		function(){
			$('#bottom-nav a').css({ 'color' : '#aabcc0' });
			$('#bottom-nav .item-' + sSection + ' a').css({ 'color': '#3dc4d5' });
		}
	)
	// Load JSON
	$.getJSON("/javascript/json/gallery.json", function(data){
		oData = data;
		$('#facility-tour h5').text(oData.gallery[0]['description']);
	});
	// Thumbnails handler
	$('#thumbnails img').click (	
		function(){
			var index = $(this).attr('idx');
			SetDisplayImage(index);
		}
	)
	// Accordion
	$('#accordion').accordion({ autoHeight: false });
	// News scroll
	var nTop = 0;
	var nEntryCount = 0;
	var nNumRows = $('#num-rows').val();
	$('#up').click(function() {
		if (nTop < 0) {
			if (!$(':animated').length) {
				nEntryCount -= 1;
				nTop += $('.entry:eq(' + nEntryCount + ')').height() + 20;
				$('#holder').animate({ top: '' + nTop + '' }, 500, "swing");
				
			}
		}
	});
	$('#down').click(function() {
		if (nEntryCount < nNumRows - 1) {
			if (!$(':animated').length) {
				nTop -= $('.entry:eq(' + nEntryCount + ')').height() + 20;
				$('#holder').animate({ top: '' + nTop + '' }, 500, "swing");
				nEntryCount += 1;
			}
		}
	});
	// Blur outline
	$('a').click(function() {
		this.blur();
	});
});


/** 
 * jQuery extension for preloading images 
 */
(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)


/** 
 * gallery methods 
 */

function AtEnd() {
	totalImages = oData.gallery.length;
	currIndex = parseInt($('#facility-tour #section-image').attr('idx'));
	if (currIndex < (totalImages - 1)) {
		return false;
	} else {
		return true;
	}
}

function AtStart() {
	currIndex = parseInt($('#facility-tour #section-image').attr('idx'));
	if (currIndex > 0) {
		return false;
	} else {
		return true;
	}
}

function SetArrowVisibility() {
	// set visibility of nav arrows
	if (AtEnd()) {
		$('#nextimg').hide();
	} else {
		$('#nextimg').show();
	}
	
	if (AtStart()) {
		$('#previousimg').hide();
	} else {
		$('#previousimg').show();
	}
}

function ShowNextImage() {
	if ($('#nextimg').is(':visible')) {
		UpdateGalleryImages(true);
	}
}

function ShowPrevImage() {
	if ($('#previousimg').is(':visible')) {
		UpdateGalleryImages(false);
	}
}

function UpdateGalleryImages(showNext) {
	newIndex = -1;
	
	if (showNext && !AtEnd()) {
		newIndex = currIndex + 1;
	} else if (!showNext && !AtStart()) {
		newIndex = currIndex - 1;
	}

	// do we have a new image to show?
	if (newIndex > -1) {
		SetDisplayImage(newIndex);
	}
}

function SetDisplayImage(newIndex) {
	// get data for next image
	img = oData.gallery[newIndex];

	// hide image so it can fade in later
	$('#section-image').hide();
	
	// set new image
	$('#facility-tour h5').text(img['description']);
	$('#facility-tour #section-image').attr('src', '/images/gallery/' + img['name'] + '.jpg');
	$('#facility-tour #section-image').attr('idx', newIndex);
	
	$('#section-image').fadeIn('slow');
	
	SetArrowVisibility();
	
	// iterate over <ul> of thumbnails and update values
	$.each(oData.gallery, function(i, val) {
		if (img.name == val['name']) {
			$('#thumbnails #' + val['name']).attr('src', '/images/gallery/thumbnails-hover/' + val['name'] + '.jpg');
		} else {
			$('#thumbnails #' + val['name']).attr('src', '/images/gallery/thumbnails/' + val['name'] + '.jpg');
		}
	});
}