﻿function updateDemoNotes() {

	var notes = $('#demo-frame .demo-description');
	if ($('#demo-notes').length == 0) {
		$('<div id="demo-notes"></div>').insertAfter('#demo-config');
	}
	$('#demo-notes').empty().html(notes.html());
	notes.hide();

}

function updateDemoSource(source) {
	if ($('#demo-source').length == 0) {
		$('<div id="demo-source"><a href="#" class="source-closed">View Source</a><div><pre><code></code></pre></div></div>').insertAfter('#demo-notes');
		$('#demo-source').find("> a").click(function() {
			$(this).toggleClass("source-closed").toggleClass("source-open").next().toggle();
			return false;
		}).end().find("> div").hide();
	}
	$('#demo-source code').empty().text(source);
}

function resetDemos() {
	( $.datepicker && $.datepicker.setDefaults($.extend({showMonthAfterYear: false}, $.datepicker.regional[''])) );
	$(".ui-dialog-content").remove();
}

$(document).ready(function() {

	//show details/hide details

	var showExamples = true;
	$(".toggle-docs-detail").toggle(function(e){
		var details = $(this).text("Hide details")
			.parent().next("ul").find("li > div:first-child").addClass("header-open");
		if ( showExamples ) {
			details.nextAll().show();
		} else {
			details.next().show();
		}
		e.preventDefault();
	},function(e){
		$(this).text("Show details")
			.parent().next("ul").find("li > div:first-child").removeClass("header-open")
			.nextAll().hide();
		e.preventDefault();
	});

	$(".toggle-docs-example").click(function(e){
		if ( showExamples ) {
			showExamples = false;
			$(".toggle-docs-example").text("Show examples").parent().next("ul").find("div.header-open ~ .option-examples, div.header-open ~ .event-examples").hide();
		} else {
			showExamples = true;
			$(".toggle-docs-example").text("Hide examples").parent().next("ul").find("div.header-open ~ .option-examples, div.header-open ~ .event-examples").show();
		}
		e.preventDefault();
	});

	//Initially hide all options/methods/events
	$('div.option-description, div.option-examples, div.event-description, div.event-examples, div.method-description, div.method-examples').hide();
	
	//Make list items collapsible
	$('div.option-header span, div.event-header span, div.method-header span').live('click', function() {
		var details = $(this).parent().toggleClass('header-open');
		if ( showExamples ) {
			details.nextAll().toggle();
		} else {
			details.next().toggle();
		}
	});
		
});

$(window).bind('load', function() {
	//If we use it as docs page, go to the selected option
	if(window.location.hash && window.location.href.indexOf('/docs/') != -1) {
		gotoHash();
	}
});

function listenToHashChange() {
	
	var savedHash = window.location.hash;
	
	window.setInterval(function() {
		
		if(savedHash != window.location.hash) {
			savedHash = window.location.hash;
			if(window.location.hash && window.location.href.indexOf('/docs/') != -1)
				gotoHash();
			//Since we have bind click event on demos link and load hash on document.ready
			//if(window.location.hash && window.location.href.indexOf('/demos/') != -1)
			//	loadHash();
		}
		
	},200);
	
}

function loadHash() {
	
	$('#demo-config-menu a').each(function() {
		if(this.getAttribute('href').indexOf('/'+window.location.hash.substr(1)+'.html') != -1) {

			$(this).parents('ul').find('li').removeClass('demo-config-on');
			$(this).parent().addClass('demo-config-on');

			loadDemo(this.getAttribute('href'));
		}
	});
	
}

function gotoHash() {
	
	var hash = window.location.hash.substr(1).split('-');
	var go = hash[1] ? hash[1] : hash[0];
	var resolve = { overview: 0,option: 1,event: 2,method: 3,theming: 4 };

	$("#widget-docs").tabs('select', resolve[hash[0]]);
	var h3 = $("#widget-docs a:contains("+go+")").parent();
	h3.parent().parent().toggleClass("param-open").end().next().toggle();
	
	$(document).scrollTop(h3.parent().effect('highlight', null, 2000).offset().top);
		
}
