// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

$(document).ready(function(){
	
	var flashvars = false;
	var params = {
		wmode: "transparent",
	  menu: "false",
	  flashvars: "page_name=" + $("body")[0].id
	};

	//swfobject.embedSWF("/flash/main.swf", "replacement", "100%", "100%", "9", "/flash/expressInstall.swf", flashvars, params );

	$("ul.scrollable").jScrollPane({
		dragMinHeight: 80,
		dragMaxHeight: 80,
		scrollbarWidth: 20, 
		scrollbarMargin: 10
	});
	
	$("#seminars ul.scrollable a").click(function(e){
		e.preventDefault();
		if (document.getElementById("pages_seminars")) {
			swfobject.embedSWF($(this).attr("rel"), "vid", "620", "380", "9");
			$("#video_title").html("<span>" + $(this).siblings("h3").text() + "</span>");
			Cufon.replace('#video_title.sifr span');
		}
		window.location.href = "/videos/#/" + $(this).parent().attr("id");
	});
	
	$("#seminars ul.scrollable h3").click(function(e){
		e.preventDefault();
		if (document.getElementById("pages_seminars")) {
			swfobject.embedSWF($(this).siblings("a").attr("rel"), "vid", "620", "380", "9");
			$("#video_title").html("<span>" + $(this).text() + "</span>");
			Cufon.replace('#video_title.sifr span');
		}
		window.location.href = "/videos/#/" + $(this).parent().attr("id");
	});
	
	if (document.getElementById("pages_seminars")) {
		var h = window.location.href.split("#/")[1];
		if (h) {
			$("#large_video").html("<div id='vid'></div>");
			swfobject.embedSWF($("#" + h + " a").attr("rel"), "vid", "620", "380", "9");
			$("#video_title").html("<span>" + $("#" + h + " h3").text() + "</span>");
			Cufon.replace('#video_title.sifr span');
		} else {
			var first = $(".scrollable li a")[0];
			swfobject.embedSWF($(first).attr("rel"), "vid", "620", "380", "9");
			$("#video_title").html("<span>" + $(first).siblings("h3").text() + "</span>");
			Cufon.replace('#video_title.sifr span');
		}
	}
			 
	$("div#flickr-slider").flickr({
		api_key: "b85d5098c1b504f945954e3c686875cf",
		type: 'search',
		user_id: "37157699@N05",
		// per_page: 10,
		thumb_size: "m",
		callback: lightboxLoad
	});

	function lightboxLoad(el) {
		$('div#flickr-slider ul li a').lightBox();
		$('#flickr ul').addClass('jcarousel-skin-andary');
		$('div#flickr-slider ul').jcarousel({
			auto: 5,
			scroll: 2,
			itemLoadCallback: loadMoreFlickr
		});
		
		$('li.jcarousel-item a').each(function(){
			var title = $(this).attr('title');
			$(this).parent().append("<span>" + title + "</span>");
		});
	}
	
	function loadMoreFlickr (carousel, state) {
	}

	// Rid BLOG_POST_IDS of the 
	$("div#blog ul li").each(function(){
		BLOG_POST_IDS.pop(parseInt($(this).attr("id")));
	});

	$('div#office_gallery ul').jcarousel({scroll: 1});
	
	// Get Blog Post IDs:
	$.getJSON("/posts.json", function(data){
		if (data) {
			for (var i in data) {
				BLOG_POST_IDS.push( data[i].post.id );
			}
		}
	});

	// Blog Module (x)'s:
	var closeClicks = function(e){
	  e.preventDefault();
		
		if (BLOG_POST_IDS.length > 0) {
			
			var theese = this;
			var new_post_id = parseInt($(this).parent().attr("id").split("-")[1]);

			$.getJSON ( "/posts/" + new_post_id + ".json", function ( data ) {
				
				var id = data.post.id;
				var date = data.post.updated_at.split ( "T" ) [ 0 ];
				var body = data.post.body;
				
				var newPost = $( "<li id='blogpost-" + id + "' class='closed'><a class='close' href='#'><span>close</span></a><a href='/posts/" + id + "'><span class='date'>" + date + "</span><br /><span class='post'>" + body + "</span></a></li>" );
				$( "div#blog ul" ).append ( newPost );
				$("div#blog ul li span.post").truncate(44);
				
				$("a.close").click(closeClicks);
				
				$(theese).parent().slideUp("normal", function(){
          $(theese).parent().remove();
        });
				
				BLOG_POST_IDS.pop ( new_post_id );
				
			} );
			
		}
		
	}

	$("a.close").click(closeClicks);

	$('#content-container').tabs({
		show:function(){
			Cufon.replace(".sifr");
		},
		select: function(event, ui) {
      var url = $.data(ui.tab, 'load.tabs');
      if( url ) {
          location.href = url;
          return false;
      }
      return true;
 		}
	});
	
	// Testimonials:
	if ($("body")[0].id == "testimonials_index") {
		$('#testimonials').accordion({ 
			clearStyle: true,
			icons: { 'header': 'ui-icon-plus', 'headerSelected': 'ui-icon-minus' } 
			});

	}
	
	// Blog:
	if ($("body")[0].id == "posts_index") {
		$('#posts').accordion({ 
			navigation: true,
			clearStyle: true,
			collapsible: true,
			icons: { 'header': 'ui-icon-plus', 'headerSelected': 'ui-icon-minus' } 
			});

	}
	
	// Pure Power Page
	if ($('body')[0].id == "pages_ppm") {
		$('#clients').accordion({ 
			navigation: true,
			clearStyle: true,
			collapsible: true,
			icons: { 'header': 'ui-icon-plus', 'headerSelected': 'ui-icon-minus' } 
			});
		
	}
	
	/* contact page */
//	load_map();
	
	/* doctor locator */
	swfobject.embedSWF("/flash/assets/map.swf", "locator_map", "220", "137", "9");
	

});

function load_map() {
	
	if (document.getElementById("map")) {
		// $('map').css({'width': 300, 'height': 300, 'border': '1px solid #000'});
	  if (GBrowserIsCompatible()) {
			var map = new GMap2(document.getElementById("map"));
			var marker = new GMarker(new GLatLng(43.48015689025832,-79.65710163116455));
			map.addControl(new GSmallMapControl());
			map.setCenter(new GLatLng(43.48015689025832,-79.65710163116455), 13);
			map.addOverlay(marker);
		}
	}
	
}
