// Common js functions 

// fixing the conflict issues with prototype
jQuery.noConflict();
     
 // scripts intiated in document.ready
 jQuery(document).ready(function($){
  	// initialize the homepage content slider
	slideshow_width_reset();
	// resetting the width upon resize
	$(window).resize(function() {
	  slideshow_width_reset();
	});
	// fizing a sizing issue with multiple reset
	slideshow_width_reset();
	if ($(".fancybox").size()>0) {
		$(".fancybox").fancybox({
			'padding':0,
			'cyclic':true,
			'centerOnScroll':true,
			'overlayOpacity':.84,
			'overlayColor':'#000'		
		});
	}
	
	//handle the search engine
	$('#q').keypress(function(e){
	  var c = e.which ? e.which : e.keyCode;
      if(c == 13){
		$('#search').submit();
		e.preventDefault();
	   }
      });
 });

// reset the homepage slideshow width
var new_height;
var new_width;
var block_width=377;
function slideshow_width_reset() {
	// reset the width and height for the homepage slideshow
	if (jQuery(".home-slideshow").size()>0){
		jQuery(".home-slideshow #home_slides li .right").width(jQuery("#home_slides").width()-255);
		new_width = jQuery(".home-slideshow #home_slides li .right img").width();
		//new_height = jQuery(".home-slideshow #home_slides li .right img").height();
		new_height = parseInt(new_width/2.3);
		jQuery(".home-slideshow, .home-slideshow #home_slides li, .home-slideshow #home_slides").height(new_height);
		jQuery(".home-slideshow #home_slides li .left").height(new_height-104);
		jQuery(".home-slideshow .slideshow-base").height(new_height-26*2);
	}
	// reset the width for the content right side
	jQuery(".content-col-2").width(jQuery(".content").width()-275);
	jQuery(".project-page .content-col-2").width(jQuery(".content").width()-310);
	// reset the width and height for the project page slideshow
	if (jQuery(".project-slideshow").size()>0){
		new_width = jQuery(".project-slideshow #project_slides").width();
		//new_height = jQuery(".project-slideshow #project_slides li img").height();
		new_height = parseInt(new_width/2.288);		
		jQuery(".project-slideshow #project_slides").height(new_height);
	}
	// adding clear after n number of blocks on resize. 
	if (jQuery(".block-listing").size()>0){
		new_width = jQuery(".block-listing").width();
		jQuery(".block-listing").find(".clear").remove();
		if (new_width>=block_width*2 && new_width<block_width*3)
			jQuery(".block-listing .block:nth-child(2n)").after("<div class='clear'></div>");	
		if (new_width>=block_width*3 && new_width<block_width*4)
			jQuery(".block-listing .block:nth-child(3n)").after("<div class='clear'></div>");	
		if (new_width>=block_width*4 && new_width<block_width*5)
			jQuery(".block-listing .block:nth-child(4n)").after("<div class='clear'></div>");	
		if (new_width>=block_width*5 && new_width<block_width*6)
			jQuery(".block-listing .block:nth-child(5n)").after("<div class='clear'></div>");	
		if (new_width>=block_width*6 && new_width<block_width*7)
			jQuery(".block-listing .block:nth-child(6n)").after("<div class='clear'></div>");	
		if (new_width>=block_width*7 && new_width<block_width*8)
			jQuery(".block-listing .block:nth-child(7n)").after("<div class='clear'></div>");	
		if (new_width>=block_width*8 && new_width<block_width*9)
			jQuery(".block-listing .block:nth-child(8n)").after("<div class='clear'></div>");	
		if (new_width>=block_width*9 && new_width<block_width*10)
			jQuery(".block-listing .block:nth-child(9n)").after("<div class='clear'></div>");
		if (new_width>=block_width*10 && new_width<block_width*11)
			jQuery(".block-listing .block:nth-child(10n)").after("<div class='clear'></div>");	
		if (new_width>=block_width*11 && new_width<block_width*12)
			jQuery(".block-listing .block:nth-child(11n)").after("<div class='clear'></div>");
		if (new_width>=block_width*12 && new_width<block_width*13)
			jQuery(".block-listing .block:nth-child(12n)").after("<div class='clear'></div>");	
		if (new_width>=block_width*13 && new_width<block_width*14)
			jQuery(".block-listing .block:nth-child(13n)").after("<div class='clear'></div>");
	}
}
