/*-----------------------
main.js
Artemis PR and Design
250.595.0136
francis@artemisbranding.com
-----------------------*/
$(document).ready(function() {
	// Vars
	var oImages;
	var aModules;
	var aReviews;
	var aPreloadPanels = new Array();
	var aPreloadLarge = new Array();
	var aPreloadThumbs = new Array();
	var aPreloadReviews = new Array();
	var preloadMainA = new Image();
	var preloadMainB = new Image();
	var sBackground = 'one';
	var bHighlighted = false;
	// Preload images
	preloadMainA.src = '/images/quote.png';
	preloadMainB.src = '/images/panels/one.png';
	$.preloadCssImages();
	// JScroll
	$('.scroll').jScrollPane({showArrows:true, scrollbarWidth:10, arrowSize:20});
	// Colorbox
	$('#reviews a').colorbox({transition:"elastic"});
	// Preload reviews
	aReviews = new Array(
		'xpress-finken', 
		'ottawacitizen-king-1', 
		'ottawacitizen-king-2', 
		'jamesbaybeacon-collins',
		'catalogue-green-1',
		'catalogue-green-2',
		'catalogue-genie'
	);
	$.each(aReviews, function() {
		aPreloadReviews[i] = new Image();
		aPreloadReviews[i].src = '/images/reviews/' + this +'.png';
	});
	// JSON
	$.getJSON("/javascript/images.json", function(data){
		oImages = data;
		$.each(oImages.gallery, function(i, val) {
			aPreloadLarge[i] = new Image();
			aPreloadLarge[i].src = '/images/gallery/large/' + val['name'] +'.png';
			aPreloadThumbs[i] = new Image();
			aPreloadThumbs[i].src = '/images/gallery/thumbs/' + val['name'] +'.png';
			if (i <= 4) {
				aPreloadPanels[i] = new Image();
				aPreloadPanels[i].src = '/images/panels/' + val['name'] +'.png';
			}
		});
	});
	// Make second level visible; swap background panels
	aModules = new Array('home', 'profile', 'statement', 'biography', 'solo', 'group', 'permanent', 'awards', 'reviews', 'professional', 'education', 'gallery', 'contact');
	for (x in aModules) {
		if ($.url.segment(0) == aModules[x]) {
			if (x == 0) {
				sBackground = 'one';
			} else if (x == 1) {
				sBackground = 'two';
			} else if (x == 2) {
				sBackground = 'three';
			} else if (x >= 3 && x <= 10) {
				$('#menu ul li ul').css ('visibility','visible');
				bHighlighted = true;
				sBackground = 'four';
			} else if (x == 12) {
				sBackground = 'five';
			}
		} 
		$('#container').css('background-image', 'url("/images/panels/' + sBackground + '.png")');
	}
	// Drop-down behaviour
	if (bHighlighted == false) {
		$('#menu ul li ul').css ('display','none');
	}
	$('#menu ul li')
	.hover (
		function(){
			if ($('ul', this).is(":hidden")) {
				$('ul', this).stop(true,true).show(400);
			}
		}, 
		function(){
			$('ul', this).css ('visibility','visible');
			if (bHighlighted == false) {
				$('ul', this).stop(true,true).hide(400);
			}
		}
	)
	// Drop-down item hover
	$('#menu ul ul')
	.hover (
		function(){
			$('.drop-down').css('color', '#ffffff');
		}, 
		function(){
			$('.drop-down').css('color', '');
		}
	)
	// Highlight hover
	$('#menu li')
	.hover (
		function(){
			$('#menu .highlight').css('color', '#8c8c8c');
		}, 
		function(){
			$('#menu .highlight').css('color', '');
		}
	)
	// Gallery effect
	$('#arrow-bottom').fadeOut(0);
	$('#arrow-top').click (
		function(){
			$('#description').animate({ height : '0', top : '60px'}, 500, 'swing');
			$('#arrow-bottom').fadeIn(500);		
		}
	)
	$('#arrow-bottom').click (
		function(){
			$('#description').animate({ height : '60px', top : '0' }, 500, 'swing');
			$('#arrow-bottom').fadeOut(500);
		}
	)
	// Gallery click handler
	$('#thumbs img')
	.click ( 
		function() {
			var sId = $(this).attr('id');
			var sPath = '/images/gallery/large/' + sId + '.png';
			$('#area-middle img')
				.css({'display' : 'none'})
				.attr('src', sPath)
				.fadeIn(300);
			if (sId == 'seven' || sId == 'eight' || sId == 'nine' || sId == 'ten') {
				$('#view #description')
					.css('background-image', 'url("/images/trans-light.png")');
			} else {
				$('#view #description')
					.css('background-image', 'url("/images/trans-dark.png")');
			};
			// Change description
			$.each(oImages.gallery, function(i, val) {
				if (val['name'] == sId) {
					$('#description p').html(oImages.gallery[i]['description']);
				}
			});
		}
	);
	// If admin...
	if ( $("#admin").length > 0 ) {
		$('#container').css({ 'background' : '#000000' });
	}
	// Admin hover
	$('#admin a')
	.hover ( 
		function() {
			$(this).css ('color','#dd7600');
		},
		function() {
			$(this).css ('color','#ffffff');
		}
	);
	// Form validation and confirmation
	$(".check").click(function() {
		var tinyContent = tinyMCE.get('admin-info').getContent();
		if ($("input:first").val() == "") {
			alert ('Oops... I think you may have forgot to choose a category or enter a date.');
			return false;
		}
		if (tinyContent == "") {
			alert ('Oops... I think you may have forgot to choose a category or enter your content.');
			return false;
		}
    });
	$(".confirm").click(function() {
		var answer = confirm('Are you sure you want to delete this item?');
		if (answer) {
			return true;
		} else {
			return false;
		}
    });
});