
/** Start navigation dropdown **/
$(document).ready(function(){

	$('#main_nav > li').mouseenter(function() { //register mouse in event on all main menu items

		if ($(this).children('.dropdown_wrapper').size() == 1) { //check if submenu is available

			$('.dropdown_wrapper').hide(); //hide all dropdowns

			$(this).children('.dropdown_wrapper').show(); //show current dropdown

		} else{ //do this if item has no submenus
			$('.dropdown_wrapper').hide(); //hide all dropdowns
		}
	}).mouseleave(function(){ //register mouse out event
		$('.dropdown_wrapper').hide(); //hide all dropdowns
	});

	// Language navi
	$('#language > li').mouseenter(function() { //register mouse in event on all main menu items

		if ($(this).children('.dropdown_wrapper').size() == 1) { //check if submenu is available

			$('.dropdown_wrapper').hide(); //hide all dropdowns

			$(this).children('.dropdown_wrapper').show(); //show current dropdown

		} else{ //do this if item has no submenus
			$('.dropdown_wrapper').hide(); //hide all dropdowns
		}
	}).mouseleave(function(){ //register mouse out event
		$('.dropdown_wrapper').hide(); //hide all dropdowns
	});			
	
});

/** End navigation dropdown **/



/** Start lightbox **/
$(document).ready(function() {
	//register lightbox triggers
	$('.lightbox_trigger').click(function(){
		showLightbox($(this));
	});

	//register lightbox close
	$('#lightbox_close_button').click(function(){
		closeLightbox();
	});
});

function showLightbox(el) {
	//console.log('showLightbox() running');
	var imgUrl = $(el).attr('rel');
	$('#lightbox_wrapper').fadeIn(300, function(){
		//$(document).pngFix();
	});

	$('#lightbox_loading').show();

	$.ajax({
	  url: '/parts/lightbox.php',
	  data: 'iid='+imgUrl,
	  success: function(xhr) {
			insertContent(xhr);
	  },
	  error: function() {
	  	alert('There was an error loading the data - Please contact the website\'s owner! ');
	  	closeLightbox();
	  }
	});
}

function insertContent(xhr) {
	var img = $(xhr).children('img');
	var title = $(xhr).children('#lightbox_title');
	var text = $(xhr).children('#lightbox_text');

	//preload image
	$.ajax({
	  url: $(img).attr('src'),
	  success: function(xhr) {
			//console.log('image loaded');
			$('#lightbox_loading').hide();
			$('#lightbox_content_ajax').html('');
			$('#lightbox_content_ajax').append(img);
			$('#lightbox_content_ajax').append(title);
			$('#lightbox_content_ajax').append(text);
	  },
	  error: function() {
	  	alert('There was an error loading the image - Please contact the website\'s owner! ');
	  	closeLightbox();
	  }
	});
}

function closeLightbox() {
	//console.log('closeLightbox() running');
	$('#lightbox_wrapper').fadeOut(300);
}
/** End lightbox **/


/* More Info Form */
function toogleField(id, value){
  if(value == 100){ // 100 = other
    $(id).stop(true, true).fadeIn('fast');
  }
  else{
    $(id).stop(true, true).fadeOut('fast');
  }
}
/* /More Info Form */
