//var staticdomain = 'http://static.cellsystems';
var staticdomain = 'http://static.cs.pixelproduction.de';

$(document).ready(function () {
  
    // check placeholder browser support
    if (!Modernizr.input.placeholder)
    {
 
        // set placeholder values
        $(this).find('[placeholder]').each(function()
        {
            $(this).val( $(this).attr('placeholder') );
        });
 
        // focus and blur of placeholders
        $('[placeholder]').focus(function()
        {
            if ($(this).val() == $(this).attr('placeholder'))
            {
                $(this).val('');
                $(this).removeClass('placeholder');
            }
        }).blur(function()
        {
            if ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))
            {
                $(this).val($(this).attr('placeholder'));
                $(this).addClass('placeholder');
            }
        });
 
        // remove placeholders on submit
        $('[placeholder]').closest('form').submit(function()
        {
            $(this).find('[placeholder]').each(function()
            {
                if ($(this).val() == $(this).attr('placeholder'))
                {
                    $(this).val('');
                }
            })
        });
 
    }  
	$('a[href^="http://"], a.blank, a[href*=".pdf"]').attr('target', '_blank');

	if ($('#gmap').length) {
		initialize();
		$('#getRoute').click(function (e) {
			var start;
			e.preventDefault();
			start = $('#start').val();
			calcRoute(start);
		});
	}
	var content = $.trim($('#info').html()).length;
	if (content > 0) {
		content = $('#info').html();
		$('#info').html('');
		$.setInfo(content);
	}

	$('#info').bind('click', function () {
		$(this).slideUp('slow');
	});

	$('ul.sf-menu').supersubs({
		minWidth: 12,
		maxWidth: 32,
		extraWidth: 1
	}).superfish({
   	pathLevels: 1,
    delay: 500,
		speed: 'fast',
    onShow: function () {
      $('.sf-menu li li').css({'background-color':'transparent'});
    },
    onHide: function () {
      $('.sf-menu li li').css({'background-color':'#ffffff'});
	  $('.sf-menu li li').css({'background-color':'transparent'});
    }
  });
	$('ul.sf-menu li:not(ul.sf-menu li li)').mouseenter(function () {
		$(this).next().find('.lis:not(li li .lis)').css({borderLeft: '1px solid #fff'});
	}).mouseleave(function () {
		$(this).next().find('.lis:not(li li .lis)').css({borderLeft: '1px solid #e0e0e0'});
	});

	$('a.md').fancybox({
		opacity: true,
		modal: false,
		cyclic: true,
		centerOnScroll: true,
		overlayOpacity: 0.8,
		titlePosition: 'over'
	});
  $(function() {
      var cache = {}, lastXhr;
      $('#sip').autocomplete({
          minLength: 3,
          source: function (request, response) {
              var term = request.term, data;
              if ( term in cache ) {
                data = cache[term];
                response($.map(data, function (item) {
                    return {
                        label: item.title,
                        value: item.url
                    };
                }));
                return;
              }
              lastXhr = $.ajax({
                  url: '/autosearch',
                  type: 'post',
                  dataType: 'json',
                  data: {
                      q: term
                  },
                  success: function (data, status, xhr) {
                      if (data) {
                      
                        cache[ term ] = data;
                        if ( xhr === lastXhr ) {
                          response($.map(data, function (item) {
                              return {
                                  label: item.title,
                                  value: item.url
                              };
                          }));
                        }
                      }
                  }
              });
          }
      });
  });
    $('#sip').bind('autocompleteselect', function (event, ui) {
        //$('#sip').val(ui.item.value);
        //$('#sform #sb').trigger('click');
        location.href = ui.item.value;
        $('body').html('Goto ' + ui.item.value);
    });

	$('#cyc').cycle({
		speed: 1500,
		timeout: 5000
	});

	if ($('#survey').length) {
		$('label[for="name"]').append(' *');
		if ($('#survey').attr('checked')) {
			$('#fieldset-Contact').show('slow');
		}
		$('#survey').click(function(){ 
			if ($(this).attr('checked')){
				$('#fieldset-Contact').show('slow');
			} else {
				$('#fieldset-Contact').hide('slow');
			}
		});
	}
	$('#emaillist').live('click', function(){ 
		if ($(this).attr('checked')){
			$('#fieldset-More').append('<div id="ifo"><p>After sending the form you will be redirected to our newsletter-form</p></div>');
		} else {
			$('#ifo').remove();
		}
	});
});
