$(document).ready(function(){

	$('.gallery-image').click( function(){
		showgalleryimage($(this).attr('id'));
	})
	
	$('#keywords').change( function(){
		if( $(this).val() == '' ){
			$(this).parent().find('#keywordid').val('')
		}
	})

	
	$( "#keywords" ).autocomplete({
		source: availableTags,
		minLength: 2,
		select: function( event, ui ) {
		
			//finder det skjulte input felt og sætter værdien ind
			$(this).parent().find('#keywordid').val(ui.item.id)
					
		}
	});
	

	if( $('#costumescontainer').length > 0 ) {
		
		showcostumes( document.forms['searchproductform']);
		
	}
	
	if($('#accessoriescontainer').length > 0 ) {
	
		showaccessories( document.forms['searchproductform']);
		
	}
	
	$('#searchproductform input').keydown(function(event) {
	
		if(event.keyCode == '13'){
			event.preventDefault(); 
		}

	});		

	$('#searchproductform select').keydown(function(event) {
	
		if(event.keyCode == '13'){
			event.preventDefault(); 
		}

	});	

	$('#fb-wall').fbWall({
		id: 'rodkjaer.dk',
		accessToken: '206158599425293|7809823973348bcf8cd72f6d.1-100000221135225|BW9n2eoyL7EYvJs7GEmv61NbBFk',
		showGuestEntries: true,
		showComments: true,
		max:10,
		timeConversion:24
	});

	if($('.sponsors').length>0){scrollsponsors()};

	$('#gallery').cycle({
		fx:			'fade',
		timeout:	8000,
		speed:		1000,
		cleartype:	true,
	    next:   '#next', 
		prev:   '#prev',
		pause:   1,
		pager:	'#indicators',
		pagerAnchorBuilder: function(idx, slide) {
			return '<a href="#">&nbsp;</a>';
		}
	});

	$('.showhideblock').click( function(){
	
		if( $(this).find('.close').length == 1 ){
			$(this).find('.close').addClass('open');
			$(this).find('.close').removeClass('close');
			$(this).find('p').show();
		} else {
			$(this).find('.open').addClass('close');
			$(this).find('.open').removeClass('open');
			$(this).find('p').hide();
		}
	
	})

	var zindex;
	$("ul.medarbejdere li").hover(
	  function () {
		zindex = $(this).css('z-index' );
	    $(this).css('z-index','10');
	    $(this).find('img').attr("src",$(this).find('img').attr("src").replace('.png','_over.png'));
	    
	  },
	  function () {
		$(this).css('z-index',zindex);
	    $(this).find('img').attr("src",$(this).find('img').attr("src").replace('_over.png','.png'));
	  }
	);

	$(document).find('.scrambleemail').each(function(){
		scrambleEmail($(this));
	});
	
	$(".employee img").hover(
	  function () {
	    $(this).attr("src",$(this).attr("src").replace('.png','_over.png'));
	  },
	  function () {
	    $(this).attr("src",$(this).attr("src").replace('_over.png','.png'));
	  }
	);

	$(".sponsors img").hover(
	  function () {
	    $(this).attr("src",$(this).attr("src").replace('-off','-on'));
	  },
	  function () {
	    $(this).attr("src",$(this).attr("src").replace('-on','-off'));
	  }
	);

	$("img.hover").hover(
	  function () {
	    $(this).attr("src",$(this).attr("src").replace('.png','_over.png'));
	  },
	  function () {
	    $(this).attr("src",$(this).attr("src").replace('_over.png','.png'));
	  }
	);

});

function showgalleryimage(id){

	$('.preview').remove();
	
	if ( $('#'+id).attr('rel') ) {
		preview = '<div class="preview" objectid="'+ $('#'+id).attr('id') +'"><img class="left-arrow" src="/ajrgfx/gfx/arrow-left.png" alt="" /><img src="'+ $('#'+id).attr('src') +'" alt="" /><img class="right-arrow" src="/ajrgfx/gfx/arrow-right.png" alt="" /><p><a href="' + $('#'+id).attr('rel') + '">'+ $('#'+id).attr('alt') +'</a></p></div>';
	} else {
		preview = '<div class="preview" objectid="'+ $('#'+id).attr('id') +'"><img class="left-arrow" src="/ajrgfx/gfx/arrow-left.png" alt="" /><img src="'+ $('#'+id).attr('src') +'" alt="" /><img class="right-arrow" src="/ajrgfx/gfx/arrow-right.png" alt="" /><p>'+ $('#'+id).attr('alt') +'</p></div>';
	}
		
	$('.preview-wrapper').prepend( preview );
	
	if(id>1){
		$('.left-arrow').click( function(){
			showgalleryimage( parseInt($(this).parent().attr('objectid')) - 1 )
		})
	} else {
		$('.left-arrow').hide()
	}

	if(id < $('.gallery-image').length){
		$('.right-arrow').click( function(){
			showgalleryimage( parseInt($(this).parent().attr('objectid')) + 1 )
		})
	} else {
		$('.right-arrow').hide()
	}
		
	if ( $('.preview').find('img').height() > 540 ){
		$('.preview').find('img').height(540);
	}
		
	var previewwidth = $('.preview').width();
	var previewheight = $('.preview').height();
		
	var offset =  $('.scroll-content').offset().left;
		
	var left = (898/2 - previewwidth/2) + 36 - offset;
	var top = (previewheight/2) - 24
		
	$('.preview').css('left',left)
	$('.left-arrow').css('top',-(top))
	$('.right-arrow').css('top',-(top))
		
	$('.preview').click( function(){
		$(this).remove();
	});	

}

var sponsorwidth;
var leftpos;

function scrollsponsors(){

	sponsorwidth = $('.sponsors table').width();

    setTimeout(function() { 
    
        $('div.request').fadeOut(); 

		$('.sponsors table').animate({
		  left: '-=300'
			}, 500, function() {
				
				leftpos =  $('.sponsors table').css('left');
				
				if( parseInt(leftpos.replace('px','')) > -(parseInt(sponsorwidth) - 946) ){
				
					scrollsponsors();
				
				} else {
				
					$('.sponsors table').fadeOut( function(){
					
						$(this).css('left','0px')
						
						$(this).fadeIn( function(){
						
							scrollsponsors();
						
						});
					
					})
				
				};
				
		});
        
    }, 5000);
    
}

function showcostumes(me){

	$('#costumescontainer').html( '<img src="/ajrgfx/ajax/ajax-loader-trans.gif" alt="" />' );

	$.get('/ajrgfx/ajax/costumes.asp?'+$(me).serialize(), function(data){
	
		$('#costumescontainer').html( data );
	
	})

}

function showaccessories(me){

	$('#accessoriescontainer').html( '<img src="/ajrgfx/ajax/ajax-loader-trans.gif" alt="" />' );

	$.get('/ajrgfx/ajax/accessories.asp?'+$(me).serialize(), function(data){
	
		$('#accessoriescontainer').html( data );
	
	})

}

function scrambleEmail(string){

	var emailarray = string.html().split('|');
	var account = emailarray[0];
	var domain = emailarray[1];
	var subject = '';
	
	if(emailarray[2]){subject=emailarray[2]};
	
	var email = account + '@' + domain;
	
	string.html('<a href="mailto:'+email+'?subject='+subject+'">'+email+'</a>');

}

function validateform( me ){

	$(me).find('input.alert').each( function(){
	
		$(this).css('border','1px solid #000');
		$(this).removeClass('alert')
	
	});
	
	var doSubmit = true;

	$(me).find('input.required').each( function(){
	
		if( $(this).attr('type') == 'checkbox' ){
		
			if( ! $(this).attr('checked') ){
			
				alert( $(this).attr('title') );
				
				doSubmit = false;
			
			}
		
		}
		
		if( $(this).attr('class').indexOf('email') > 0 ){
		
			if( !emailCheck( $(this).val() ) ){

				$(this).css('border','1px solid red');
				$(this).addClass('alert')
			
				doSubmit = false;
			
			}
		
		} else {
		
			if ( $(this).val() == '' ){
		
				$(this).css('border','1px solid red');
				$(this).addClass('alert')
				
				doSubmit = false;
		
			}
			
		}
		
	});

	$('.alert').focus( function(){
		$(this).css('border','1px solid #000');
		$(this).removeClass('alert');
	})

	
	if ( doSubmit ){ me.submit() }

}

