$(document).ready(function(){
	
	var slideShowPlay = true;
	var searchquote = "";

	// Search string - Focus:
	$(".searchfield", this).live('focus', function(){
			searchquote = $(this).attr("data-text");
			if ($(this).attr("value") == searchquote){ $(this).attr("value", ""); }
	});
			
	// Search string - Blur:
	$(".searchfield").live('blur', function(){
			if ($(this).attr("value") == "" || $(this).attr("value") == " "){ $(this).attr("value", searchquote); }
	});
	
	// Slideshow:
	if ($('div#slideshow').length > 0){
		var slideshow = $('div#slideshow div#panels').nivoSlider({
			effect: 'boxRain,boxRainReverse',
			slices: 30,
			boxCols: 20,
			boxRows: 4,
			animSpeed: 500,
			pauseTime: 6000,
			startSlide: 0,
			controlNav: false,
			directionNav: false,
			directionNavHide: true,
			controlNavThumbs: false,
			keyboardNav: true,
			pauseOnHover: false,
			captionOpacity: 0
		}).data('nivoslider');
		
		$('div#slideshow').click(function(){
			if (slideShowPlay){
				slideshow.stop();
				slideShowPlay = false;
			} else {
				slideshow.start();
				slideShowPlay = true;
			}
		});
		
	}
	
	// Object slideshow:
	if ($('div.slideshow').length > 0){
		setTimeout(initiateSlideshow, 500);
	}
	
	// Fancybox for object slideshow:
	$("a.fancybox").fancybox({
		'titleShow'     : false,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'centerOnScroll': true,
		'easingIn'      : 'easeOutBack',
		'easingOut'     : 'easeInBack'
	});
	
	// Fancybox for video:
	$("a.video").fancybox({
		'type'			: 'iframe',
		'titleShow'     : false,
		'width'			: 853,
		'height'		: 480,
		'centerOnScroll': true,
		'transitionIn'	: 'fade',
		'transitionOut'	: 'fade'
	});
	
	var drawerLimit = 600;
	
	// Drawer code:
	/*if ($('div#objectDescription div.right').height() > drawerLimit){
		var origHeight = $('div#objectDescription div.right').height();
		$('div#objectDescription div.right').attr('data-height', origHeight);
		$('div#objectDescription div.right').css({
			height: drawerLimit
		});
	} else {
		$('div.drawer').hide();
	}
	
	$('div.drawer').click(function(){
		if ($(this).hasClass('closed')){
			var newHeight = $(this).parent().attr('data-height');
			$(this).parent().animate({ height: newHeight });
			$(this).removeClass('closed');
			
			if ($(this).attr('data-lang') == 'sv'){ $(this).html('Se mindre information'); }
			else { $(this).html('See less information'); }
			
		} else {
			var newHeight = drawerLimit;
			$(this).parent().animate({ height: newHeight });
			$(this).addClass('closed');
			
			if ($(this).attr('data-lang') == 'sv'){ $(this).html('Se mer information'); }
			else { $(this).html('See more information'); }
		}
	});*/
	
	// Checkbox image selection:
	$('div.category img, div.category label').click(function(){
		if ($(this).parent().find('input:checkbox').is(':checked')){
			$(this).parent().find('input:checkbox').attr('checked', '');
		} else {
			$(this).parent().find('input:checkbox').attr('checked', 'checked');
		}
	});
	
	// Broschure ordering:
	$('form#broschure').submit(function(e){
		var allSet = true;
		var currentLang = $(this).attr('data-lang');
		var ajaxFile = $(this).attr('action');
		
		$('form#broschure input:text').each(function(){
			if ($(this).val() == ""){ allSet = false; }
		});
		
		if (allSet){
			
			var dataArray = new Array;
			$('form#broschure input:text').each(function(x){
				dataArray[x] = $(this).val();
			});
			
			$.ajax({
				url: ajaxFile,
				type: "POST",
				data: { info: dataArray }, 
				success: function(msg){
					if (msg == "INVALID"){
						if (currentLang == "sv"){
							alert("Du har angett en ogiltig e-post adress, vänligen försök igen!");
						} else {
							alert("You entered an invalid email address, please try again!");
						}
					} else if (msg == "SUCCESS"){
						if (currentLang == "sv"){
							alert("Tack för din beställning!");
						} else {
							alert("Thank you for your order!");
						}
						
						// Clear fields:
						$('input.field').attr('value', '');
						
					} else if (msg == "ERROR"){
						if (currentLang == "sv"){
							alert("Ett fel uppstod!");
						} else {
							alert("An error occured!");
						}
					}
				}, error: function(){
					if (currentLang == "sv"){
						alert("Ett fel uppstod!");
					} else {
						alert("An error occured!");
					}
				}
			});
		
		} else {
			if (currentLang == "sv"){
				alert("Du har glömt att fylla i något fält!");
			} else {
				alert("You forgot to fill a field!");
			}
		}
		
		
		e.preventDefault();
	});
	
	// Block main page click:
	$('ul.menu li a').click(function(e){	
		if ($(this).parent().attr('id') == "menu-item-54" || $(this).parent().attr('id') == "menu-item-53"){
			e.preventDefault();
		}
	});
	
});

// Print images:
function printImages(objectID){
	var images = window.open("/wp-content/plugins/navark-admin/files/getObjectImages.php?id=" + objectID, "Object images", "status = 1, height = 600, width = 850, resizable = 1, scrollbars = 1, location = 0, menubar = 0, toolbar = 0, status = 0");
}

function initiateSlideshow(){
	$("div.tabs ul").tabs("div.slideshow div.panels img", {
		effect: 'fade',
		fadeInSpeed: 'slow',
		fadeOutSpeed: 'slow',
		rotate: true
	}).slideshow({
		clickable: false,
		prev: '.prev',
		next: '.next'
	});
}
