/* Thanks for stopping by, please don't copy :) */

function Facebook_HandleResponse(response) {
	if ( response.session ) {
		location.href = document.location.pathname + '?'+ $.param(response.session);
	}
}

function nl2br(str) {
    return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1<br>$2');	
}

function GetCreditCardType(cc) {
    if ((/^(34|37)/).test(cc) && cc.length <= 15) {
        return 'amex'; //AMEX begins with 34 or 37, and length is 15.
    } else if ((/^(51|52|53|54|55)/).test(cc) && cc.length <= 16) {
        return 'masterCard'; //MasterCard beigins with 51-55, and length is 16.
    } else if ((/^(4)/).test(cc) && (cc.length <= 13 || cc.length <= 16)) {
        return 'visa'; //VISA begins with 4, and length is 13 or 16.
    } else if ((/^(300|301|302|303|304|305|36|38)/).test(cc) && cc.length <= 14) {
        return 'dinersclub'; //Diners Club begins with 300-305 or 36 or 38, and length is 14.
    } else if ((/^(2014|2149)/).test(cc) && cc.length <= 15) {
        return 'enroute'; //enRoute begins with 2014 or 2149, and length is 15.
    } else if ((/^(6011)/).test(cc) && cc.length <= 16) {
        return 'discover'; //Discover begins with 6011, and length is 16.
    } else if ((/^(3)/).test(cc) && cc.length <= 16) {
        return 'jcb';  //JCB begins with 3, and length is 16.
    } else if ((/^(2131|1800)/).test(cc) && cc.length <= 15) {
        return 'jcb';  //JCB begins with 2131 or 1800, and length is 15.
    }
    return false;
}

var UnoEuro = {
	PromptRedirect: function(question, url) {
		$('<div/>').dialog({
			title: 'Vent.. er du sikker?',
			width: 512,
			buttons: {
				'Nej': function() {
					$(this).dialog('close');
				},
				'Ja, jeg er sikker': function() {
					location.href = url;
					$(this).dialog('close');
				}
			}
		}).html(question).dialog('open');
		return false;
	},
	Confirm: function(el, question) {

		var url = $(el).attr('href');
		var form = $(el).find('form');
		$('<div/>').dialog({
			title: 'Vent.. er du sikker?',
			width: 512,
			buttons: {
				'Nej': function() {
					$(this).dialog('close');
				},
				'Ja, jeg er sikker': function() {
					if ( url ) {
						location.href = url;
					} else if ( form ) {
						form.submit();
					}
					$(this).dialog('close');
				}
			}
		}).html(question).dialog('open');
		return false;
	},
	
	
	Dialog: function(title, content) {
		$('<div/>').dialog({
			title: title,
			buttons: {
				'OK': function() {
					$(this).dialog('close');
					return true;
				}
			}
		}).html(content).dialog('open');
		return false;
	}
}

$.extend($.ui.dialog.prototype.options, {
/*	close: function() { $(this).dialog('destroy') }, */
    modal: true,
    resizable: false,
    draggable: false,
	autoOpen: false,
	closeText: 'Luk',
	width: 512
});

$.extend({'QueryString':window.location.search.length<=1?new Array():function(a){var b=new Array();for(var i=0;i<a.length;++i){var p=a[i].split('=');b[p[0]]=unescape(p[1]);}return b;}(window.location.search.substr(1).split('&'))});

(function($){
	$.fn.translate = function() {
		var languageTo = "en";
		return this.each(function() {
			window.mycallback = function(response) { alert(response); }
			var s = document.createElement("script");
			s.src = "http://api.microsofttranslator.com/V2/Ajax.svc/Translate?oncomplete=mycallback&appId=7C0CD59E17AA7B695BC8139A31765D7AA80E020A&to=" + languageTo + "&text=" + $(this).html();
			document.getElementsByTagName("head")[0].appendChild(s);
		});
	}
	
	$.fn.toggleCheckbox = function() {
		return $(this).attr('checked', !$(this).is(':checked'));
	}
	$.fn.noclick = function() {
		return this.each(function() {
			$(this).click(function(e) { e.preventDefault(); });	
		});
		
	}
	$.fn.putCursorAtEnd = function() {
		function moveCaretToEnd(el) {
			if (typeof el.selectionStart == "number") {
				el.selectionStart = el.selectionEnd = el.value.length;
			} else if (typeof el.createTextRange != "undefined") {
				el.focus();
				var range = el.createTextRange();
				range.collapse(false);
				range.select();
			}
		}
		
		return this.each(function() {
			moveCaretToEnd(this);
		
			// Work around Chrome's little problem
			window.setTimeout(function() {
				moveCaretToEnd(this);
			}, 1);
		});
	};
	$.fn.InsertAtCarret = function(myValue){
		return this.each(function(i) {
			if (document.selection) {
				//For browsers like Internet Explorer
				this.focus();
				sel = document.selection.createRange();
				sel.text = myValue;
				this.focus();
			} else if (this.selectionStart || this.selectionStart == '0') {
				//For browsers like Firefox and Webkit based
				var startPos = this.selectionStart;
				var endPos = this.selectionEnd;
				var scrollTop = this.scrollTop;
				this.value = this.value.substring(0, startPos)+myValue+this.value.substring(endPos,this.value.length);
				this.focus();
				this.selectionStart = startPos + myValue.length;
				this.selectionEnd = startPos + myValue.length;
				this.scrollTop = scrollTop;
			} else {
				this.value += myValue;
				this.focus();
			}
		});
	}
	$.fn.CapsLock = function(callback){
		return $(this).keypress(function(e){
			if ( navigator.userAgent.match(/iPhone|iPad/i) ) {
				return;
			}
			var w = e.which ? e.which : (e.keyCode ? e.keyCode : -1);
			var s = e.shiftKey ? e.shiftKey : (e.modifiers ? !!(e.modifiers & 4) : false);
			var c = ((w >= 65 && w <= 90) && !s) || ((w >= 97 && w <= 122) && s);
			callback.call(this, c);
		});
	};
	
	$.fn.InsideHint = function(hint) {
		if ( $(this).val() == '' ) {
			$(this).val(hint);
		}
		return $(this).bind('focus', function() {
			if ( $(this).val() == hint) { 
				$(this).val('').addClass('active').removeClass('inactive');
			}
			}).bind('blur', function() {
			  if ( $(this).val() == '' ) { 
				$(this).val(hint).addClass('inactive').removeClass('active');
			}
		}).addClass('inactive');
	};
	
	$.fn.CheckComplexity = function() {
		return $(this).bind('change keyup', function() {
			var score = 0;
			var password = $(this).val();
			if ( password.length >= 6 ) {
				score += 50;	
			}
			
			if ( password.length >= 15 ) {
				score += 50;	
			}
			
			/* Contains Uppercase */
			if ( password.match(/[A-Z]+/) ) {
				score += 10;
			}
			
			/* Contains lowercase */
			if ( password.match(/[a-z]+/) ) {
				score += 10;
			}
			
			/* Contains a number */
			if ( password.match(/[0-9]+/) ) {
				score += 10;
			}
			
			/* Contains only lowercase chars */
			if ( password.match(/^[a-z]+$/) ) {
				score -= 20;
			}
	
			/* Contains only uppercase chars */
			if ( password.match(/^[A-Z]+$/) ) {
				score -= 15;
			}
	
		
			/* Contains only numbers, BAD BAD BAD */
			if ( password.match(/^[0-9]+$/ ) ) {
				score -= 50;
			}
			
			if ( score < 50 ) {
				$(this).css('color', '#FF5353');
			} else if (score >= 50 && score < 75) {
				$(this).css('color', '#FAD054');
			} else if ( score >= 75 ) {
				$(this).css('color', 'green');
			}
		}).change();
	};

    $.SlideShow = function(el){
        var base = this;
        
        base.$el = $(el);
        base.el = el;
        
        base.$el.data("SlideShow", base);
        
        base.init = function(){
			base.currentPosition = 0;
			base.numberOfSlides = $('.slide').length;
			base.slideTimer;
			base.isFrozen = false;
			$('.slide:first').addClass('active').css('opacity', 1);
			base.$el.hover(function() {
				base.StopTimer();
			}, function() {
				base.StartTimer();
			});
			for(var i=1; i <= base.numberOfSlides; i++){  
				$('#slideControls').append('<span class="control" rel="'+ i +'">&#8226;</span>');
			}
		
			base.setControls(base.currentPosition);
			
			$('.control').bind('click', function(){
					base.MoveToSlide($(this).attr('rel')-1);
					base.Freeze();
			});
			
			base.StartTimer();
        };

		base.GetNext = function() {
			if ( base.currentPosition+1 > base.numberOfSlides-1 ) {
				return 0;	
			}
			return base.currentPosition+1;
		};
		base.MoveNext = function() {
			base.MoveToSlide(base.GetNext());
			return this;
		};
		base.MoveToSlide = function(number) {
			if ( isWindowInFocus ) {
				base.Unfreeze();
				base.currentPosition = number;
				base.setControls(number);
		
				$('.slide.active').animate({'opacity' : 0}, 1000, '', function() {
					$(this).removeClass('active')	
				});
				$('.slide').eq(number).addClass('active').animate({'opacity' : 1}, 1000);
			}

			return this;
		};
		base.setControls = function(){
			$('#slideControls .control').each(function() {
				if ( $(this).attr('rel')-1 == base.currentPosition ) {
					$(this).addClass('current');	
				} else {
					$(this).removeClass('current');	
				}
			});
			return this;
		};
		
		base.StopTimer = function() {
			if ( base.slideTimer ) {
				clearInterval(base.slideTimer);
				base.slideTimer = null;
			}
			return this;
		};
			
		base.StartTimer = function() {
			if ( !base.isFrozen && !base.slideTimer ) {
				base.slideTimer = setInterval(function() {
					base.MoveNext()
				}, 5*1000);
			}
			return this;
		};
		
		base.Freeze = function() {
			if ( !base.isFrozen ) {
				base.StopTimer();
				base.isFrozen = true;	
			}
		}
		
		base.Unfreeze = function() {
			if ( base.isFrozen ) {
				base.isFrozen = false;	
				base.StartTimer();
			}
		}
        
        base.init();
    };
    
    $.fn.slideShow = function(){
	  	var el = $(this);
		if ( el.data('SlideShow') ) 
			return el.data('SlideShow');

        return this.each(function(){
            (new $.SlideShow(this));
        });
    };    
})(jQuery);

var isWindowInFocus = true;
$(function() {
	$('form').submit(function(){
		$("input:submit", this).attr('disabled', 'disabled').addClass('disabled');
	});
	
	$(window).bind('focus', function(){ isWindowInFocus = true }).bind('blur', function() { isWindowInFocus = false });

	$("tr.alternate:odd").css("background-color", "#F5F7F0");
	$("tr.alternate:even").css("background-color", "#FFFFFF");
	$('.required').after('<span style="padding-left: 3px; color: #990000; font-family: Verdana; font-size: 10pt; vertical-align: middle">*</span>');
	$('.ui-button-link').button();
	$('.prettyButton').addClass('ui-corner-all');
	if ( typeof $.fn.tipsy != "undefined" ) {
		$('A.tooltip').html('<img src="/img/layout/help_grey.gif" width="11" height="11" alt="" />').attr('href', '#').bind('click', function() {return false }).tipsy({html: true, gravity: 'w'});
		$('span.tooltip').tipsy({html: true, gravity: 'se'});
	}
	
	if ( typeof $.fn.superfish != "undefined" ) {
		$('.superfish').superfish({
			delay: 500,
			animation: {
				opacity: 'show',
				height: 'show'
			},
			pathClass: 'current', 
			speed: 'fast',
			autoArrows: false,
			dropShadows: false,
			onShow: function() {
				$('#slideshow').animate({
					opacity: 0.2
				}, 'fast');	
			},
			onHide: function() {
				if ( this.length ) {
					$('#slideshow').animate({
							opacity: 1
					}, 'fast');
				}
			}	
	
		});
	}
   });

