$(function() {
	//return; 
	/*
	KEEP SIFR TO HAND, TO DEMONSTRATE PERFORMANCE ISSUE ^KR
	
	sIFR.activate(bookman);
	var bookman = {
     src: '/public/swf/fonts/rockwell.swf'
    };

    //sIFR.domains = ['thisisbd.local'] // Don't check for domains in this demo
    //sIFR.useStyleCheck = true;

    sIFR.replace(bookman, {
      selector: '#header p'
      ,css: [
        '.sIFR-root { text-align: left;  background-color: #efefef; color: #333333; }'
        ,'a { text-decoration: none; color: #666666;}'
		,'.work { text-decoration: none; color: #FF9600;}'
		,'.blog { text-decoration: none; color: #2AAEB2;}'
		,'.contact { text-decoration: none; color: 	#AD91FF;}'
		,'a:hover { text-decoration: none; color: #333333;}'
      ]
    });
	*/
	
	//$('ul#grid').find('#case_1,#case_4').corner('20px'); //not working yet due to css positioning of children (probably)
	
	
	
	
	
	$('#grid li.rows_1, #related li.rows_1').hover( 
		function()
		{	$(this).bdSlide(
				{ 	'opacity' : '0.2',	
					'animHeight' : 0
				});
		},
		function()
		{	$(this).bdSlide(
				{ 	'opacity' : '1',	
					'animHeight' : $(this).height() -28 + 'px'
				});
		}		
	);
	$('#grid li').filter(':not(.rows_1)').hover( 
		function()
		{	$(this).bdSlide(
				{ 	'opacity' : '0.2',	
					'animHeight' : $(this).height() / 2 + 'px'
				});
		},
		function()
		{	$(this).bdSlide(
				{ 	'opacity' : '1',	
					'animHeight' : $(this).height() -28 + 'px'
				});
		}		
	);
	$('#grid li').click(
		function(){
			var link = $(this).find("a").attr("href");
			if (link) {
				location.href=link;
			}
	});
	
	
  
  
  
	
	/*
	
	// change anim values to affect all animations
	var anim = {
		'speed' : 150
	};
	
	$('#grid li#case_1 img').bdSlideImage(
		$.extend(
			anim,{ 'delay': 1, 'direction': 'left' }
		)
	);
	$('#grid li#case_2 img').bdSlideImage(
		$.extend(
			anim,{ 'delay': 2, 'direction': 'top' }
		)
	);
	$('#grid li#case_3 img').bdSlideImage(
		$.extend(
			anim,{ 'delay': 3, 'direction': 'right' }
		)
	);
	$('#grid li#case_4 img').bdSlideImage(
		$.extend(
			anim,{ 'delay': 10, 'direction': 'top' }
		)
	);
	$('#grid li#case_5 img').bdSlideImage(
		$.extend(
			anim,{ 'delay': 5, 'direction': 'left' }
		)
	);
	$('#grid li#case_6 img').bdSlideImage(
		$.extend(
			anim,{ 'delay': 6, 'direction': 'left' }
		)
	);
	$('#grid li#case_7 img').bdSlideImage(
		$.extend(
			anim,{ 'delay': 9, 'direction': 'left' }
		)
	);
	$('#grid li#case_8 img').bdSlideImage(
		$.extend(
			anim,{ 'delay': 8, 'direction': 'top' }
		)
	);
	$('#grid li#case_9 img').bdSlideImage(
		$.extend(
			anim,{ 'delay': 4, 'direction': 'right' }
		)
	);
	$('#grid li#case_10 img').bdSlideImage(
		$.extend(
			anim,{ 'delay': 11, 'direction': 'top' }
		)
	);
	$('#grid li#case_11 img').bdSlideImage(
		$.extend(
			anim,{ 'delay': 7, 'direction': 'right' }
		)
	);
	
	*/
	
	
	
	//$('#grid')
	/*$('#nav').corner('6px left');
	$('#nav ul').corner('6px right');
	$('#i_made_this li').corner('13px');
	$('#bd_services_nav').corner('13px');
	$('#bd_services .text a.more').children().andSelf().corner('10px');*/
	
	/*$('#bd_services li:eq(2)').addClass('selected');
	$('#bd_services_nav li').each(
		function(index) 
		{	$(this).click(
				function(){
					$('#bd_services li.selected').animate(
						{	left: '+=940'
  						}, 
						500, 
						function() {
							// Animation complete.
  							$(this).css({'left': '-940px'}).removeClass('selected');
						}
					);
					$('#bd_services li').eq(index).animate(
						{	left: '+=940'
  						}, 
						500, 
						function() {
							// Animation complete.
							$(this).addClass('selected');
  							
						}
					);
							   
			});  
		
		}
	);*/
	
	$('#bd_services li').hide();
	$('#bd_services li').eq(0).show();
	$('#bd_services_nav li').each(
		function(index) 
		{	var i = index;
			$(this).click(
				function(){
					$('#bd_services li').hide();
					$('#bd_services li').eq(i).fadeIn('slow');
					return false;			   
			});
		}
	);
	$('#case_study li').hide();
	$('#case_study li').eq(0).show();
	
	/*$('#case_study ul').paginate();
	
	$('#pagination li.page:first').addClass('current');
	$('#pagination li').paginate({	'select' : '#case_study li'});*/
	/*$('#pagination li.page a').each(
		function(index) 
		{	var i = index;
			$(this).click(
				function(event){
					event.preventDefault();
					$('#case_study li').hide();
					$('#case_study li').eq(i).fadeIn('slow');
					$('#pagination li.page > span').wrap('<a></a>');
					$(this).find('span').unwrap();
		   
			});		
		}
	);
	$('#pagination li.relative a').each(
		function(index) 
		{	var i = index;
			$(this).click(
				function(event){
					event.preventDefault();
					var current = $('#case_study li:visible').index();
					if($(this).parent().is('.previous'))
					{ 	var current = $('#case_study li:visible').index();
						$('#case_study li').hide();
						$('#case_study li').eq(current-1).fadeIn('slow');
					}
					if($(this).parent().is('.next'))
					{ 	var current = $('#case_study li:visible').index();
						$('#case_study li').hide();
						$('#case_study li').eq(current+1).fadeIn('slow');
					}
		   
			});		
		}
	);*/
	
			

	//$('#smc').css( { 'left': '900px'});
	
	
	
	
	
	
	
	
	
	$("body").removeClass("no-js").addClass("js");
	page_name = document.body.id;
/*
	function get_grid_text_pos(grid_type) {
		if (grid_type == "related") {
			cells = $("#related li");
		} else {
			cells = $("#grid li");
		}
		num_cells = $(cells).length;
			cell_text_pos = new Array();
		
		for (i=0; i<num_cells; i++) {			
			cell_text_pos[i] = $(cells.get(i)).find(".text").css("top");
		}
	}
	
	if (page_name=="index" || page_name=="work" || page_name=="services") {
		get_grid_text_pos();
	} else {
		get_grid_text_pos("related");
		$("#related li").each(function() {
			if ($(this).find("a").attr("href")==undefined) {
				$(this).css("cursor","default");
			}
		});
	}
	
	$("#grid li, #related li")
		.hover(
			function() {			
				if ($(this).hasClass("rows_1") || page_name == "services") {
					new_top = 0;
				} else {
					new_top = $(this).height()/2;
				}
				if ($(this).hasClass("orig_20")) {
					$("#content-main>a.stand-out").stop().animate({ opacity:"0.2" });
				}
				$(this).find(".text").stop().animate({ top:new_top+"px" }, 200);
			},
			function() {
				$(this).find(".text").stop().animate({ top:cell_text_pos[$(cells).index(this)] }, 200);
				if ($(this).hasClass("orig_20")) {
					$("#content-main>a.stand-out").stop().animate({ opacity:"1" });
				}
			}
		)
		.click(function() {
			link = $(this).find("a").attr("href");
			if (link) {
				location.href=link;
			}
			return false;
		})
	;
	*/
	/*
	$("#grid.case-study img").hover(
		function() {			
			$(this).parent().find(".text").stop().animate({ top:100+"%" }, 200);
		},
		function() {
			$(this).parent().find(".text").stop().animate({ top:cell_text_pos[$(cells).index(this)] }, 200);
		}
	);
	*/
	
	// Set/show/hide default input value (acting as input label; non-js users see different version)
	
	function form_input_message_init(form_id, form_message) {
		input_box = $(form_id);
		$(input_box).val(form_message);
		$(input_box).focus(function() {
			if ($(this).val() == form_message) {
				$(this).val("");
			}
		});
		$(input_box).blur(function() {
			if (!$(this).val()) {
				$(this).val(form_message);
			}
		});
	}
	if (page_name=="blog" || page_name=="work" || page_name=="case-study") {
		form_input_message_init("#in_search", "Search");
	}
	else if (page_name=="contact") {
		form_input_message_init("#in_postcode", "Your Postcode");
	}	
	
	// Add videos
	
	if (page_name==("blog")) {
		var flash_vars = {};
		var params = {};
		var attributes = { "class": "video" };
		var video_width = 640;
		var video_height = 360;		
		
		$("#content-primary .video").each(function() {
			
			if ($(this).hasClass("vimeo")) {
				var video_swf = "http://vimeo.com/moogaloop.swf";
				var flash_vars = {
					clip_id: this.id,
					server: "vimeo.com",
					show_title: 0,
					show_byline: 0,
					show_portrait: 0,
					fullscreen: 0
				};
			}
			else if ($(this).hasClass("youtube")) {
				var video_swf = "http://www.youtube.com/v/"+this.id;
				var flash_vars = {
					hl: "en",
					fs: 1,
					rel: 0
				};
				var params = {
					allowFullScreen: true,
					allowscriptaccess: "always"
				};
			}

			swfobject.embedSWF(video_swf, this.id, video_width, video_height, "9.0.0","/public/swf/expressInstall.swf", flash_vars, params, attributes);			
		});	
	}
	
	// Object converter	to compare string with array values
	function oc(a)
	 {
	    var o = {};
	    for (var i = 0; i < a.length; i++)
	    {
	        o[a[i]] = '';
	    }
	    return o;
	}
	
	$.fn.pause = function(n) {
	    return this.queue(function() {
	        var el = this;
	        setTimeout(function() {
	            return $(el).dequeue();
	        }, n);
	    });
	};
	
	function compare_grid(html) {
		current_grid = $("#grid li");
		num_blocks = current_grid.length;
		same_blocks = new Array;
		
		$(html).find("li").each(function() {
			for (i=0; i<num_blocks; i++) {
				if (this.id == current_grid[i].id && this.className == current_grid[i].className) {
					same_blocks.push(i);
				}
			}
		});
		
		for (i=0; i<num_blocks; i++) {
			if (i in oc(same_blocks)) {
				// identical block to original
			} else {
				$(current_grid[i]).queue("fx",
					function() {
					    $(this).fadeOut(500);
					    var self = this;
					    setTimeout(function() {
					        $(self).dequeue("fx");
					    },
					    1000);
					}
				);
			}
		}
	}
	/*
	$.ajax({
		url: "/test",
		cache: false,
		success: function(html){
			compare_grid(html);
		}
	});
	*/
	
	//$('#grid #case_1 img').show('slow');
	
	
	
	
	
	
});
