document.observe('dom:loaded', function(event) {

	// This will take care of proper "active" and expanding of sub-menus
	$$('.sub_nav_main').each(function(obj) {
		obj.observe('click', function(event) {
		
			// If it has sub-items, otherwise it's just a link
			if ( $('sub_'+obj.id) ) {
		
				// Remove any previously expanded items
				$$('.sub_nav_main').each(function(obj) {
					obj.removeClassName('active');
					$('sub_'+obj.id).setStyle({ display: "none" });
				});
			
				// Expand this one and set it active
				obj.addClassName('active');
				$('sub_'+obj.id).setStyle({ display: "block" });
				
			}
		
		});
	});
	
	// This will show the mouse-over image for the sub-menus
	$$('.sub_nav_sub').each(function(obj) {
	
		obj.observe('mouseover', function(event) {
		
			// Hide any other mouse-over images
			$$('.product_image_tooltip').each(function(obj) {
				$(obj.id).setStyle({ display: "none" });
			});
			
			// Get the position of the menu element and position the mouse-over image
			var position = findPos(obj);
			
			if ( $(obj.id+'_image') ) {
			
				$(obj.id+'_image').setStyle({ display: "block" });
				$(obj.id+'_image').setStyle({ top: position[1]-32+"px" });
				$(obj.id+'_image').setStyle({ left: position[0]+170+"px" });
				
			}
		
			// If there is an image to show
			//if ( $(obj.id+'_image').innerHTML.length > 0 ) {
		
				/*// Get the position of the menu element and position the mouse-over image
				var position = findPos(obj);
				$('product_image_tooltip').setStyle({ display: "block" });
				$('product_image_tooltip').setStyle({ top: position[1]-25+"px" });
				$('product_image_tooltip').setStyle({ left: position[0]+170+"px" });
				$('product_image_tooltip').innerHTML = '<img src="' + $(obj.id+'_image').innerHTML + '" alt="Product image" />';*/
				
			//}
		
		});
		
		obj.observe('mouseout', function(event) {
		
			$(obj.id+'_image').setStyle({ display: "none" });
		
		});
		
	});
	
	// New thumbnail-script for changing image and zoom
	$$('.prod_thumbs').each(function(obj) {
	
		obj.observe('click', function(event) {
		
			//$('prod_image').innerHTML = $(obj.id+'_large').innerHTML;
			$$('.big_picture_div').each(function(obj) {
				obj.setStyle({ display: "none" });
			});
			
			$(obj.id+'_large').setStyle({ display: "block" });
		
		});
		
	});
	
	// Find position of object
	function findPos(obj) {
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			do {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			} while (obj = obj.offsetParent);
			return Array(curleft,curtop);
		}
	}
	
	//fancyAlert('add_to_cart');

});

// This function will show the fancy alert

function fancyAlert(msg_id) {

	// This will contain all messages so that we can access this function like fancyAlert('add_to_cart'); etc
	var messages = new Array();
	
	messages['add_to_cart_msg'] = "Varan är nu inlagd i varukorgen";
	messages['add_to_cart_type'] = "alert";
	
	messages['makulera_order_msg'] = "Ordern är nu makulerad";
	messages['makulera_order_type'] = "alert";
	
	messages['not_available_msg'] = "Ej tillgängligt";
	messages['not_available_type'] = "error";
	
	messages['failed_login_msg'] = "Felaktigt användarnamn/lösenord";
	messages['failed_login_type'] = "error";
	
	if (messages[msg_id+'_msg']) {
	
		// Remove any previously added elements
		//$('page_wrap').removeChild(msgDiv);

		// Create element
		var msgDiv = document.createElement("div");
		
		// Add text
		msgDiv.innerHTML = messages[msg_id+'_msg'];
		
		// Add style
		$(msgDiv).addClassName('msgDiv');
		
		if ( messages[msg_id+'_type'] == "alert" )
			$(msgDiv).addClassName('tummeupp');
		else
			$(msgDiv).addClassName('tummener');
		
		// Add to document
		document.body.appendChild(msgDiv);
		//$('page_wrap').appendChild(msgDiv);
		
		setTimeout(function() {
			Effect.Fade(msgDiv, {duration:1});
		}, 3000);
	
	}

}

// This function will print the html inside <div id="content"> ... </div>
function printContent() {

	// Get html from standard print page
	new Ajax.Request('/index.php?doc=print', {
		onComplete: function(reply) {
		
			// Save content in variable
			var ajax_content = reply.responseText;
			
			// Open popup
			var print_window = window.open('','print_content','width=100,height=100');
			
			if (print_window) {
			
				//print_window.onload = function() {
				
					// Add standard content
					print_window.document.write(ajax_content);
				
					// Add this content
					print_window.document.getElementById('content').innerHTML = $('content').innerHTML;
				
					// Print page
					print_window.print();
					
					// Close popup
					print_window.close();
				
				//}
			
			}
		
		}
		
	});


}


function checkContactForm() {
	var error = '';
	
	if ($('message').value == '')
		error = ' - Du m?ste skriva ett meddelande';
	
	// Email pattern
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	
	if (!pattern.test($('email').value)) {
		if (error != '')
			error += '\n';
		
		error += ' - Du m?ste skriva en korrekt e-postadress';
	}
		
	
	if (error != '') {
		alert(error);
		return false;
	} else
		return true;
}

function hideInvoiceForm() {
	$('invoice_form').setStyle({ 'display' : 'none' });
}

function displayInvoiceForm() {
	$('invoice_form').setStyle({ 'display' : 'block' });
	hideKlarnaForm();
}

function hideKlarnaForm() {
    
    $('klarna_form').setStyle({ 'display' : 'none' });
}

function displayKlarnaForm() {
    $('klarna_form').setStyle({ 'display' : 'block' });
    hideInvoiceForm();
}

function checkCartForm() {
	if ($('delbetalning').checked == true) {
		var error = '';
		
		if ($('workplace').value == '')
			error = ' - Du m?ste ange din arbetsplats\n';
				
		if ($('permanent').options[$('permanent').selectedIndex].value == 'Ja') {
			if ($('permanent-year').value == '')
				error += ' - Du m?ste ange vilket ?r du blev anställd\n';
				
			if ($('permanent-month').value == '')
				error += ' - Du m?ste ange vilken m?nad du blev anställd\n';
		}
		
		if ($('phone_workplace').value == '')
			error += ' - Du m?ste ange ett telefonnr. till din arbetsplats';
		
		if (error != '') {
			alert(error);
			return false;
		} else
			return true;
	} else
		return true;
}


