var resizeYoutube = function() {
  if ($('#dataview_lesson').length == 0) return;
  
  var w = parseInt($(window).width() * .6) - 100;
  var h = parseInt(w / 560 * 340);
  var maxh = parseInt($(window).height() * .5);
  if (h > maxh) {
    h = maxh;
    w = parseInt(h * 560 / 340);
  }
  $('.youtube_view').width(w);
  $('.youtube_view object').width(w);
  $('.youtube_view').height(h);
  $('.youtube_view object').height(h);
  $('#dataview_lesson .detail_view_text').height(h - 57);
  if ($('#dataview_lesson .detail_view_text').length > 0) {
    $('#dataview_lesson .detail_view').slideDown(300, function() {
      $('#dataview_lesson .index_view').css('bottom', h + "px");
    });
  }
  else {
    $('#dataview_lesson .index_view').css('bottom', 0);
    $('#dataview_lesson .detail_view').slideUp(300);
  }
};

var updateCommentsTimeout;
var updateComments = function() {
  if (window.location.hash == '#home/') {
    clearTimeout(updateCommentsTimeout);
    updateCommentsTimeout = setTimeout(function() {
      if (window.location.hash == '#home/')
        ma.load($('#prikbord-message-form input[name="next"]').val() + '&noformclear', '', null, true);
    }, 10000);
  } else
    clearTimeout(updateCommentsTimeout);
};

/* jQuery placeholder */
(function($){
	
	var ph = "PLACEHOLDER-INPUT";
	var phl = "PLACEHOLDER-LABEL";
	var boundEvents = false;
	var default_options = {
		labelClass: 'placeholder'
	};
	
	//check for native support for placeholder attribute, if so stub methods and return
	var input = document.createElement("input");
	if ('placeholder' in input) {
		$.fn.placeholder = $.fn.unplaceholder = function(){}; //empty function
		delete input; //cleanup IE memory
		return;
	};
	delete input;

	$.fn.placeholder = function(options) {
		bindEvents();

		var opts = $.extend(default_options, options)

		this.each(function(){
			var rnd=Math.random().toString(32).replace(/\./,'')
				,input=$(this)
				,label=$('<label style="position:absolute;display:none;top:0;left:0;"></label>');

			if (!input.attr('placeholder') || input.data(ph) === ph) return; //already watermarked

			//make sure the input tag has an ID assigned, if not, assign one.
			if (!input.attr('id')) input.attr('id', 'input_' + rnd);

			label	.attr('id',input.attr('id') + "_placeholder")
					.data(ph, '#' + input.attr('id'))	//reference to the input tag
					.attr('for',input.attr('id'))
					.addClass(opts.labelClass)
					.addClass(opts.labelClass + '-for-' + this.tagName.toLowerCase()) //ex: watermark-for-textarea
					.addClass(phl)
					.text(input.attr('placeholder'));

			input
				.data(phl, '#' + label.attr('id'))	//set a reference to the label
				.data(ph,ph)		//set that the field is watermarked
				.addClass(ph)		//add the watermark class
				.after(label);		//add the label field to the page

			//setup overlay
			itemIn.call(this);
			itemOut.call(this);
		});
	};

	$.fn.unplaceholder = function(){
		this.each(function(){
			var	input=$(this),
				label=$(input.data(phl));

			if (input.data(ph) !== ph) return;
				
			label.remove();
			input.removeData(ph).removeData(phl).removeClass(ph);
		});
	};


	function bindEvents() {
		if (boundEvents) return;

		//prepare live bindings if not already done.
		$('.' + ph)
			.live('click',itemIn)
			.live('focusin',itemIn)
			.live('focusout',itemOut);
		bound = true;

		boundEvents = true;
	};

	function itemIn() {
		var input = $(this)
			,label = $(input.data(phl));

		label.css('display', 'none');
	};

	function itemOut() {
		var that = this;

		//use timeout to let other validators/formatters directly bound to blur/focusout work first
		setTimeout(function(){
			var input = $(that);
			$(input.data(phl))
				.css('top', input.position().top + 'px')
				.css('left', input.position().left + 'px')
				.css('display', !!input.val() ? 'none' : 'block');
		}, 200);
	};

}(jQuery));

$(function() {
  if (!($.browser.msie && (parseFloat($.browser.version) < 8.0)) &&
      !($.browser.mozilla && (parseFloat($.browser.version) < 1.9)))
    $('#browserCheckOverlay').remove();
  else $('body').html(
    '<div id="browserCheckOverlay">' +
      '<h1>The Guitar Community is not compatible with your current web browser.</h1>' +
      '<p>To use the Guitar Community, please make sure that you use a modern web browser (one of the following applications):</p>' +
      '<ul>' +
        '<li><strong>Google Chrome</strong> (recommended, <a class="external" href="http://www.google.com/chrome/">click here to download</a>)</li>' +
        '<li>Mozilla Firefox 3.5 or later (<a class="external" href="http://www.mozilla.com/en-US/firefox/firefox.html">click here to download</a>)</li>' +
        '<li>Apple Safari 4 or later</li>' +
        '<li>Opera 10.50 or later</li>' +
        '<li>Internet explorer 8 or later (Internet Explorer 8 is not recommended, use version 9 or later for the best experience)</li>' +
      '</ul>' +
      '<p>Please note that using a modern application to browse the web is nicer anyway, since it will protect you from viruses and loads web pages way faster.</p>' +
    '</div>'
  );
  
  ma.mutation(resizeYoutube);
  ma.mutation(updateComments);
  
  if (window.location.hash.length <= 1 && window.location.pathname == ma.root)
    $($('#main_menu a')[0]).click();
});

$(window).resize(resizeYoutube);

/* Google analytics code */ /*
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-6883420-7']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })(); */
/* end analytics */
