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

Artemis Branding
1.250.595.0136
admin@artemisbranding.com
----------------------------*/
$(document).ready(function() {
	// Vars
	var aBanners = new Array();
	var aGallery = new Array();
	var aPhotos = new Array();
	// Get module name
	var jModule = $.url.segment(0);
	if (jModule == null) {
		jModule = 'home';
	}
	// Swap logo
	if (jModule != 'home') {
		$('#logo a').css({ 'background': 'transparent url("/images/logo-b.png") no-repeat' });
	}
	// Preload Images
	var aBannersStr = new Array('home', 'about-us', 'our-shop', 'courses', 'community', 'road-trip', 'contact');
	$.each(aBannersStr, function(i, val) {
		aBanners[i] = new Image();
		aBanners[i].src = '/images/banners/' + val + '.jpg';
    });
	var aGalleryStr = new Array('image1', 'image2', 'image3', 'image4', 'image5', 'image6', 'image7');
	$.each(aGalleryStr, function(i, val) {
		aGallery[i] = new Image();
		aGallery[i].src = '/images/gallery/' + val + '.jpg';
    });
	var aPhotosStr = new Array('bicycle', 'gallery', 'gears', 'keys', 'tour', 'tracks');
	$.each(aPhotosStr, function(i, val) {
		aPhotos[i] = new Image();
		aPhotos[i].src = '/images/photos/' + val + '.jpg';
    });
	$.preloadCssImages();
	// Highlight menu items
	$('#menu .item-' + jModule + ' a').css({ 'background' : 'transparent url("/images/hover.png") repeat-x' });
	$('#nav .item-' + jModule + ' a').css({ 'color': '#58585a' });
	if (jModule == 'contact') {
		$('#contact-us a').css({ 'background' : '#c0c0c0 url("/images/menu.png") -687px 9px no-repeat' });
	}
	// Hover handlers
	$('#menu a').hover (	
		function(){
			$('#menu a').css({ 'background': 'transparent' });
			$(this).css({ 'background': "transparent url('/images/hover.png') repeat-x" });
			if (jModule == 'contact') {
				$('#contact-us a').css({ 'background-color' : 'transparent' });
			}
		}
	)
	$('#menu').mouseleave (	
		function(){
			$('#menu a').css({ 'background': 'transparent' });
			$('#menu .item-' + jModule + ' a').css({ 'background': 'transparent url("/images/hover.png") repeat-x' });
			if (jModule == 'contact') {
				$('#contact-us a').css({ 'background-color' : '#c0c0c0' });
			}
		}
	)
	$('#nav a').hover (	
		function(){
			$('#nav a').css({ 'color': '#ffffff' });
			$(this).css({ 'color': '#58585a' });
		}
	)
	$('#nav').mouseleave (	
		function(){
			$('#nav a').css({ 'color': '#ffffff' });
			$('#nav .item-' + jModule + ' a').css({ 'color': '#58585a' });
		}
	)
	// Colorbox
	$('#our-shop area').colorbox({ rel: 'gallery', transition: 'elastic', speed: 500 });
	$('#courses #inner .details .view-calendar').colorbox({ iframe: true, width: '830px', height: '745px', scrolling: false, transition:'elastic', speed:500 });
	$('#product .body .thumb').colorbox({ transition: 'elastic', speed: 500 });
	$('#viewer #side-right a').colorbox({ transition: 'elastic', speed: 500 });
	$('.add-to-cart').live('click', function() { 
		var oForm = $(this).parents('form:first');
		$.post("/modules/sections/sub/cart.mod.php", $(oForm).serialize(), function() {
			$.fn.colorbox({ iframe: true, href:"/modules/sections/sub/cart.mod.php", width:  '505px', height: '600px', scrolling: false, transition:'elastic', speed:500 });
			}
		);
		return false;
	});
	$('.view-cart').live('click', function() { 
		$.fn.colorbox({ iframe: true, href:"/modules/sections/sub/cart.mod.php", width:  '505px', height: '600px', scrolling: false, transition:'elastic', speed:500 });
		return false;
	});
	// Form validation
	$('#rt-form').submit(function() {
		var content = tinyMCE.activeEditor.getContent(); // get the content
		$('#rt-body').val(content); // put it in the textarea
	});
	// Road Trip Form
	$('#rt-form').validate({	
		rules: {
			rtdate: {
				required: true,
				date: true
			},
			rtbody: {
				required: true
			}
		},
		errorElement: "div",
		errorPlacement: function(error, element) {
			error.insertBefore(element);
		}
	});
	// Tool Store buttons
	$('.add-to-cart').hover(function(){
		$(this).attr('src', '/images/add-to-cart-on.png');
	});
	$('.add-to-cart').mouseleave(function(){
		$(this).attr('src', '/images/add-to-cart-off.png');
	});
	$('.view-cart').hover(function(){
		$(this).attr('src', '/images/view-cart-on.png');
	});
	$('.view-cart').mouseleave(function(){
		$(this).attr('src', '/images/view-cart-off.png');
	});
	$('#enter-code').hover(function(){
		$(this).css ({ 'background-image' : 'url("/images/enter-on.png")' });
	});
	$('#enter-code').mouseleave(function(){
		$(this).css ({ 'background-image' : 'url("/images/enter-off.png")' });
	});
	// Tool Store Form
	$('#insert').click(function(){
		$('#ts-form').validate({	
			rules: {
				tsname: {
					required: true
				},
				tsprice: {
					required: true
				},
				tsimage: {
					required: true
				},
				tscategory: {
					required: true
				},
				tsdescription: {
					required: true
				},
				tsshipping1: {
					required: true,
					number: true
				},
				tsshipping2: {
					required: true,
					number: true
				}
			},
			errorElement: "div"
		});
	});
	$('#replace').click(function(){
		$('#ts-form').validate({	
			rules: {
				tsname: {
					required: true
				},
				tsprice: {
					required: true
				},
				tscategory: {
					required: true
				},
				tsdescription: {
					required: true
				},
				tsshipping1: {
					required: true,
					number: true
				},
				tsshipping2: {
					required: true,
					number: true
				}
			},
			errorElement: "div"
		});
	});
	$('#clear').click(function(){
		location.reload();
	});
	// Confirm
	$('#replace').click(function(){
		var answer = confirm('Are you sure you would like to replace this entry?');
		return answer;
	}); 
	$('#delete').click(function(){
		var answer = confirm('Are you sure you would like to delete this entry?');
		return answer;
	}); 
	// Scroll Position
	$(window).scrollTop($.cookie('scrollPosition'));
	$('.ajax-arrow').click(function(){
		$(window).unload(function() {
			var scrollTop = $(window).scrollTop();
			$.cookie('scrollPosition', scrollTop);
		});
	});
	$.cookie('scrollPosition', '0');
	// AJAX - Categories
	$('#cat-category').click(function() {
		var cat = $('#cat-category').val();
		$('#cat-table tbody').load('/includes/ajax.handler.php?cat=' + cat + '');
	});	
	// Local pickup
	$('#ts-checkbox').click(function(){
		var cat = $('#display-cat').val();
		$('#display').load('/includes/ajax.handler.php?display-cat=' + cat + '&pickup=toggle', function() {
				$('#product .body .thumb').colorbox({ transition: 'elastic', speed: 500 });
			}
		);	
	});
	// Discount code
	$('#code button').click(function(){
		var cat = $('#display-cat').val();
		var code = $('#ts-code').val();
		$('#display').load('/includes/ajax.handler.php?display-cat=' + cat + '&code=' + code + '', function() {
				$('#product .body .thumb').colorbox({ transition: 'elastic', speed: 500 });
			}
		);
	});
});
