// JavaScript Document
jQuery(document).ready(function(){
	var el = document.createElement("div");
	if(typeof el.style.textShadow != "string") {
		$('#main-menu .tab .text').clone().addClass('shadow').appendTo($('#main-menu .tab .text'));
	}
	
	/********************
		#search
	*********************/
	
	var el = $('#search .container .center input');
	$.data(el, 'width', el.width());
	
	$('#global-navigation #search .container .center input').bind({
		focus: function (e) {
			if (!$.data(e.target, 'width'))
				$.data(e.target, 'width', $(this).width())
			$(this).stop().animate({width: 267});
		},
		blur: function (e) {
			$(this).stop().animate({width: $.data(el, 'width')});
		}
	});
	
	$('#global-navigation #search .container .center input, #main-content #search .container .center input').bind({
		focus: function (e) {
			$(this).parent().parent().addClass('focus');
		},
		blur: function (e) {
			$(this).parent().parent().removeClass('focus');
		},
		keydown: function (e) { 
			if (e.which == 13) { 
				__doPostBack($(this).parents('.container').find('.searchButton').attr('id').replace(/_/g, '$'), ''); 
			} 
		} 
	});
	
	
	/********************
		#main-menu
	*********************/
	
	$('#main-menu ul').first().addClass('first-child').end().last().addClass('last-child');
	
    $('#main-menu .tab .content').append('<div class="symbol">').parent().bind({
		click: function(){
			if ($('#main-menu').hasClass('show')) {
				$('#main-menu .mega-menu').stop(true,true).slideUp('fast');
				$('#main-menu').removeClass('show');
			} else {
				$('#main-menu .mega-menu').stop(true,true).slideDown('fast');
				$('#main-menu').addClass('show');
			}
		},
		mouseenter: function(){
			$('#main-menu').addClass('show');
			$('#main-menu .mega-menu').stop(true,true).slideDown('fast');
			$('#main-menu').addClass('show');
		},
		mouseleave: function(){
			$('#main-menu .mega-menu').stop(true,true).delay('slow').slideUp('fast', function(){
				$('#main-menu').removeClass('show');
			});
		}
	});
	
	
	/********************
		#main-image
	*********************/
	$(window).load(function(){
		// Set the min-height of the body equal to the image's height.
		$('#bd').css('min-height', $('#main-image img').height());
		$('#main-content').css('min-height', $('#main-image img').height());
	});
	
	
	/********************
		#side-box
	*********************/
	$('#side-box .accordion').accordion({
		autoHeight: false
	});
	
	$('#side-box .submit').click(function(e){
		$(this).closest('li').find('textarea, input').each(function(i, el){
			if ($(el).val() == $(el).data('defaultValue')) {
				$(el).addClass('error');
				e.preventDefault();
			}
		});
	});
	
	/***********************
		input behaviour
	************************/
	$('#global-navigation #search .container .center input, #side-box input[type=text], #side-box textarea').each(function(i, el){
		$(this).data('defaultValue', $(this).val())
	}).bind({
		focus: function(){
			if ($(this).val() == $(this).data('defaultValue'))
				$(this).val('');
			$(this).removeClass('error');
		},
		blur: function(){
			if ($(this).val() == '')
				$(this).val($(this).data('defaultValue'));
		}
	});
	
	
	/****************************
		#main-content .models
	*****************************/
	$('#main-content .models a').hover(
		function(e){
			$(e.target).stop(true,true).animate({
				paddingBottom: "1.5em"
			},'fast');
		},
		function(e){
			$(e.target).stop(true,true).animate({
				paddingBottom: "0.5em"
			},'fast');
		}
	);
	$('#main-content .models div:last-child').addClass('last-child');
	
	
	/******************************
		#main-content #ResultArea
	*******************************/
	$('#main-content #ResultArea ol li:odd').addClass('odd');
	$('#main-content #ResultArea ol li a').prepend('<span class="symbol"></span>');
	
	/********************
		#gallery
	*********************/
	if (typeof(Galleria) != "undefined" && $('#gallery').size() > 0) {
		Galleria.loadTheme('/Scripts/galleria/themes/classic/galleria.classic.min.js');
		$('#gallery > ul').galleria({
			width:366,
			height:300,
			lightbox: true,
			lightboxFadeSpeed:0,
			showInfo: false,
			dataConfig: function(img) {
				return {
					title: (typeof($(img).attr('alt')) != "undefined") ? $(img).attr('alt') : ""
				};
			}
		});
	}
	
	
	/********************
		#carousel
	*********************/
	// TODO
	//$('#carousel img').addClass('reflect');
});

$(window).load(function(){
	/********************
		#carousel
	*********************/
	new Carousel($('#carousel > .new'));
	new Carousel($('#carousel > .used'));
	
	// Tab interaction
	$('#ft .tabs div.new, #ft .tabs div.used').click(function(){
		$('#ft .tabs div').removeClass('active');
		$(this).addClass('active');
		
		if ($(this).hasClass('new')){
			$('#carousel > .used').data('carousel').stop('none');
			$('#carousel > .new').data('carousel').play();
		}
		if ($(this).hasClass('used')){
			$('#carousel > .new').data('carousel').stop('none');
			$('#carousel > .used').data('carousel').play();
		}
			
		return false;
	});
	
	// Init carousel for new cars
	$('#carousel > .used').data('carousel').stop('none');
	$('#carousel > .new').data('carousel').play();
	
	$('.galleria-stage').attr('title', 'Klikk på bildet for stor versjon');
});
