/*
 * Project: Rio Cristal Resources Corp.
 * Author: Rudy Affandi
 * Created: 05/09/2011
 * File name: functions.js
 */

// Basic settings
var companyName = 'Rio Cristal Resources Corp.';

// 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
var lang_selector = jQuery.url.segment(0);

// jQuery initialization and CSS settings, waiting for DOM tree to initialize
$(document).ready(function(){

   // Set language information and retrieve associated content
   if (lang_selector == 'esp'){
      $('html').attr('lang', 'es');
      // Translate elements to Spanish
      $('.footer').translate('spanish');
   }
   else {
      $('html').attr('lang', 'en');
   }

   $('a.cn_box').colorbox();

   // Photo gallery settings
   // add Gallery title from REL tag
	var galleryTitle = $('.items span.gallerytitle a').attr('rel');
   $('h3.gallery_title').text(galleryTitle);

   // Divide list of thumbs into 5 (or any arbitrary number set in the variable
   var itemindex = 0;
   var maxThumbs = 5;
   var Jlistobj = null;
   $('span.biglist img').each(function(){
      if (itemindex % maxThumbs == 0)
      {
         Jlistobj = $("<div></div>");
      }
      Jlistobj.append($(this));
      $('.items').append(Jlistobj);
      itemindex++;
   });
   $('span.biglist').remove();
   $('.scrollable').scrollable();
   $('#image_wrap, .scrollable, .scrollable img').addClass('ui-corner-all');
   $('.items img').click(function() {

	// see if same thumb is being clicked
	if ($(this).hasClass('active')) { return; }

	// Get the large image filename from thumbnail
	var url = $(this).attr('src').replace('_sm', '');

   // add caption from TITLE tag
	var caption = $(this).attr('title');
   $('div.gallery_format h4').text(caption);
   
	// get handle to element that wraps the image and make it semi-transparent
	var wrap = $('#image_wrap').fadeTo('medium', 0.5);

	// the large image from CMS
	var img = new Image();

   	// call this function after it's loaded
	img.onload = function() {

		// make wrapper fully visible
		wrap.fadeTo('fast', 1);

		// change the image
		wrap.find('img').attr('src', url);
	};

	// begin loading the image from source folder/path
	img.src = url;

	// activate item
	$('.items img').removeClass('active');
	$(this).addClass('active');

   // when page loads simulate a "click" on the first image
   }).filter(':first').click();
});

// Open Chinese content from language selector
function openShadowbox(content, player, height, width) {
// open a welcome message as soon as the window loads
    Shadowbox.open({
        content: content,
        player:  player,
        height:  height,
        width:   width
    });
}

