// JavaScript Document

jQuery.extend({
    getURLParam: function(strParamName){
        var strReturn = "";
        var strHref = window.location.href;
        var bFound=false;
        
        var cmpstring = strParamName + "=";
        var cmplen = cmpstring.length;
        
        if ( strHref.indexOf("?") > -1 ) {
            var strQueryString = strHref.substr(strHref.indexOf("?")+1);
            var aQueryString = strQueryString.split("&");
            
            for ( var iParam = 0; iParam < aQueryString.length; iParam++ ) {
                if (aQueryString[iParam].substr(0,cmplen)==cmpstring){
                    var aParam = aQueryString[iParam].split("=");
                    strReturn = aParam[1];
                    bFound=true;
                    break;
                }
            }
        }
        
        if (bFound==false) return null;
        return strReturn;
    }
});
jQuery(function($){
    $.datepicker.regional['da'] = {
		closeText: 'Luk',
        prevText: '&#x3c;Forrige',
		nextText: 'N&aelig;ste&#x3e;',
		currentText: 'Idag',
        monthNames: ['Januar','Februar','Marts','April','Maj','Juni','Juli','August','September','Oktober','November','December'],
        monthNamesShort: ['Jan','Feb','Mar','Apr','Maj','Jun','Jul','Aug','Sep','Okt','Nov','Dec'],
		dayNames: ['S&oslash;ndag','Mandag','Tirsdag','Onsdag','Torsdag','Fredag','L&oslash;rdag'],
		dayNamesShort: ['S&oslash;n','Man','Tir','Ons','Tor','Fre','L&oslash;r'],
		dayNamesMin: ['S&oslash;','Ma','Ti','On','To','Fr','L&oslash;'],
		weekHeader: 'Uge',
        dateFormat: 'dd-mm-yy',
		firstDay: 1,
		isRTL: false,
		showMonthAfterYear: false,
		yearSuffix: ''};
    $.datepicker.setDefaults($.datepicker.regional['da']);
});

$(document).ready(function() {
	$("textarea.tinymce").tinymce({
		// Location of TinyMCE script
		script_url : '/fileadmin/jscripts/tiny_mce/tiny_mce.js',

		// General options
		theme : "simple",
		plugins : "autolink,lists,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,advlist",
		width : "500",
		height: "400",

		// Example content CSS (should be your site CSS)
		content_css : "/fileadmin/tpl/css/stylesheet.css"
		
	});
	
/*	if($.getURLParam("feedback")){
		$.scrollTo(   //http://plugins.jquery.com/project/ScrollTo
			'div#feedbackform',
			3000, 
			{
				over:{
					top:-0.2, 
					left:0
				},
				onAfter:function(){
//					$('#feedbackform').fadeOut();
//					$('#feedbackform').fadeIn();
				}
			}
		);
	}
*/	
	// datepicker used for company feedback http://jqueryui.com/demos/datepicker/
	$( ".datepicker" ).datepicker($.datepicker.regional['da']);
	$(".newstatus").change(function(){
		var status = $(this).val();
		if(status == 2){
			$(this).nextAll('.delivery_date:first').show();
		} else {
			$(this).nextAll('.delivery_date:first').hide();
		}
	});
	

	//Ajax call for email subscription
	$("#newsletter_subscribe").click(function(){
		var valid = '';
		var email = $('#newsletter_email').val();
		
		if (!email.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
			valid += 'Emailen er ikke godkendt';
		}
		$("#newsletter_text").addClass('newsletter_feedback');
		if(valid!=""){
			$("#newsletter_text").html(valid);	
		} else {
			var datastr ='eID=tg_newsletter_register&email=' + email;
			$("#newsletter_text").html("Tilmelder dig... ");
			setTimeout("subscribe('"+datastr+"')",10);
		}
	});
	
	$('#lightbox a').lightBox({
		imageLoading: '/fileadmin/tpl/images/lightbox-ico-loading.gif',
		imageBtnClose: '/fileadmin/tpl/images/lightbox-btn-close.gif',
		imageBtnPrev: '/fileadmin/tpl/images/lightbox-btn-prev.gif',
		imageBtnNext: '/fileadmin/tpl/images/lightbox-btn-next.gif',
		txtImage: 'Billede',
		txtOf: 'af'
	});
	
	$("#provider_create").click(function(){
		validate_provider();
	});
	
	$(".addtolist").click(function(){
		
		var actid = $(this).attr('name');
		var acturl = $('#activityurl'+actid).val();
				
		datastr ='eID=register_activity&actid=' + actid + '&acturl=' + acturl;
		setTimeout("register_activity('"+datastr+"')",10);
		
//		$(".addtolist").replaceWith('<img src="fileadmin/tpl/images/addedtolist.jpg" id="addedtolist" />');
//		$("#form-comments").delay(8000).fadeOut();
				
	});
	
	$(".deleteactivity").live("click", function(){
		var delid = $(this).attr('id');

		$(this).parents(".addedactivity").fadeOut(
			500,
			function(){
				datastr ='eID=register_activity&delid=' + delid;
				setTimeout("register_activity('"+datastr+"')",10);
			}
		);
	});
	
	$("#contactme1").click(function(){
		$("#contactme-form-wrap").fadeIn();
		$("#contactme1").hide();
	});

	$("#directform").validate({
		rules: {
			"tx_teamfe_pi1[lead_name]": "required",
			"tx_teamfe_pi1[lead_phone]": {
				required: true,
				minlength: 8
			},
			"tx_teamfe_pi1[lead_email]": {
				required: true,
				email: true
			}
	    },
	    messages: {
			"tx_teamfe_pi1[lead_name]": "Angiv venligst dit navn.",
		 	"tx_teamfe_pi1[lead_phone]": {
		   		required: "Angiv venligst telefonnummer.",
		   		minlength: "Telefonnummer er ikke angivet korrekt."
		 	},
		 	"tx_teamfe_pi1[lead_email]": {
		   		required: "Angiv venligst din e-mail adresse.",
		   		email: "E-mail er ikke angivet korrekt."
		 	}
	   }
	});
	
	$("#leaddetails").validate({
	
		rules: {
			c_name: "required",
			c_phone: {
				required: true,
      			minlength: 8
			},
			c_email: {
				required: true,
				email: true
			}
	    },
	    messages: {
			c_name: "Angiv venligst dit navn.",
		 	c_phone: {
		   		required: "Angiv venligst telefonnummer.",
		   		minlength: "Telefonnummer er ikke angivet korrekt."
		 	},
		 	c_email: {
		   		required: "Angiv venligst din e-mail adresse.",
		   		email: "E-mail er ikke angivet korrekt."
		 	}
	   },
	   submitHandler: function() {   
			var email = escape($("#c_email").val());
			var phone = escape($("#c_phone").val());
			var company = escape($("#c_company").val());
			var name = escape($("#c_name").val());
			var participants = escape($("#c_participants").val());
			var time = escape($("#c_activity_time").val());
			
			$(".contact-main").fadeOut(function(){
				$(".contact-busy").show();
			});
				
			datastr ='eID=register_lead&name=' + name + '&company=' + company + '&phone=' + phone + '&email=' + email + '&participants=' + participants + '&time=' + time;

			setTimeout("register_lead('"+datastr+"')",1000);
	   }
	});

/*	$("#contactme2").click(function(){
		
		var email = $("#c_email").val();
		var phone = $("#c_phone").val();
		var company = $("#c_company").val();
		var name = $("#c_name").val();
		
		$(".contact-main").fadeOut(function(){
			$(".contact-busy").show();
		});
			
		datastr ='eID=register_lead&name=' + name + '&company=' + company + '&phone=' + phone + '&email=' + email;
		
		setTimeout("register_lead('"+datastr+"')",1000);
	});
*/	
	$("#contactme-form-close").click(function(){
		$("#contactme-form-wrap").fadeOut(0,function(){
			$("#contactme1").show();
		});
	});

	datastr ='eID=register_activity';
	setTimeout("register_activity('"+datastr+"')",10);

	slideShow(4000);
	
	$("#enquiryForm").validate({
		rules: {
			navn: "required",
			tlf: {
				required: true,
      			minlength: 8
			},
			email: {
				email: true
			},
			postnr: {
      			min: 1
			}
		},
	   messages: {
		 navn: "Angiv venligst dit navn.",
		 tlf: {
		   required: "Angiv venligst telefonnummer.",
		   minlength: "Telefonnummer er ikke angivet korrekt."
		 },
		 email: {
		   required: "Angiv venligst din e-mail adresse.",
		   email: "E-mail er ikke angivet korrekt."
		 },
		 postnr: {
			 min: "Angiv venligst en by."
		 }
	   }
	});

});
function validate_provider(){
		$('#provider_text').hide();
		var name = $('#provider_contactname').val();
		var email = $('#provider_contactemail').val();
		var phone = $('#provider_contactphone').val();

		var company = $('#provider_company').val();
		var cvr = $('#provider_cvr').val();
		var address = $('#provider_address').val();
		var zip = $('#provider_zip').val();
		var city = $('#provider_city').val();
		var leademail = $('#provider_leademail').val();
		var leadsms = $('#provider_leadsms').val();
		
		var username = $('#provider_username').val();
		var password = $('#provider_password').val();
		var passwords = $('#provider_passwords').val();

		providerdatastr = 'eID=provider_create&contactname=' + name + '&contactemail=' + email + '&contactphone=' + phone + '&company=' + company + '&cvr=' + cvr + '&address=' + address + '&zip=' + zip + '&city=' + city + '&leademail=' + leademail + '&leadsms=' + leadsms + '&username=' + username + '&password=' + password + '&passwords=' + passwords;

		setTimeout("create_provider('"+providerdatastr+"')",10);
}
function create_provider(datastr){
	$.ajax({
		type: "POST",
		url: "/index.php",
		data: datastr,
		cache: false,
		success: function(html){
			$("#provider_text").show();
			$("#provider_text").html(html);
		}
	});
}
function subscribe(datastr){
	$.ajax({
		type: "POST",
		url: "/index.php",
		data: datastr,
		cache: false,
		success: function(html){
			if(html == 'OK'){
				$("#newsletter_text").html('Du er nu tilmeldt nyhedsbrevet!');
				$("#newsletter_email").val('');
			}
			else {
				$("#newsletter_text").html(html);
			}
		}
	});
}
function register_lead(datastr){
	$.ajax({
		type: "POST",
		url: "/index.php",
		data: datastr,
		cache: false,
		success: function(html){
			if(html == 'OK'){
				$(".contact-busy p").fadeOut(function(){
					$(this).removeClass();
					$(this).html('Registering gennemført. Vi kontakter dig hurtigst muligt');
					$(this).fadeIn();
				});
				$("#contact-form-bottom").delay(4000).fadeOut(1500,function(){
					$(".contact-busy").hide();
					$(".contact-main").show();
				});
			}else if(html > ''){
				alert(html);
				$(".contact-busy").hide();
				$(".contact-main").show();				
			} else {
				$(".contact-busy").hide();
				$(".contact-main").show();
				alert('Der opstod en fejl, prøv venligst igen.');
			}
		}
	});
}

function register_activity(datastr){
	$.ajax({
		type: "POST",
		url: "/index.php",
		data: datastr,
		cache: false,
		success: function(html){
			$("#contactme-list").html(html);
			if(html > ''){
				$("#contact-form-bottom").fadeIn(function(){
					$("#newactivity").fadeOut();
					$("#newactivity").fadeIn();
					$("#newactivity").fadeOut();
					$("#newactivity").fadeIn();
				});
			} else {
				$("#contact-form-bottom").fadeOut();
			}
		}
	});
}

//http://www.queness.com/resources/html/slideshow2/index.html

function slideShow(speed) {
 
 
	//append a LI item to the UL list for displaying caption
	$('ul.slideshow').append('<li id="slideshow-caption" class="caption"><div class="slideshow-caption-container"><h3></h3><p></p></div></li>');
 
	//Set the opacity of all images to 0
	$('ul.slideshow li').css({opacity: 0.0});
	
	//Get the first image and display it (set it to full opacity)
	$('ul.slideshow li:first').css({opacity: 1.0});
	
	//Get the caption of the first image from REL attribute and display it
	$('#slideshow-caption h3').html($('ul.slideshow a:first').find('img').attr('title'));
	$('#slideshow-caption p').html($('ul.slideshow a:first').find('img').attr('alt'));
		
	//Display the caption
	$('#slideshow-caption').css({opacity: 0.7, bottom:0});
	
	//Call the gallery function to run the slideshow	
	var timer = setInterval('gallery()',speed);
	
	//pause the slideshow on mouse over
	$('ul.slideshow').hover(
		function () {
			clearInterval(timer);	
		}, 	
		function () {
			timer = setInterval('gallery()',speed);			
		}
	);
	
}
 
function gallery() {
 
 
	//if no IMGs have the show class, grab the first image
	var current = ($('ul.slideshow li.show')?  $('ul.slideshow li.show') : $('#ul.slideshow li:first'));
 
	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().attr('id') == 'slideshow-caption')? $('ul.slideshow li:first') :current.next()) : $('ul.slideshow li:first'));
		
	//Get next image caption
	var title = next.find('img').attr('title');	
	var desc = next.find('img').attr('alt');	
 
	//Set the fade in effect for the next image, show class has higher z-index
	next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 1000);
	
	//Hide the caption first, and then set and display the caption
	$('#slideshow-caption').slideToggle(300, function () { 
		$('#slideshow-caption h3').html(title); 
		$('#slideshow-caption p').html(desc); 
		$('#slideshow-caption').slideToggle(500); 
	});		
 
	//Hide the current image
	current.animate({opacity: 0.0}, 1000).removeClass('show');
 
}
