document.write('<style type="text/css">.jsshow { display: block; } .jshide { display: none; } .headline { visibility: hidden; } .headline .cufon { visibility: visible; }</style>');

// default easing
jQuery.easing.def = 'easeOutQuad';

// disable selection of text
$.extend($.fn.disableTextSelect = function() {
	return this.each(function(){
		if ($.browser.mozilla) {
			$(this).css('MozUserSelect', 'none');
		} else if ($.browser.msie) {
			$(this).bind('selectstart', function() { return false; });
		} else {
			$(this).mousedown(function() { return false; });
		}
	});
});

Cufon.replace('.headline');
Cufon.replace('.smallheadline');

var KORT123 = {
  utils: {
    ie: false,
    init: function(funcs) {
      for (var i = funcs.length; i--;) {
        if (typeof funcs[i] !== 'undefined') {
          funcs[i].init();
        }
      }
    }
  },
  init: function() {
    if ($.browser.msie) {
      this.utils.ie = parseInt($.browser.version, 10);
    }
    
    this.utils.init([
      this.cards,
      this.navigation,
			this.popular,
			this.faq
    ]);
    
    // animated scrolling on anchortags
    // $.localScroll({ 'easing': 'easeInOutQuad' });
    
    // ie8 button press
    if(this.utils.ie && this.utils.ie === 8) {
      $('.button').mousedown(function() {
        $(this).addClass('press');
      }).bind('mouseleave mouseup', function() {
        $(this).removeClass('press');
      });
    }
  },
  cards: {
    init: function() {
      $('div.mCard div.flash').each(function() {
        var $this = $(this),
            meta = $.metadata.get(this);
        
        if(typeof meta.url !== 'undefined') {
          var src = meta.url,
							img = meta.img || '',
          					imgWidth = meta.imgWidth || '',
          					imgHeight = meta.imgHeight || '';
          $this.flash({
            src: src,
            width: 468,
            height: 375,
						flashvars: { imageUrl: img, imageWidth: imgWidth, imageHeight: imgHeight }
          },{
            version: '8',
            update: false
          });
        }

				if(!$this.hasClass('flash-replaced')) {
					$this.addClass('no-flash');
					$this.find('img').attr('src', meta.img);
				}
      });
			
			var $from_email = $('#form_from_email'),
					$recipients = $('li.recipients'),
					$carduser = $('div.carduser'),
					$userotherday = $('li.userotherday');
					
			if($from_email.length > 0) { this.cardlogin($from_email); }
			if($recipients.length > 0) { this.recipients($recipients); }
			if($carduser.length > 0) { this.carduser(); }
			if($userotherday.length > 0) { this.userotherday($userotherday); }
			
    },
		cardlogin: function($from_email) {
			var toggle = function() {
				$.getJSON('/services/ajax/frontend/123kort_cardlogin.php', {'email': $from_email.val()}, function(data) {
					if(typeof data !== 'undefined') {
						$('.newsletter').hide();
						$('.cardlogin').hide();						
						if(data.status == 'user') {
							$('.cardlogin').show();
							// $('#form_from_password').focus().select();
						} else if(data.status == 'show_newsletter') {
							$('.newsletter').show();
						}
					}
				});
			};
			
			if($from_email.val()) { toggle(); }
			$from_email.change(function() { toggle(); });

		},
		recipients: function($recipients) {
			var $addresslist = $recipients.find('div.contacts li'),
					$recipientlist = $recipients.find('div.recipientlist ul');
			
			$recipients.find('select').change(function() {
				var val = $(this).val();
				if(val) {
					$addresslist.hide().filter('.'+ val).show();
				} else {
					$addresslist.show();
				}
			});
			
			$addresslist.live('click', function() {
				var $this = $(this),
						meta = $.metadata.get(this),
						val = meta.recipient,
						content = $this.html();
						
				if($recipientlist.find('input[value='+ val +']').length === 0) {
					var t = new Date().getTime();
					$recipientlist.prev('p').hide();
					
					var recipient = '<li style="background:url(/images/yellow-fade.gif?t='+ t +')"><input type="hidden" name="recipients[]" value="'+ val +';false" /><span>&nbsp;&nbsp;&nbsp;</span> '+ content +'</li>';
					$recipientlist.append($(recipient));
				}
						
			});
			
			$recipients.find('span').live('click', function() {
				if(confirm('Er du sikker på, at du vil fjerne modtageren?')) {
					$(this).parent().remove();
				}
			});
			
			$('a.addRecipient').click(function() {
				var $name = $('#form_to_name'),
						name = $name.val(),
						$email = $('#form_to_email'),
						email = $email.val(),
						$save = $('#save_recipient'),
						save = $save.attr('checked'), 
						error = '';
				
				if(!name.length > 0) {
					error = 'Navnet på modtageren skal indtastes';
				} else if(!email.length > 0) {
					error = 'Modtagerens e-mail skal indtastes';
				} else if(!email.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/)) {
					error = 'Modtagerens e-mail er ikke korrekt';
				} 
					
				if(error.length) {
					alert(error);
				} else {
					var t = new Date().getTime(),
							recipient = '<li style="background:url(/images/yellow-fade.gif?t='+ t +')"><input type="hidden" name="recipients[]" value="'+ name +';'+ email +';'+ save +'" /><span>&nbsp;&nbsp;&nbsp;</span> <strong>'+ name  +'</strong> ('+ email +')</li>';
					$('div.recipientlist ul').append($(recipient)).prev('p').remove();
					$save.attr('checked', '');
					$email.val('');
					$name.val('').focus();
				}				
				
				return false;
			});
		},
		carduser: function() {
			var $more_recipients = $('#form_more_recipients'),
					$otherday = $('#form_otherday'),
					$info = $('div.carduser'),
					toggle = function(clicked) {
						var more = $more_recipients.filter(':checked').length,
								otherday = $otherday.filter(':checked').length;
						
						clicked = ($(clicked).attr('id') === 'form_more_recipients') ? true : false;
						
						if(otherday && (!more || !clicked)) {
							$info.hide().filter('.otherdayinfo').show();
						} else if(more) {
							$info.show().filter('.otherdayinfo').hide();
						} else {
							$info.hide();
						}
					};
			
			$more_recipients.add($('li.otherday input')).click(function() { toggle(this); });
			toggle('');
		},
		 // carduser: function($carduser) {
		 // 
		 // 	var $recipients = $('#form_more_recipients'),
		 // 			recipients = $recipients.filter(':checked').length,
		 // 			recipients_pos = $recipients.parents('li:first').position().top - 80,
		 // 			$otherday = $('#form_otherday'),
		 // 			otherday = $otherday.filter(':checked').length,
		 // 			otherday_pos = $otherday.parents('li:first').position().top + 125 - $carduser.height(),
		 // 			toggle = function(top) {
		 // 				recipients = $recipients.filter(':checked').length;
		 // 				otherday = $otherday.filter(':checked').length;
		 // 				
		 // 				var posChange = $('li.cardlogin:visible').length > 0 ? 40 : 0;
		 // 
		 // 				if(recipients && otherday) {
		 // 					posChange += (top > 200 && $('li.newsletter:visible').length > 0) ? 108 : 0;
		 // 					$carduser.css('top', top + posChange).show();
		 // 				} else if(recipients) {
		 // 					$carduser.css('top', recipients_pos + posChange).show();
		 // 				} else if(otherday) {
		 // 					posChange += $('li.newsletter:visible').length > 0 ? 108 : 0;
		 // 					$carduser.css('top', otherday_pos + posChange).show();
		 // 				} else {
		 // 					$carduser.hide();
		 // 				}
		 // 				
		 // 			};
		 // 	
		 // 	if(recipients) {
		 // 		toggle(recipients_pos);
		 // 			} else if(otherday) {
		 // 		toggle(otherday_pos);
		 // 	}
		 // 	
		 // 	$recipients.unbind().click(function() { toggle(recipients_pos); });
		 // 	$('li.otherday input').unbind().click(function() { toggle(otherday_pos); });
		 // 			
		 // },
		userotherday: function($userotherday) {
		 	var toggle = function() {
		 		if($userotherday.find('input#form_otherday:checked').length > 0) {
		 			$userotherday.find('div.date').show();
		 		} else {
		 			$userotherday.find('div.date').hide();
		 		}
		 	};
		 	
		 	$userotherday.find('input').click(function() { toggle(); });
		 	toggle();
		 	
		 }
  },
  navigation: {
    init: function() {
      
      this.favorites();
      this.sideMenu();
      
      $('li.fillout a').click(function() { $('div.mCard input.first').focus().select(); });
      
    },
    favorites: function() {
			if(window.sidebar || window.external) {
				$('li.favorites a').click(function() {
					var url = 'http://www.123kort.dk',
							title = '123kort.dk - send gratis postkort';
					if (window.sidebar) { 
						window.sidebar.addPanel(title, url, '');
					} else if (window.external) {
						window.external.AddFavorite(url, title);
					}
	        return false;
	      });
			} else {
				$('.favorites').remove();
			}
      
      if(!KORT123.utils.ie) {
        $('li.homepage').hide();
      }
    },
    sideMenu: function() {
      var $menu = $('div.sideMenu'),
          height = $menu.height(),
          activeHeight = $menu.find('li.active ul').height(),
          over = function($li) {
            var $content = $li.find('ul'),
                contentHeight = $content.height();
						
						if($li.hasClass('active')) {
							$li.removeClass('active');
							$content.slideUp(300);
						} else {
	            $content.slideDown(300);
	            $li.addClass('active').siblings('li.active').removeClass('active').find('ul').slideUp(300);
						}

            if(contentHeight != activeHeight) {
              var newHeight = height + contentHeight - activeHeight;
              activeHeight = contentHeight;
              height = newHeight;
              $menu.animate({ height: newHeight + 24 }, 300);
            }
          };

      $menu.height(height + 10);
      $menu.find('a.first ~ ul').prev().mousedown(function() {
        over($(this).parents('li:first'));
      }).click(function() { $(this).blur(); return false; });
    }
  },
	popular: {
		init: function() {
			$('div.mPopularCards').each(function() {
				var t = new Date().getTime();
				$(this).find('div').flash({
          src: '/flash/mostpopular.swf',
          width: 940,
          height: 230,
					flashvars: { XMLurl: '/services/frontend/mostpopular.php?'+ t }
        },{
          version: '7',
          update: false
        });
			});
		}
	},
	faq: {
		init: function() {
			$('div.mFAQ h3 a').live('mousedown', function() {
				$li = $(this).parents('li:first');
				if($li.hasClass('active')) {
					$li.removeClass('active').find('div.content').slideUp(200);
				} else {
					$li.addClass('active').find('div.content').slideDown(300).end()
						.siblings('li.active').removeClass('active').find('div.content').slideUp(200);
				}
			}).live('click', function() { $(this).blur(); return false; });
		}
	}
};

$(function() {
  KORT123.init();
	$('.headline').addClass('cufoned');
	$('.smallheadline').addClass('cufoned');
});
