jQuery(document).ready(function() {
	
	var url_match = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
	var url_match_www = /www./;
	var url_match_short = /www.test.de/;
	
	
	jQuery('a.lbOn').facebox();
	jQuery('.date').datepicker(jQuery.datepicker.regional['de']);

	
	jQuery('div.template-preview div.template-preview-text div.text').html(jQuery('#SmadsJobText').val());
	
	if (jQuery('#SmadsJobText').val() == '') {
		jQuery('div.template-preview-text-notext').css('display','block');
		jQuery('div.template-preview-text').css('display','none');
		jQuery('p#imgLinkInActive').css('display', 'block');
		jQuery('p#imgLinkActive').css('display', 'none');
	} else {
		jQuery('p#imgLinkInActive').css('display', 'none');
		jQuery('p#imgLinkActive').css('display', 'block');
	}
	
	jQuery('#imgLinkInActive a').click(function() { return false; });
	
	
	jQuery('#SmadsJobText').keyup(function() { 
		jQuery('div.template-preview div.template-preview-text div.text').html(jQuery(this).val()); 
		
		if (jQuery('#SmadsJobText').val() == '') {
			jQuery('div.template-preview-text-notext').css('display','block');
			jQuery('div.template-preview-text').css('display', 'none');
			jQuery('p#imgLinkInActive').css('display', 'block');
			jQuery('p#imgLinkActive').css('display', 'none');
		} else {
			jQuery('div.template-preview-text-notext').css('display','none');
			jQuery('div.template-preview-text').css('display', 'block');
			jQuery('p#imgLinkInActive').css('display', 'none');
			jQuery('p#imgLinkActive').css('display', 'block');
		}
		
		textSplit = jQuery(this).val().split(' ');
		matchesLinks = 0;
		matchesLinksWWW = 0;
		matchesLinksShort = 0;
		
		for(i=0;i<textSplit.length;i++) {

			var matches = textSplit[i].match(url_match);
			if (typeof matches == 'object' && matches != null) {
				matchesLinks++;
			}
			
			var matches = textSplit[i].match(url_match_www);
			if (typeof matches == 'object' && matches != null) {
				matchesLinksWWW++;
			}
			
			var matchesShort = textSplit[i].match(url_match_short);
			if (typeof matchesShort == 'object' && matchesShort != null) {
				matchesLinksShort++;
			}	
			
		}
		
		if (matchesLinks > matchesLinksShort || matchesLinksWWW > matchesLinksShort) {
			jQuery('#smsUrlShorter').css('display','block');
		} else {
			jQuery('#smsUrlShorter').css('display','none');
		}	
		
	} );
	
	jQuery('#smsUrlShorter a').click(function() {
		alert("Aktion für den URL-Shorter muss noch gebaut werden");
		return false;
	});
	
	
	
	jQuery('#ownLogo').change(function() { jQuery('#logo-upload').toggle(); });
	jQuery('#ownLogoHas a.change').click(function() { jQuery('#logo-upload').toggle(); return false; });
	jQuery('#ownLogoHas a.delete').click(function() { alert("Has to come"); return false; });
	
	jQuery('div.template-preview').addClass(jQuery(this).attr('rel'));
	
	jQuery('fieldset.template-selection input').change(function() {

		template = jQuery(this).attr('rel').replace(/tpl/,'');
		
		if (jQuery('fieldset.template-selection input.selected').attr('rel') != null) {
			jQuery('div.template-preview').removeClass(jQuery('fieldset.template-selection input.selected').attr('rel'));
		}
		
		jQuery('fieldset.template-selection input').removeClass('selected');
		jQuery(this).addClass('selected');
		jQuery('div.template-preview').addClass(jQuery(this).attr('rel'));
		
		
	});
});
