/* Declare a namespace for the site */
var Site = window.Site || {};

/* Create a closure to maintain scope of the '$'
   and remain compatible with other frameworks.  */
(function($) {
	
	//same as $(document).ready();
	$(function() {
	});

	$(window).bind("load", function() {
	});
	
	// Pop-box
	$(document).ready(function() {
	    $("a.infobox").fancybox({ 'autoDimensions' : false,
                                  'titleShow'      : false,
                                  'width'          : 520,
                                  'height'         : 200
	    });

	    $('ul.products-five li.product:nth-child(5n+1)').addClass('alpha');
	    $('ul.products-five li.product:nth-child(5n)').addClass('omega');
	    
	    $("a.confirm").click(function() {
	    	var href = $(this).attr('href');
	    	fancyConfirm($(this).attr('msg'), function(ret) {
	    		if (ret == 1) {
	    			document.location.href =  href;
	    		}
	    	});
	    	return false;
	    } );
	});


})(jQuery);

$(function(){$('#tabs').tabs()});
jQuery(document).ready(function() {
    jQuery('#mycarousel').jcarousel();
});

jQuery(document).ready(function() {
    jQuery('.tagmenu .mycarousel').jcarousel({
		visible:	5,
		scroll:		5
    });
	  jQuery('.featured-campaign .mycarousel').jcarousel({
		scroll:		3,
        visible: 	3
      });
});


function fancyBoxConfig() {
	return {
		'autoDimensions'	:	false,
		'titleShow'			:	false,
		'width'				:	520,
		'height'			:	200
			};
}	
$('.slideshow').cycle({ 
	fx: 'fade',
	speed:       1000,
	timeout:     7000,
	pager:      '#pager',
	pagerEvent: 'mouseover',
	pauseOnPagerHover: true,
	autostop: false,
	fastOnEvent: true
});
$('.topCampaigns').cycle({
	fx: 'scrollLeft',
	speed:       1000,
	timeout:     6000,
	pager:      '#pager',
	pagerEvent: 'mouseover',
	pauseOnPagerHover: true,
	autostop: false,
	fastOnEvent: true
});

// AddThis config
var addthis_config = {
	data_track_clickback : true,
	services_exclude     : "googlebuzz,tellmypolitician",
	ui_language          : "no"
};
function replaceBrokenImages() {
	$('img.cover').error(function() {
			$(this).attr('src',contextPath+'/images/bildemangler_218.gif');
		  });
 }

// "Look inside book" function
$(document).ready(function(){
	$("a#issuu").fancybox({
		'autoDimensions' : false,
		'titleShow'      : false,
		'height'         : 687,
		'width'          : 800 }
	);
});

$(document).ready(function() {
    // Change text of submit button on order summary page
	$('input[name="paymentType"]').click(function(){
		if ($('input[name="paymentType"]:checked').val() == 'CREDITCARD')
			$('#store-order_order-summary_store').attr("value","Gå til betaling");
		else if ($('input[name="paymentType"]:checked').val() == 'INVOICE')
			$('#store-order_order-summary_store').attr("value","Fullfør bestilling");
	});
});

// Check/uncheck selected newsletters when user clicks No/Yes on registration form
$(document).ready(function() {
    $('#person-registration input#becomeMemberNo').click(function(){
            $('input[name="selectedNewsletters"]').attr('checked',false);
    });
    $('#person-registration input#becomeMemberYes').click(function(){
        $('input[name="selectedNewsletters"]').attr('checked',true);
});    
    $('#person-registration input[name="selectedNewsletters"]').click(function(){
        $('input#becomeMemberYes').attr('checked',true);
    });
});

// Check all newsletters by default
$(document).ready(function() {
	$('#person-registration input[name="selectedNewsletters"]').attr('checked',true);
});

function splashLink(o) {
	 var a  = $(o); 
	 var href = a.attr('href');
	 var div = $(href);
	 div.stop().css({backgroundColor: '#EDEDED'});
	 div.stop().delay(500).animate({backgroundColor: '#FFFFFF'}, 1000);
}
//"Watermark" help text for input fields
$(document).ready(function() {
	$('form#person-registration input#person-registration_birthDate').watermark('dd.mm.åååå', {className: 'explanation', useNative: false});
	$('form#person-registration input#person-registration_user_password').watermark('Minimum 6 tegn, minst ett tall eller spesialtegn', {className: 'explanation',  useNative: false});	
	$('form#searchform input#searchinput').watermark('Søk på tittel, forfatter, ISBN eller fritekst', {className: 'explanation', useNative: false});
});
// Show/hide conditions for company registration
$(document).ready(function() {
	$('form#chooselogintype input#radio-company').click(function() {
		$('#company-registration-restrictions').slideDown("fast");
	});
	$('form#chooselogintype input#radio-private').click(function() {
		$('#company-registration-restrictions').slideUp("fast");
	});
});



function fancyAlert(msg) {
    jQuery.fancybox({
    'modal' : true,
    'content' : "<div style=\"margin:1px;width:240px;\">"+msg+"<div style=\"text-align:right;margin-top:10px;\"><input style=\"margin:3px;padding:0px;\" type=\"button\" onclick=\"jQuery.fancybox.close();\" value=\"Ok\"></div></div>"
    });
    }
     
function fancyConfirm(msg,callback) {
    var ret;
    jQuery.fancybox({
	    modal : true,
	    content : "<div style=\"margin:1px;width:240px;\">"+msg+"<div style=\"text-align:right;margin-top:10px;\"><input id=\"fancyConfirm_cancel\" style=\"margin:3px;padding:0px;\" type=\"button\" value=\"Avbryt\"><input id=\"fancyConfirm_ok\" style=\"margin:3px;padding:0px;\" type=\"button\" value=\"Ok\"></div></div>",
	    onComplete : function() {
			jQuery("#fancyConfirm_cancel").click(function() {
				ret = false;
				jQuery.fancybox.close();
		    })
		    
		    jQuery("#fancyConfirm_ok").click(function() {
		    	ret = true;
		    	jQuery.fancybox.close();
		    })
	    },
	    onClosed : function() {
	    	callback.call(this,ret);
	    }
    });
}
    



