var bookshelfcount = 4;

var tabLinks = new Array();
var contentDivs = new Array();

$(document).ready(function()
{
	// Shop Opening hours loop
	shopLoopStart('#opening li');

	// hover on products - infopanel
	$('.bookshelf ul.mycarousel li .picture').each(function(i)
	{
		$(this).hover(function()
		{
			var liPos = $(this).parent().offset(),
			wrapPos = $('#wrapper').offset();

			$('.bookInfo .info:eq('+i+')').css({ top: liPos.top - wrapPos.top - 100, left: liPos.left - wrapPos.left + 60 }).show();
		}, 
      		function ()
		{
			$('.bookInfo .info').hide();
		});
	});


	// hover on infopanel
	$('.bookInfo .info').each(function(i)
	{
		$(this).hover(function()
		{
			$('.bookInfo .info:eq('+i+')').show();
		}, 
      		function ()
		{
			$('.bookInfo .info').hide();
		});
	});

	// Setur sjálfgefin gildi á input
	$('#postlisti_netfang').originalValue();

	// put sharing buttons on the page
	if ($('.share .share-button').length) installSharing();

	$('.mycarousel').each(function(){
		var xxx = this;
		function mycarousel_initCallback(carousel) {
		    var jumpTo = jQuery(xxx).parents('.bookshelf').find('>ul')
			jumpTo.find('a').bind('click', function() {
		        carousel.scroll( ((jQuery.jcarousel.intval(jQuery(this).text())-1)*bookshelfcount)+1 );
			jumpTo.find('li.active').removeClass('active');
			jQuery(this).parent().addClass('active');
		        return false;
		    }).filter(':first').parent().addClass('active');
		}

		function dotHandler(carousel)
		{
			var jumpers = carousel.container.parent().find('#jumpTo');
			jumpers.find('li.active').removeClass('active');
			jumpers.find('li:nth-child(' + (carousel.last/bookshelfcount) + ')').addClass('active');
		}
	
		// bookshelf init
		$(xxx).jcarousel({
				itemLastInCallback: { onAfterAnimation: booshelfpageing },
				visible: bookshelfcount,
				animation: 'slow',
				scroll: bookshelfcount,
				initCallback: mycarousel_initCallback,
				itemFirstInCallback : dotHandler
		});
	});

	// Add html elements
	$('#submenu').addClass('javascript');
	$("#submenu ul ul").prepend('<li class="back"><a href="#submenu=0">Til baka</a></li>');
	
	// Get menu height
	function getHeight(id){
		id.css('position','absolute');
		id.css('height','auto');
		height = id.height();
		id.css('position','relative');
		id.css('height','0px');
		
		return height-1;
	}
	
	// When clicked on menu item
	/*$('#submenu a').click(function ()
	{
		// Set submenu position
		top = $(this).parent().position().top;
		$(this).parent().find('ul').css('top', -($(this).height()+top) + 'px');
		
		// Hide all submenus
		$('#submenu ul ul').hide();
		
		// Show current submenu
		$(this).parent().find("ul").show();
		$(this).parents().filter('ul').show();
		
		if( $(this).parent().find("ul").length )
		{
			// Get position
			pos = $("#submenu ul:first").css("left");
			pos = pos=="auto" ? 0 : Number( pos.split('px')[0] );
			
			// Scale mask
			pHeight = getHeight( $(this).parent().parent() ) + $("#submenu .header").height();
			cHeight = getHeight( $(this).parent().find("ul") ) + $("#submenu .header").height();
			
			// Set default height
			$("#submenu").animate({ height: pHeight }, 0);
			
			if( pHeight > cHeight )
			{
				// Before
				$("#submenu").animate({
					height: cHeight
				}, 500,function() {
    							$("#submenu ul:first").animate({
						left: pos-$("#submenu").width()
					}, 500);
				});
			}else{
				// After
				$("#submenu ul:first").animate({
					left: pos-$("#submenu").width()
				}, 500,function() {
    							$("#submenu").animate({
						height: cHeight
					}, 500);
				});
			}
			
		}
	});
	
	// When clicked on back
	$('#submenu li.back a').click(function ()
	{
		pos = $("#submenu ul:first").css("left");
		pos = pos=="auto" ? 0 : Number( pos.split('px')[0] );
		
		// Scale mask
		pHeight = getHeight( $(this).parent().parent() ) + $("#submenu .header").height();
		cHeight = getHeight( $(this).parent().parent().parent().parent() ) + $("#submenu .header").height();
		
		if( pHeight > cHeight )
		{
			// Before
				$("#submenu").animate({
					height: cHeight
				}, 500,function() {
    							$("#submenu ul:first").animate({
						left: pos+$("#submenu").width()
					}, 500);
				});
		}else{
			// After
				$("#submenu ul:first").animate({
					left: pos+$("#submenu").width()
				}, 500,function() {
    							$("#submenu").animate({
						height: cHeight
					}, 500);
				});
		}
		
	});*/

	$('ul.toplistTabs a').click(function()
	{
		$(this).addClass('selected').parent().parent().find('a.selected').not(this).removeClass('selected');
		$('#'+this.hash.substr(1)).show().siblings('.tabContent').hide();
		return false;
	});
})

// switching selected on paging dots
function booshelfpageing(carousel, item, idx, state)
{
	// setting selected on paging dots
	var pagenumber = Math.ceil( idx / bookshelfcount);
	$('.bookshelf .pos span').removeClass('selected');
	$('.bookshelf .pos .item' + pagenumber).addClass('selected');

	// changes directions on infobox
	var halfcount = ( Math.floor( bookshelfcount / 2 ) );

	$('.bookshelf .picture .info').removeClass('leftside');
	for( i=idx; i > (idx - halfcount); i=i-1 )
	{
		$('.bookshelf .picture .infoitem' + (idx - (idx-i)) ).addClass('leftside');
	}
}

function shopLoopStart( CSS )
{
	var elements = $(CSS);
	if( elements.length > 1 )
	{
		setInterval(  "shopLoop('"+CSS+"')", 7000 );
	};
};

function shopLoop( CSS )
{
	var Now = $(CSS+':visible').fadeOut( 2000 );
	var Next = ( Now.next().is(CSS) && Now.next() ) || $(CSS+':first' );
	Next.fadeIn( 2000 );
};

function validate(form) {
	var isValid = true;
	var elemIsValid = true;
	var errString = "";
	var errMsgDetail = "";
	
	for (var i = 0; i < form.elements.length; i++) {
		var elem = form.elements[i];
		if (elem.className.indexOf('reqd') > -1) {
			/* input, select og textarea er höndlað á sama hátt .... */
			if ((elem.tagName == "INPUT") || (elem.tagName == "TEXTAREA") || (elem.tagName == "SELECT")) {			
				if (elem.className.indexOf('emailval') > 0) {
					elemIsValid = isValidEmail(elem.value);
				} else if(elem.className.indexOf('number') > 0) {
					elemIsValid = isValidNumber(elem.value, 7); 
				} else if(elem.className.indexOf('kennitala') > 0) {
					elemIsValid = isValidNumber(elem.value, 10); 
				} else {
					elemIsValid = (elem.value != '');
				}
				
				if (!elemIsValid) {
					if(elem.title == 'Nafn') {
						errMsgDetail = 'Athugaðu að skrifa nafnið þitt í reit merktan "Nafn"';			
					} else if(elem.title == 'Netfang'){
						errMsgDetail = 'Sláðu inn netfang hér fyrir neðan';
					} else if(elem.title == 'Skilaboð'){
						errMsgDetail = 'Athugaðu að skrifa erindið þitt í reitinn merktan "Skilaboð"';
					} else {
						errMsgDetail = 'Athugaðu að fylla í reit merktan "' + elem.title + '"';
					}

					errString += '<label for="' + elem.id + '"><strong>' + elem.title + ' vantar: </strong></label>' + errMsgDetail + '<br/>'
					elem.focus();
					elem.style.borderColor = '#FF4A4A';
					elem.style.backgroundColor = '#FDFAD0';
					isValid = false;
				} else {
					elem.style.borderColor = '';
					elem.style.backgroundColor = '';
					elemIsValid = true;
				}
			}			
		}
		else if(elem.className.indexOf('kennitala') > 0) {
			elemIsValid = isValidNumber(elem.value, 10); 
			
			if (!elemIsValid) {
				errString += elem.title + ' er ekki rétt út fyllt!<br/>'
				elem.focus();
				elem.style.borderColor = '#FF4A4A';
				elem.style.backgroundColor = '#FDFAD0';
				isValid = false;
			} else {
				elem.style.borderColor = '';
				elem.style.backgroundColor = '';
				elemIsValid = true;
			}
		}
		
	}
	if(!isValid) {
		document.getElementById('notice').innerHTML = errString;
		document.getElementById('notice').style.visibility = "visible";
	}
	else
	{
		document.getElementById('spamAction').name = 'action';
		document.getElementById('spamAction').value = 'process';
	}
	return isValid;
}

function isValidEmail(value) {
	return (value.indexOf(".") > 2) && (value.indexOf("@") > 0);
}

function isValidNumber(value, number) {
	var elemIsValid = true;
	var stripped = value.replace(/[\(\)\.\-\ ]/g, '');
	
	if (!(stripped.length == number)) {
		elemIsValid = false;
	}

	if(isNaN(stripped)) {
	     elemIsValid = false;
   	}

	if (stripped.length == 0) {
		elemIsValid = true;
	}
	
	return elemIsValid;
}

// put product sharing buttons on the page
function installSharing() {
	$('.share-button').css('display','inline');
}


function disable(theID)
{
	document.getElementById('splash').style.visibility = 'visible';
}

function openPopUp(page)
{
	window.open('/pages/' + page, 'Skilmálar','width=400,height=400,toolbar=no,resizable=no, menubar=no');
	return false;
}

function openForm() {
	document.getElementById('commentform').style.display = "block"; 
}

function closeForm() {
	document.getElementById('commentform').style.display = "none"; 
}

function lisa_viewfinder(url,viewname,w,h) { window.open(url,viewname||'_blank','scrollbars,resizable,width='+(w||180)+',height='+(h||180)).focus(); return false }
