jQuery.fn.rdy = function(func){
	this.length && func.apply(this);
	return this;
};
var clearValue = function() {};
jQuery(document).ready(function($){
	
	$('html').removeClass('no-js');
	
	/*== placeholder for browsers that sucks ==*/
	var fakeInput = document.createElement("input"),
			placeHolderSupport = ("placeholder" in fakeInput);
  clearValue = function (e) {
    if ($(e).val() === $(e).data('placeholder')) {
      $(e).val('');
    }
  };
	if (!placeHolderSupport) {
		$('input[placeholder]').each(function(){
			var searchField = $(this),
					originalText = searchField.attr('placeholder'),
					val = jQuery(this).val();
			searchField.data('placeholder', originalText);
			if(val == '') { jQuery(this).val(originalText); } else {
				searchField.addClass("placeholder")
			}
			
			searchField.bind("focus", function () { 
        if (jQuery(this).val() == jQuery(this).data('placeholder')) {
          this.value = '';
        }
      }).bind("blur", function () {
				if (jQuery(this).val() == '') {
					$(this).val(originalText).addClass("placeholder");
				}
			});
		});

		// Empties the placeholder text at form submit if it hasn't changed
		
    $("form").bind("submit.defaultclearplaceholder", function () {
			clearValue($('input[placeholder]', this));
		});

		// Clear at window reload to avoid it stored in autocomplete
		$(window).bind("unload", function () {
			clearValue($('input[placeholder]', this));
		});
	}
	/*== end placeholder ==*/
	
	
	$('.doubleList li').slice(0, Math.ceil($('.doubleList li').length/2)).addClass('leftside');
	
	$('#breadcrumb').each(function(){
		$('li',this).each(function(){
			$('a',this).prepend($(this).index()+1+'. '); //Add index for each item in breadcrumb
		});
		
		$('li.active',this).prevAll('li').addClass('done'); // Add class "done" for previous elements	
	});
	
	
	$('.fakeUpload').each(function(){
		var real = $('input[type="file"]',this),
				fake = $('input[type="text"]',this);
				
		real.change(function(){
			fake.val(real.val());
		})		
				
	});
	
		
	//Popup
	
	$('a[rel*="popup"]').live('click', function() {
		var t = $(this), anchor = t.attr('href') || '';

		$.get(anchor, function(data){
      $('#popup, .overlayer').remove();
	    if($.browser.msie && $.browser.version < 9){
	        popUp(innerShiv(data, false));
	    } else {
	        popUp(data);
	    }
		});					
		
		return false;
	});
	
	$('.underPages li').each(function(){
		var hasSubmenu = $('ul',this).length;
		if (hasSubmenu >= 1) {
			$(this).addClass('hasSubmenu');
		};
	});
	
	
	$('#innerNav').rdy(function(){
		var hasPages = $('.underPages',this).length;
		if (hasPages >= 1 ) {
			$('.content').addClass('subNav');
		};
	});

});

function last_class(elem, parent){
	elem.each(function(){
		$(this).find(parent).last().addClass('last');
	});	
}

last_class($('.dataTable'),'tr');

//Tabs

function tabs(){
	var t = $('.tabs'),
			tc = $('.tabContent'),
			onPage = $('.tabs').length;

	tc.hide();
		
	$('a',t).unbind('click').bind('click',function(){
		var th = $(this),
				tab = th.attr('href');
	
		tab = tab.split('#');
	
		if (!th.parent().hasClass('closePopup')) {
			$('li.s',t).removeClass('s');
			th.parent().addClass('s');
			$('#'+tab[1]).show().siblings('.tabContent').hide();
			return false;
		};

	});		
	if (onPage > 1) {
		t.first().find('li:first a').click();
	} else {
		$('li:first a',t).click();
	}
}

//Slider

function slider(){
	var t = $('.slider');
	$('.bigWrap li:first',t).addClass('active'); 
	
	$('.next, .prev',t).unbind('click').bind('click',function(){
		var th = $(this),
				current = $('li.active',t),
				showHide;
		
		if (th.hasClass('next')) {
			showHide = current.next();
		} else {
			showHide = current.prev();
		}
		
		showHide.addClass('active').fadeIn().siblings().fadeOut().removeClass('active');

		return false;
	});
		
}

//Popup

function popUp(data) {
	
	var baseHtml = '<div id="popup"><span class="round contentTop"></span><div class="innerZone"></div><span class="round contentBtm"></span></div>';
	
	var curScroll = $(window).scrollTop();
	
	$('<div class="overlayer" />')
	.css({
		'width' : $('body').width(),
		'height' : $('body').height()
	})
	.appendTo('body');
	
	$('html, body').animate({scrollTop : 0},'slow');
		
	$(baseHtml).appendTo('body');
		
	$('#popup .innerZone').empty();
	
	$(data).appendTo('#popup .innerZone');
	
	$('.closePopup').unbind('click').bind('click',function(){
		$('#popup, .overlayer').remove();
		$('html,body').animate({scrollTop : curScroll}, 'slow');
		return false;
	});
	
	$('.overlayer').unbind('click').bind('click',function(){
		$('.closePopup').trigger('click');
	});
	

	$('.dataTable').rdy(function(){
		last_class($('.dataTable'),'tr');
	})

	$('.slider').rdy(function(){
		slider();
	});
	
}

// Contact form validation/popup
var ajaxSubmitSuccess = function(responseText, statusText, xhr, $form) {
  $('#popup, .overlayer').remove();
  if($.browser.msie && $.browser.version < 9){
    popUp(innerShiv(responseText, false));
  } else {
    popUp(responseText);
  }
};
jQuery().ready(function () {
  jQuery('.contactForm').unbind('submit.defaultclearplaceholder').ajaxForm({
    'success': ajaxSubmitSuccess,
    'resetForm': true,
    'beforeSerialize': function($form, options) {
      var events = [];
      if (!('placeholder' in document.createElement("input"))) {
        options.resetForm = false;
        jQuery('input[placeholder]', $form).each(function() {
          events.push({ 'element': this, 'events': jQuery(this).data('events')});
          jQuery(this).data('events', {});
          clearValue(this);
        });
      }
      options.beforeSubmit = function (arr, $form1, options1) {
        jQuery.each(events, function() {
          jQuery(this.element).data(this.events).focus().blur();
        });
        var status = true;
        jQuery.each(arr, function (key, obj) {
          if ((obj.name == 'contactname' && obj.value == '') ||
              (obj.name == 'contactphone' && obj.value == '') ||
              (obj.name == 'contactzip' && obj.value == '')) {
            return status = false;
          }
        });
        if (!status) {
          alert('Udfyld venligst alle felter');
        } else {
          jQuery.each(events, function() {
            jQuery(this.element).val('').focus().blur();
          });
        }
        return status;
      };
    },  
  });
});

