/*
 * Project: Cascadero Copper Corporation
 * Author: Rudy Affandi
 * Created: 10/26/2011
 * File name: functions.js
 */

// Basic settings
var companyName = 'Cascadero Copper Corporation';

// Main navigation drop down, 3rd level settings (please enter 'yes' or 'no')
var allow_main_3rd_level = 'yes';

// Side navigation drop down, 3rd level settings (please enter 'yes' or 'no')
var allow_side_3rd_level = 'yes';

// Grab language selector from scripts alias
$.getScript('/cc/lib/jquery/plugins/jquery.url.js',function(){
	var lang_selector = jQuery.url.segment(0);
});

// Cufon usage in main navigation
var cufon_nav = 'no';

// jQuery initialization and CSS settings, waiting for DOM tree to initialize
$(document).ready(function(){

   // Set language information, default to English
   $('html').attr('lang', 'en');

	// Preload images from CSS
	$.getScript('/cc/lib/jquery/plugins/preloadCssImages.jQuery_v5.js',function(){
		$.preloadCssImages();
	});

	// prettyPhoto set up
	if ($("a[rel^='prettyPhoto']").length > 0){
		$.getScript('/cc/lib/jquery/plugins/prettyphoto/jquery.prettyPhoto.js',function(){
			$("a[rel^='prettyPhoto']").prettyPhoto();
		});
	}

	// Shadowbox set up
	if ($("a[rel^='shadowbox']").length > 0){
		$.getScript('/cc/lib/shadowbox/v3/shadowbox.js',function(){
			Shadowbox.init();
		});
	}
	
   // Photo gallery settings (Grid format)
 	$('.image_grid_format ul li a[href*="_sm"]').each(function(){
 	   var image_grid_path = $(this).attr('href').replace('_sm', '');
 	   $(this).attr('href', image_grid_path);
   });	

	// Set h tag margin
   $('.content_body h2:first, .content_body h3:first').css('margin-top', '0');

   // Detect first word of page header
   $('.content_header h2').each(function(){
      var me = $(this);
      me.html(me.html().replace(/^(\w+)/, '<span class="copper">$1</span>'));
   });

	// Set left & right background height
	var content_height = $('.content').height();
	$('.left_bg, .right_bg').css('height', content_height + 'px');

   // Fix Cufon refresh issue with navigation colour
   if (cufon_nav == 'yes')
   {
		$('.dropdown li a').ready(function(){
			Cufon.refresh();
		});

		$('.dropdown li').mouseout(function(){
			Cufon.refresh();
		});
   }

   // Form
   var default_form_value = $('input.field').val();
   $('input.field').val(default_form_value).focus(function() {
      if ($(this).val() == default_form_value)
      {
         $(this).val('')
      }
   });

   $('input.field').blur(function() {
      if ($(this).val() == '')
      {
         $(this).val(default_form_value);
      }
   });

	// Tooltips
	if ($('.front_map area').length > 0){
		$.getScript('/cc/lib/jquery/plugins/jquery.qtip-1.0.0-rc3.min.js',function(){
			// Features tooltips
			$('.front_features a').each(function(){
				$(this).qtip(
				{
					content: $(this).attr('caption'),
					style: {
						color: '#000',
						background: '#fff',
						border: {
							width: 0,
							radius: 4,
							color: '#fff'
						},
						tip: true // Apply a tip at the default tooltip corner
					},
               position: { corner: { target: $(this).attr('box'), tooltip: $(this).attr('tip') } },
					hide: { when: 'mouseout', fixed: true }
				});
			});

			// Maps tooltips
			$('.front_map area').each(function(){
				$(this).qtip(
				{
					content: $(this).attr('alt'),
					style: {
						color: '#000',
						background: '#fff',
						border: {
							width: 0,
							radius: 6,
							color: '#fff'
						},
						tip: true // Apply a tip at the default tooltip corner
					},
               position: { corner: { target: $(this).attr('box'), tooltip: $(this).attr('tip') } },
					hide: { when: 'mouseout', fixed: true }
				});
			});
		});
   }

function megaHoverOver(){
		$(this).find(".sub").stop().fadeTo('fast', 1).show();

		//Calculate width of all ul's
		(function($) {
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function() {
					rowWidth += $(this).width();
				});
			};
		})(jQuery);

		if ( $(this).find(".row").length > 0 ) { //If row exists...
			var biggestRow = 0;
			//Calculate each row
			$(this).find(".row").each(function() {
				$(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$(this).find(".sub").css({'width' :biggestRow});
			$(this).find(".row:last").css({'margin':'0'});

		} else { //If row does not exist...

			$(this).calcSubWidth();
			//Set Width
			$(this).find(".sub").css({'width' : rowWidth});

		}
	}

	function megaHoverOut(){
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() {
		  $(this).hide();
	  });
	}

	var config = {
		 sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)
		 interval: 100, // number = milliseconds for onMouseOver polling interval
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
		 timeout: 100, // number = milliseconds delay before onMouseOut
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)
	};

	$("ul.dropdown li .sub").css({'opacity':'0'});
	$.getScript('/cc/lib/jquery/plugins/jquery.hoverIntent.minified.js',function (){
   	$("ul.dropdown li").hoverIntent(config);
	});
});
