/*<![CDATA[*/
/// <reference path="jquery uncompress/jquery-1.2.3-intellisense.js" />

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
		jQuery DOM ready functions
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

jQuery(function() {

    //external link
    $('A[rel="external"]').click( function() {
        window.open( $(this).attr('href') );
        return false;
    });
 
    //focus on searchfield
    if($('#searchResult').is(':visible')){
        $('.searchField').focus(); 
    }
    
    $('.frontpage .searchField').focus(); 

    //odd even on StaticContentPage.aspx
    $('#staticPage table tr:odd').addClass('odd')
    
    //Category on frontpage
	$("<div class='catcRT'></div><div class='catcRB'></div>").insertAfter('.category:third a span.catCorner');
	$("<div class='catcLT'></div><div class='catcLB'></div>").insertAfter('.category:thirdFirst a span.catCorner');
	$("<div class='hr clear clearfix'><hr/></div>").insertAfter('.category:third');
	
	//rounded corners on boxes
	$("<div class='boxTop'><div class='boxcLT'></div><div class='boxcRT'></div></div>").prependTo('.box');
	$("<div class='boxBottom'><div class='boxcLB'></div><div class='boxcRB'></div></div>").appendTo('.box');
	$("<div class='boxTop'><div class='boxcLT'></div><div class='boxcRT'></div></div><div class='boxBottom'><div class='boxcLB'></div><div class='boxcRB'></div></div>").prependTo('.popupContent');
	
	//remove line on first element TODO test!!!!!!!!!!!!
	$('.box').each(function(el)
    {
        $(this).find("dt:first").css({background: 'none', padding: '0'});
    })
	
	$('.frontpage .box ul.list .hr:last').parent().remove();
	
	var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
	if (IE6 = true){
	function content_resize()  
    { 
        var w = $( document); 
        var H = w.height(); 
        var W = w.width();
        // otherwise we get an exception when H and W are 0
        var doOverlay = H > 20 && W > 22;
        if (doOverlay)
        { 
            $( '#overlay' ).css( {width: W-22, height: H-20} );
        }
    } 
    $(window).wresize(content_resize); 
    content_resize(); 
	}
	
	//Facebook link
	$('.facebook').click(fbs_click);
    
    //bind showpopup
	bindBehaviors();
	var commentURL = window.location.href;
	CommentId = commentURL.substring(commentURL.indexOf('#'), commentURL.length);
	if(commentURL.indexOf("#comment") > -1){
        textToggle($('a.showAllComments'),$('#showallExpandText').val(),$('#showallCollapseText').val()); 
        $('.userComment').not(':first').toggle();
        $(CommentId).css({background: '#ededed'});
        setTimeout("$(CommentId).focus()", 250);
    }

	//remove margin on service navigation
	$('.service a:last').css({margin: '0'});

	//print entry function
	$('#printEntry').one('click',printEntry);
  
	//show recommend popup
	$('.recommend').click(function(){
	    showPopup($(this).offset(),$('#recommend'),'true')
		return false;
	});
	
	//show TellAFriend popup
	$('#tipAFriend').click(function(){
	    showPopup($(this).offset(),$('#tellAFriend'),'false')
	    setTimeout("$('#uiToEmail').focus();",1000);
	    return false;
	});
	
	//show add comment popup
	$('#addComment').click(function(){
	    showPopup($(this).offset(),$('#addCommentPopup'),'false')
	    setTimeout("$('#name').focus();",1000);
	    return false;
	});
	
	//set focus on add comment submit field
	$("#captcha").keypress(function (e) {
      if (e.which == 3)
      $(this).next().focus();
      });
      $("#captcha").keyup(function(e) {    
            var cap = $(this).val();
            if(cap.length >= 3) 
            {   
              $('#addCommentSubmit').focus();
            }
          }); 
	
	//onError add comment
    $('.tryAgain').click(function(){
	    $('#addCommentForm').show();
        $('#addCommentContent .status').hide(); 
	    return false;
	});
   
    //hide popup
    $('.close').click(function(){
		hidePopup(this);
		return false;
    });
    
   //make localscroll on noticeIndexAlphabet			
	 if ( jQuery('#alphabetContent').is(":visible")){
	 var target = $('#alphabetContent').get(0);//the scrolled div
	    target.scrollLeft = target.scrollTop = 0;
			
		//scroll initially if there's a hash (#something) in the url 
		$.localScroll.hash({
			target: target, //could be a selector or a jQuery object too.
			axis:'xy',//the default is 'y'
			queue:true,
			duration:1500
		});
		
		var $last = $([]);//save the last link
		$.localScroll({
			target: target, //could be a selector or a jQuery object too.
			axis:'xy',//the default is 'y'
			queue:true,
			duration:1000,
			hash:true,
			onBefore:function( e, anchor, $target ){//'this' is the clicked link
				$last.removeClass('scrolling');
				$last = $(this).addClass('scrolling');
				this.blur();//remove the awful outline
			},
			onAfter:function( anchor ){
				$last.removeClass('scrolling');
			}
		});
	}
	
});

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
		Functions
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

// Facebook link
function fbs_click() {u=location.href;t=document.title;window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');return false;}

//function to toggle text 
function textToggle(element,text1,text2){
    if( jQuery.trim($(element).text()) == text1 )
        $(element).text(text2);
    else $(element).text(text1);
}

function hidePopup(elm)
{
    $('#overlay').animate({left: '0'}, 700).fadeOut('300');
    $('.popupContent:visible').fadeOut('300');
    $('.popupContent:visible .boxBottom').hide('400');
    $('.popupContent:visible .boxTop').hide('400');
    setTimeout("$('#addCommentForm').show()", 1000);
    setTimeout("$('.boxBottom').show()", 1500);
    setTimeout("$('.boxTop').show()", 1500);
    setTimeout("$('.popupContent').show()", 1500);
    
    setTimeout("$('.popup:visible').hide().removeAttr('style')", 1000);
    setTimeout("$('.progress').show()", 800);
    setTimeout("$('#tellAFriendContent .progress').hide()", 800);
    setTimeout("$('.status:visible').hide()", 800);
    setTimeout("$('.result').show()", 800);
    setTimeout("$('.popup:visible :input').clearForm()", 600);
 
    if($('#tipAFriend').is(':visible')){
        
        setTimeout("$('#uiSubject').val(tellSubject)", 700); 
    }
    $('.movie').show();
    return false;  
}

function showPopup(offset,elm,confirmationPopup)
{
    $('#overlay').animate({opacity: '0.8'}, 0).fadeIn('400');
    $('.movie').hide();
    if(confirmationPopup == 'true'){
	    var popTop = offset.top - '50';
        $(elm).css({bottom: 'auto', left: offset.left, top: popTop, position: 'absolute'}).fadeIn();
        setTimeout("$('.progress').hide('300')", 1500);
        setTimeout("$('.status').show('300')", 1500);
    }
    else{
        elmH = 400;
        posY = offset.top;
        posX = offset.left;
        docH = $(document).height();
        if (offset.top + elmH > docH){
            $(elm).css({top: 'auto'});
        }
        else{
            $(elm).css({top: posY})
        }
        $(elm).animate({left: posX}, 800).fadeIn('300');
    }
}

//add comment functions
	var bindBehaviors = function(scope){
    //Show-hide user comment
    $('.userComment').show();
    $('.userComment').not(':first').hide();
    
    if ($('a.showAllComments').text() == $('#showallCollapseText').val()){
         textToggle($('a.showAllComments'),$('#showallExpandText').val(),$('#showallCollapseText').val());    
    }
    if ($('.userComment a.more').text() == $('#commentCollapseText').val()){
         textToggle($('.userComment a.more'),$('#commentExpandText').val(),$('#commentCollapseText').val());
    }
    
     // Toggle Single user comment
    $('.userComment a.more').click(function(){
		$(this).prev('.more').toggle();
		$(this).prev('.more').prev('.dots').toggle();  
        textToggle($(this),$('#commentExpandText').val(),$('#commentCollapseText').val()); 
        return false;
	});
	
	
    if ($('.userComment').length>1 )
    {
      $('a.showAllComments').show();  
      //$('.numberOfCommentsWrapper').show();
      $('a.showAllComments').click(function(){
       $('.userComment').not(':first').toggle();
       //$('.numberOfCommentsWrapper').toggle();
        textToggle($(this),$('#showallExpandText').val(),$('#showallCollapseText').val());
        return false;
      });
    }
	
}

var printEntry = function ()
{
    if($('.printArea').length<2){
    $("<div class='printContainer' style='position: absolute; width: 600px; top: 0; z-index: 10001; display: none; text-align: left;'></div>").appendTo('body');
    $('.printArea').clone().appendTo('.printContainer');
    $('.printContainer .service').hide();
    $('.printContainer .userComment').show();
    $('.printContainer div.more').show();
    $('.printContainer a.more').hide();
    $('.printContainer a.commit').hide();
    $('.printContainer .hr').hide(); 
    if ($('.userComment').length<1){
        $('.commentHeading').hide();
    }
    $('#overlay').animate({opacity: '1.0'}).fadeIn('200');
    $('.printContainer').animate({left: '10'}, 400).fadeIn('100');
    $('#page').hide();
    setTimeout("window.print()", 1000);
    $("<div class='service'><a href='#' class='ie6Link'>Tilbage til opslaget</a></div>").appendTo('.printContainer .ie6LinkContainer');
    $('.ie6Link').click(function(){
	    $('.printContainer').remove();
	    $('#page').show();
	    $('#overlay').animate({opacity: '0.8'}, 200).fadeOut('400'); 
	    $('.ie6Link').remove();
	    $('#printEntry').bind('click',printEntry);
    }); 
}	
}

/** Validation methods **/
function ValReqEmail(ctrlId,msg,valsum)
{
  return Val(ctrlId,/^[A-Za-z0-9](([\_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$/,msg,valsum)
}
   
function ValReqDigits(ctrlId,msg,valsum)
{
  return Val(ctrlId,/^\d+$/,msg,valsum)
}
    
function ValReq(ctrlId,msg,valsum)
{
  return Val(ctrlId,/^.+$/,msg,valsum)
}

function Val(ctrlId,regex,msg,valsum)
{
  var val = jQuery.trim($(ctrlId).val());
  if (!val.match(regex))
  {
    // TODO: insert into valsum
    alert(msg);
    $(ctrlId).focus();
    return false;
  }
  return true;
}

/*]]>*/
