// usage: log('inside coolFunc', this, arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function(){
  log.history = log.history || [];   // store logs to an array for reference
  log.history.push(arguments);
  arguments.callee = arguments.callee.caller;  
  if(this.console) console.log( Array.prototype.slice.call(arguments) );
};
// make it safe to use console.log always
(function(b){function c(){}for(var d="assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info,log,markTimeline,profile,profileEnd,time,timeEnd,trace,warn".split(","),a;a=d.pop();)b[a]=b[a]||c})(window.console=window.console||{});


/* Check if the document is ready */

function ready () {
  docReady (jQuery, window, document);
}


var DOMContentLoaded;

// Cleanup functions for the document ready method
if ( document.addEventListener ) {
	DOMContentLoaded = function() {
		document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
		ready();
	};

} else if ( document.attachEvent ) {
	DOMContentLoaded = function() {
		// Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
		if ( document.readyState === "complete" ) {
			document.detachEvent( "onreadystatechange", DOMContentLoaded );
			ready();
		}
	};
}




// browser event has already occurred.
if ( document.readyState === "complete" ) {
	// Handle it asynchronously to allow scripts the opportunity to delay ready
	setTimeout( ready, 1 );
}

// Mozilla, Opera and webkit nightlies currently support this event
if ( document.addEventListener ) {
	// Use the handy event callback
	document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );

	// A fallback to window.onload, that will always work
	window.addEventListener( "load", ready, false );

// If IE event model is used
} else if ( document.attachEvent ) {
	// ensure firing before onload,
	// maybe late but safe also for iframes
	document.attachEvent("onreadystatechange", DOMContentLoaded);

	// A fallback to window.onload, that will always work
	window.attachEvent( "onload", ready );
}



// If the Document is ready
function docReady ($, window, document, undefined) {
  /**
   * Get the value of a cookie with the given key.
   * @example $.cookie('the_cookie');
   * @desc Get the value of a cookie.
   *
   * @param String key The key of the cookie.
   * @return The value of the cookie.
   * @type String
   *
   * @name $.cookie
   * @cat Plugins/Cookie
   * @author Klaus Hartl/klaus.hartl@stilbuero.de
   */
  jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
  };
  
  
  // Check this again!
  $(document).ready( function () {
  
    /* Don't show Context-Menu */
    $('img').bind('contextmenu', function (e) {
      return false;
    });
    
    
    
  
    /* Hide Context-Menu for Slider */
    $('.tx-cfamooflow-pi1').prepend('<div class="hide-context-menu-for-mooflow-slider" />');
    
    
    
    
    /* Setting Function for saving the cookie */
    function set_cookie (cookie, val, expires) {
      if ( expires === undefined ) {
        $.cookie(cookie, val, {
          'path' : '/'
        });
      } else {
        $.cookie(cookie, val, {
          'expires' : expires,
          'path' : '/'
        });
      }
    }
    
    
    function get_cookie (cookie) {
      log( $.cookie(cookie) )
      return $.cookie(cookie);
    }
    
    
    function remove_cookie (cookie) {
      log( 'Removed ' + cookie )
      $.cookie( cookie, null );
    }
    
    if ( location.hash && location.hash.substr(0,7) == "#remove" ) {
      var cookie = location.hash.substr( 8 );
      remove_cookie( 'lottery' );
    }

    
    /* Overlay for Lottery */
    /*
    // changes by claudius: deleted this from if condition:  || get_cookie ('lottery') == false 
    if (get_cookie ('lottery') == null) { 
      // Append all HTML
      if ($('#my-overlay').size() < 1) {
        $('<div />', {
          id : 'my-overlay'
        }).click(function () {
          $('#my-overlay').fadeOut(function () {
            $(this).remove();
          });
          $('#close-overlay').fadeOut(function () {
            $(this).remove();
          });
          $('#show-room').fadeOut(function () {
            $(this).remove();
          });
        }).appendTo('body');
      }
      
            
      
      if ($('#show-room').size() < 1) {
        $('<div />', {
          'id' : 'show-room'
        }).appendTo('body');
      }
      
      
      if ($('#show-room').size() > 0) {
        $('#show-room').html('' +
          '<h2>Win an Apple iPad 2!</h2>' + 
          '<p class="teaser">It’s easy and fast.  Just click on the YES-Button below, fill out the Contact Form and maybe YOU will be the Lucky Winner!</p>' +
          '<a href="#!/nein" class="button button-back"><strong>No</strong> <br> I don’t want <br>to take part.</a>' +
          '<a href="#!/ja" class="button button-right"><strong>Yes</strong><br> I would like <br>to take part.</a>' +
          '<a href="#!/later" class="button button-back button-later"><strong>Later</strong><br>Ask me next <br> time again.</a>'
        );
        
        $( 'embed' ).each( function() {
          $( this ).css( 'height', '0' );
        });
        
        
        
        $('.button').click( function (e) {
          e.preventDefault ();
          var reference = $( this ).attr( 'href' );
              reference = reference.split( /\!\//g );
              reference = reference[1];
          log( reference );          
          

          
          
          $( 'embed' ).each( function() {
            $( this ).css( 'height', $( this ).attr( 'height' ) + 'px' );
          });

          if (reference == 'nein') {
            $('#my-overlay').trigger('click');
            set_cookie('lottery', 'no', 9999);
          }

          else if (reference == 'ja') {
            $('#my-overlay').trigger('click');
            set_cookie('lottery', 'no', 9999);
            window.location.href = 'http://www.headrest.de/news/win-an-ipad/';

          } else if (reference == 'later') {
            $('#my-overlay').trigger('click');
            set_cookie('lottery', 'no', 1);
          }
        });
      }
      
    
    }*/
    
  });
  
  
}
