/* To reduce the number of global variables that we must create, as well as remove any possibilities of the $ symbol clashing with other JavaScript libraries, 
 * let’s wrap our plugin in a self-executing anonymous function. 
 */
(function($) {

	$(document).ready(function(){
		
		//Do something for all pages
		$("ul.menu li.selected:last").parent().addClass('selected_menu');
		initBlogBox();
		initFinishViewer();
		initModelImageLinks();
		initLightBox();
		initHideBoxes();
			
	});
	
	
	// **************************
	// PRIVATE FUNCTIONS
	// **************************

	function privateExampelFunction() {
		
		//code here
		
	};	
	

	function initFinishViewer() {
		
		$('.finishes li').hover(
			function () {
				$(this).css('background-image', $(this).css("background-image").replace(".jpg", "_over.jpg") );
			},
			function () {
				$(this).css('background-image', $(this).css("background-image").replace("_over.jpg", ".jpg") );
			}
		).click(function() {
			
			$('.finishes #finish_big').remove();
			var p = $('<p></p>').attr('id','finish_big').insertBefore( ".finishes ul" );
			var bgImage = $(this).css("background-image").replace("_over.jpg", ".jpg");
			var span = $('<span></span>').appendTo(p);
			span.css("background-image", bgImage.replace(".jpg", "_big.jpg"));
			
			//Hide "no image" bg for a second to give image some time to show
			span.css( "background-color", $('body').css('background-color') );
			setTimeout(function() { span.css( "background-color", 'transparent' ) },200);
			
		});
		
	};	
	
	function initModelImageLinks() {
		$('.productserie .model').css('cursor','pointer').click(function() {
			window.location = $('a.button', $(this)).attr('href');
		});
	}
	
	function initLightBox() {
		/*
		$("a[rel=lightbox_group]").fancybox({
			'transitionIn'		: 'fade',
			'transitionOut'		: 'fade',
			'overlayOpacity'  	: 0.6,
			'overlayColor' 		: '#000',
			'titlePosition' 	: 'over',
			'padding'			: 1,
			'titleShow'			: false,
			//'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			//	return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
			//}
		});
		*/
	}
	
	/*function initTechBackgrounds() {
		$('a.show_tech').click(function() {	
			if (!$(this).next().parent().children().is(':animated')) {
				if ( $(this).hasClass('selected') ) {
					$('body').css('background-image', $('body').css("background-image").replace("_tech.jpg", ".jpg") );
				} else {
					$('body').css('background-image', $('body').css("background-image").replace(".jpg", "_tech.jpg") );
				}
			}
		});
	}*/
	function initTechBackgrounds() {
		$('a.show_tech').click(function() {	
			if (!$(this).next().parent().children().is(':animated')) {
				if ( $(this).hasClass('selected') ) {
					$('#bodydouble').fadeOut('slow', function() {
						$('#bodydouble').remove();
					});
				} else {
					var techImage = $('body').css("background-image").replace(".jpg", "_tech.jpg");
					$('<div></div>').attr('id','bodydouble').css("background-image", techImage).insertBefore('#page').fadeIn('slow');
				}
			}
		});
	}
	
	function initHideBoxes() {
		
		initTechBackgrounds(); 
		
		var accordion = false;
		if (accordion) {
			$(".hidebox").accordion({ 
				autoheight: false 
			});
		} else {
	
			$('.hidebox .header').click(function() {
			
				var header = $(this);
				
				if (!header.next().parent().children().is(':animated')) {
					var hide = header.hasClass('selected');
					if (!hide) { header.addClass('selected');}
					header.next().animate({
						height: 'toggle'
					}, 500, function() {
						if (hide) {header.removeClass('selected');}
					});
				}
				
				return false;
				
			});
			
			$('.header:not(.selected)').next().hide();
			
		}
		
	}
              
	function initBlogBox() {
		
		$("#blogbox").removeAttr('onclick').unbind('click').click(function() { 
			//$('ul.menu li a[target="_blank"]').first().click(); can not cause link to be clicked this way
			window.open('http://marten.se/blog/','_blank');
			return false; 
		});
		
		$('#latestBlog').html("&nbsp;");
		
		var url = $(location).attr('href');
		url = url.match(/(http:\/\/.[^/]+)/)[1];
		url = (url == "http://localhost") ? url + "/marten.se/_ajax/test.php": url + "/_ajax/latestblogpost.php";
		
		// This gave error on IE before changing to jquery 1.5.2 for some reason.
		$.post(url, { chars: 100 }, function(data){
			$('#latestBlog').html(data);
		});
		
		
		//$('#blogbox').css({ opacity: 0.7 });
		if ( $("#blogbox").hasClass('hidden') ) {
			$('"#blogbox #latestBlog').css('margin-left','0px').css('width','0px').css({ opacity: 0 });
		}
		
		$("#blogbox").mouseenter(function() {
			$('#latestBlog').stop(true, false).animate({ 
				width: "180px",
				marginLeft: "10px"
			}, 500, function() {
				$("#latestBlog").css({ opacity: 1 });
			});
		}).mouseleave(function() {
			$("#latestBlog").css({ opacity: 0 });
			$("#latestBlog").stop(true, false).animate({
				width: "1px",
				marginLeft: "0px"
			}, 500 );
		});

	};

})(jQuery);


// **************************
// PUBLIC FUNCTIONS
// **************************


function printmail(name,domain,code) {
    var mail = name +'@'+ domain +'.'+ code;            
    document.write('<a href=\"mailto:'+ mail +'\">'+ mail +'</a>');
}

function printmail2(name,domain,code,text) {
    var mail = name +'@'+ domain +'.'+ code;            
    document.write('<a href=\"mailto:'+ mail +'\">'+ text +'</a>');
}

function fadeOut(id) {
    var box = $('#' + id);
    setTimeout(function () {
        box.fadeOut('slow', function() {
            //action when complete
        });
    }, 3000);
}


function initFloatPanel(elClass, top) {

	var msie6 = ($.browser.msie && $.browser.version < 7);
	if (!msie6) {
	
		var wrapper = $(elClass);
	
		var wrapperPos = wrapper.offset().top;
		var wrapperHeight = wrapper.height();
		var wrapperEnd = wrapperPos + wrapperHeight;
		var wrapperMaxEnd = wrapperEnd - top;
		
		updateFloatPanels(wrapper, wrapperMaxEnd, wrapperEnd, wrapperHeight, top);
		
		$(window).scroll(function(){
			updateFloatPanels(wrapper, wrapperMaxEnd, wrapperEnd, wrapperHeight, top);
		});
		$(window).resize(function() {
			updateFloatPanels(wrapper, wrapperMaxEnd, wrapperEnd, wrapperHeight, top);
		});
		
	}

}

function updateFloatPanels(el, startPos, endPos, elHeight, top) {

	var scrollY = $(window).scrollTop();
	var windowHeight = $(window).height(); //Get new height if windows size is changed
	
	if (scrollY > (endPos - windowHeight)) {
		$('.promopanels-wrapper ul').css("position", "absolute");
		$('.promopanels-wrapper ul').css("top", "auto");
	} else if (scrollY < (startPos - windowHeight)) {
		$('.promopanels-wrapper ul').css("position", "absolute");
		$('.promopanels-wrapper ul').css("top", -top + "px");
	} else {
		$('.promopanels-wrapper ul').css("position", "fixed");
		$('.promopanels-wrapper ul').css("top", windowHeight - elHeight + "px");
	}
	
}


