/*	common.js Chicken House javascript 
	
	Author: Chris How
	Copyright: 2009 primesolid.com. All rights reserved.

*/

// constants

var pauseAnimations = false;

var leftColWidth = 117;
var contentColWidth = 961;
var rightColWidth = 117;
var totalContentWidth = leftColWidth + contentColWidth + rightColWidth;

jQuery.noConflict();

 
jQuery(document).ready(function(){
	registerPositionAdjustments();
 });

 
 function registerPositionAdjustments() {
	adjustContentPositions();
 	resizeBackgroundImage();
 	jQuery(window).resize(function() {
		resizeBackgroundImage();
		adjustContentPositions();
 	});
 }
 
 function adjustContentPositions() {
 	var windowWidth = jQuery(window).width();
	var excessWidth = jQuery('#container').width() - windowWidth;
	var currentLeftColWidth;
	var leftColShrinkVal;
	var leftColAdjustment;
	var newContentWidth;
	
//	console.log('excessWidth : %d', excessWidth);
	
	currentLeftColWidth = jQuery('#left_background_column').width();
	currentRightColWidth = jQuery('#right_background_column').width();
	
	if (excessWidth > 0 || jQuery('#container').width() < totalContentWidth) {
		leftColAdjustment = gaussianRound(excessWidth / 2);
		leftColShrinkVal = currentLeftColWidth - leftColAdjustment;
		jQuery('#left_background_column').css('width', leftColShrinkVal >= 0 ? leftColShrinkVal : 0);
		rightColShrinkVal = currentRightColWidth - (excessWidth - leftColAdjustment);
		jQuery('#right_background_column').css('width', rightColShrinkVal >= 0 ? rightColShrinkVal : 0);
		
		jQuery('#container').css('width', newContentWidth = (contentColWidth + leftColShrinkVal  + rightColShrinkVal));
		if (newContentWidth <= contentColWidth) {
			jQuery('body').css('overflow-x', 'auto');
		} else { 
			jQuery('body').css('overflow-x', 'hidden');
		}
	}
	 	 
 }
 

function gaussianRound(x) {
    var absolute = Math.abs(x);
    var sign     = x == 0 ? 0 : (x < 0 ? -1 : 1);
    var floored  = Math.floor(absolute);
    if (absolute - floored != 0.5) {
        return Math.round(absolute) * sign;
    }
    if (floored % 2 == 1) {
        // Closest even is up.
        return Math.ceil(absolute) * sign;
    }
    // Closest even is down.
    return floored * sign;
}


var pic_width = 1254;
var pic_height = 1111;
var	pic_aspect_ratio = pic_width / pic_height;

 
 function resizeBackgroundImage() {
	var win_width = jQuery(window).width();
	var win_height = jQuery(window).height();
	var win_aspect_ratio = win_width / win_height;

	current_width = jQuery('#grass_image').width();
	if (current_width > 1) {
		pic_width = current_width;
	}	

	current_height = jQuery('#grass_image').height();
	if (current_height > 1) {
		pic_height = current_height;
	}
	
	
	if (win_aspect_ratio >= pic_aspect_ratio) {
		// scale pic to fit window width
		var new_height = (win_width / pic_width) * pic_height;
		var new_width = win_width;
	} else {
		// scale pic to fit window height		
		var new_height = win_height;
		var new_width = (win_height / pic_height) * pic_width;
	}
	
	// center image
	new_top = 0 - ((new_height - win_height) / 2);
	new_left =  0 - ((new_width - win_width) / 2);
//	console.log('new_top: ' + new_top + 'new_left: ' + new_left);
	
	jQuery('#grass_image').css({top: new_top, left: new_left, height: new_height, width: new_width});
	jQuery('#image_holder').css({height: win_height, width: win_width, visibility:"visible", display:"inline"});
 }
 
  
 function moveFirstBookInSmallCarouselToEnd() {
	 // needed because specifying the start position in jCarouselLite doesn't seem to work :(
	 // who'd have thought that insertAfter _moves_ the element? Well it does. :P	 
	 if (jQuery('#new_books_carousel_small li').length > 1) {
		 jQuery('#new_books_carousel_small li:first').insertAfter('#new_books_carousel_small li:last');
	 }
 }
 
 function initNewsTicker() {
	 // copy items from hidden news ticker to visible news ticker
	 var tickerItems = jQuery('#hidden_news_ticker li');

	 jQuery('#news_ticker').empty();
	 
	 // reset position and width
	 jQuery('#news_ticker_div').css('left', '0px');
	 jQuery('#news_ticker_div').width('415px');
	 
	 
	 if (tickerItems.length > 0) {
		 for(var i = 0; i < tickerItems.length; i++) {
			 jQuery('#news_ticker').append(jQuery(tickerItems[i]).clone());
		 }
	 }
	 
	 // start the ticker
	var options = {
	  		newsList: "#news_ticker",
	 		startDelay: 10,
			firstRun:1	 		
		};
	clearTimeout(tickerTimeoutId);
	jQuery().newsTicker(options);
 }
	

 function setData(elementSelector, varName, val) {
	 jQuery.data(jQuery(elementSelector).get(0), varName, val);
 }

 function getData(elementSelector, varName) {
	 var val = jQuery.data(jQuery(elementSelector).get(0), varName);
	 return val;
 }

