$(document).ready(function(){
	$('.clickGetA').click(function(e){
	    var link = $(this).find('a').attr('href');
		location.href = link;
		return false;
	});
	if($('.startPage').length == '1'){
		var nav = $('<div id="backgroundNav" />');
		var left = $('<div class="leftArrow"/>');
		var right = $('<div class="rightArrow"/>');
		nav.append(left, right);
		$('.pageWidthRow.middleRow').append(nav);
		
		$( function() {
			//if($('html').width() > 1600){
			    $.vegas('slideshow', {
			    	preload : true,
			    	backgrounds:[
			    	{src:'/img/startBackFolder/01_B.jpg', fade: 500},
					{src:'/img/startBackFolder/02_B.jpg', fade: 500},
					{src:'/img/startBackFolder/03_B.jpg', fade: 500},
					{src:'/img/startBackFolder/4.jpg', fade: 500},
					{src:'/img/startBackFolder/5.jpg', fade: 500}
					]
			    });	
			/*}
			else{
			    $.vegas('slideshow', {
			    	preload : true,
			    	backgrounds:[
					{src:'/img/startBackFolder/1.jpg', fade: 500},
					{src:'/img/startBackFolder/2.jpg', fade: 500},
					{src:'/img/startBackFolder/3.jpg', fade: 500}
					]
			    });
			}*/
		    $.vegas('pause');
	    });
	}
	else{
		if($('.menuItemCollection.alwaysShow').length == 0) {
			//if($('html').width() > 1600){
				$.vegas({
	                src:'/img/startBackFolder/01_B.jpg'
	            });
			/*}
			else{
				$.vegas({
	                src:'/img/startBackFolder/1.jpg'
	            });
			}*/
			
        }
	}
	//$('.pageWidthRow.topRow').css('width', $('html').width() +'px');
	
	$('#backgroundNav .leftArrow').click(function(){
		$.vegas('previous');
	});
	$('#backgroundNav .rightArrow').click(function(){
		$.vegas('next');
	});
	
	
	
	function changeMiddleHeight(){
		$('#middle').css('height', function () {
			if ($(this).height() > ($('html').height() - 100)) {
				return $(this).height();
			}
			
			return $('html').height() - 100;
		});//($('html').height() - 100) + 'px');
	}
	
	changeMiddleHeight();
	
	$(window).resize(function(){
		changeMiddleHeight();		
	});
	
	$('.clickGetA').bind('click', function() {
		location.href = $(this).find('a').attr('href');
	});
	
	var height = $(window).height();
	var width = $(window).width();


	$('.mainMenu > li').hover(
		function() {
			$(this).addClass('hover');
/*			if($(this).hasClass('menuItemCollection')){
				$('.collectionMenuRow').fadeIn('fast');
				$(this).addClass('active');
			}
			else{*/
				if($('.menuItemCollection').hasClass('active')){
					$('.collectionMenuRow').fadeOut('fast');
					$('.menuItemCollection').removeClass('active');
				/*}*/
				
			}
		},
		function() {
			$(this).removeClass('hover');
		}
	);
	
	
	$('#map_canvas').css({'width': width+'px ', 'height': (height-142)+'px'});
	
	
	$('.collectionMenuRow').mouseleave(function() {
		$('.collectionMenuRow').fadeOut('fast');
		$('.menuItemCollection').removeClass('active');
	});
	
	
	/* empty inputs */
	var defaultValues = {};
	$.each($('input, textarea'), function() {
		if(!$(this).hasClass('submit')){
			defaultValues[$(this).attr('name')] = $(this).val();
		}
	});
	
	$('input, textarea').focus(function() {
		if(!$(this).hasClass('submit')){
			if ($(this).val() == defaultValues[$(this).attr('name')]) {
				$(this).val('');
			}
			$(this).removeClass('inactive');
		}
	})
	.blur(function() {
		if ($(this).val().replace(/\s/g, '') == '') {
			$(this).val(defaultValues[$(this).attr('name')]);
			$(this).addClass('inactive');
		}
	});
	
	/* Sumenu under mynishiki */
	$('.hasChildren').each(function(){
		$(this).click(function(){
			if($(this).hasClass('open')){
				$(this).find('ul').slideUp();
				$(this).removeClass('open');
			}
			else{
				$('.children').slideUp();
				$('.hasChildren').removeClass('open');
				$(this).find('ul').slideDown();
				$(this).addClass('open');
			}	
	
		})	

	});
	$('.hasChildren').find('ul.active').slideDown().parent().addClass('open');
	
	
	$('#bikeChoosers .bike').each(function(){
		if(!$(this).hasClass('active')){
			//$(this).css('opacity', '0.7');
		}
		else{
			//$(this).find('img').css('height','77px');
		}
	});
	
	$('.rightFindBikeBox .selectBox').hover(
		function(){
			$(this).find('.bikeList').fadeIn(300);
		},
		function(){
			$(this).find('.bikeList').fadeOut(300);
		}
	);
	

	$('#bikeChoosers .bike').hover(
		function(){
            $(this).parent().css('zIndex', '11');
			if(!$(this).hasClass('active')){
				$('.infoBox').hide();
				$(this).find('.infoBox').show();
			}
			//$(this).find('img').animate({'height': '77px'}, 400);
		},
		function(){
			$(this).find('.infoBox').hide();
			$(this).parent().css('zIndex', '0');
		}
	);
	
	$('.bigger').fancybox();
	
	// START PAGE
	
	$('.startPromos .item:not(.nofollow)').click(function(e) {
	   e.stopPropagation();
	   e.preventDefault();
	   
	   location.href = $(this).find('a').attr('href');
	   
	   return false;
	});
	
	if($('.startPage').length == '1'){
		var resizeStartPage = function(){
			$('#middle').css({'height': (height-142)+'px'});
		};
		/*
		var bgImgStartPage = function(){
			
			var imgHeight = 1000;
			var bgPadding = ((height-imgHeight)/2)+70;
			
			
			$('body.startPage').css('background-position', 'center '+(bgPadding)+'px');
		*/
			/*if(height < '900'){
				$('body.startPage').css('background-image', 'url(/img/page/start/1/bg_C.jpg)');
			}
			else if(height > '899'){
				$('body.startPage').css('background-image', 'url(/img/page/start/1/XL.jpg)');
			}
			
			if(width > '1600'){
				$('body.startPage').css('background-image', 'url(/img/page/start/1/XL.jpg)');
			}*/
		/*
		};
		*/
				
		/*bgImgStartPage();*/
		resizeStartPage();
		
		$(window).resize(function() {
			
			height = $(window).height();
			width = $(window).width();
			//$('.pageWidthRow.topRow').css('width', width +'px');
			/*bgImgStartPage();*/
			resizeStartPage();
		});		
		
	}
	
	$('#search').parent().submit(function(e) {
	   e.stopPropagation();
	   e.preventDefault();
	   
	   location.href = '/search/query/' + $(this).find('input').val().replace(/ /g, '_');
	   
	   return false;
	});
	
    if ($('.searchBox').length > 0) {
        var locked = false;
        
        var attachSearchResults = function(query) {
            locked = true;
            
            $.ajax({
                'url': '/search/search',
                'type': 'post',
                'data': {
                    'search': query
                },
                'success': function(response) {
                    if (response === '') {
                        response = [];
                    }
                    
                    if (typeof response !== 'object') {
                        response = JSON.parse(response);
                    }
                    
                    $('#autoComplete ul').empty();
                    
                    $.each(response, function() {
                        var li = $('<li></li>');
                        li.append('<a href="/' + this.collection.replace(/ /g, '_') + '/' + this.bicycle.replace(/ /g, '_') + '"><span>' + this.collection + '</span> - ' + this.bicycle + '</a>');
                        $('#autoComplete ul').append(li);
                    });
                    
                    $('#autoComplete').show();
                    
                    $(document.body).click(function(e) {
                        $('#autoComplete ul').empty();
                        $('#autoComplete').hide();
                        $(document.body).unbind('click');
                    });
                    
                    locked = false;
                }
            });
        }
        
        $('.searchBox input').keyup(function(e) {
            if (e.keyCode == 13) {
                location.href = '/Sök/' + $(this).val().replace(/ /g, '_');
            }
            if ($(this).val().length >= 3 && !locked) {
                attachSearchResults($(this).val());
            }
        });
        $('.searchBox input').click(function(e) {
            if ($(this).val().length >= 3) {
                attachSearchResults($(this).val());
            }
        });
    }
    
    $('.middle a.showDescription').toggle(
        function(e) {
            e.stopPropagation();
            e.preventDefault();
            
            $(this).parent().parent().parent().find('.bikeArchiveInfoBox').slideDown();
        },
        function(e) {
            e.stopPropagation();
            e.preventDefault();
            
            $(this).parent().parent().parent().find('.bikeArchiveInfoBox').slideUp();
        }
    );
});

