/*
 * Project: Sunward Resources Ltd.
 * Author: Rudy Affandi
 * Created: 08/04/2011
 * File name: functions.js
 */

// Basic settings
var companyName = 'Sunward Resources Ltd.';

// Main navigation drop down, 3rd level settings (please enter 'yes' or 'no')
var allow_main_3rd_level = 'no';

// 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);

// Cufon usage in main navigation
var cufon_nav = 'yes';

// jQuery initialization and CSS settings, waiting for DOM tree to initialize
$(document).ready(function(){
	// Preload images
	$.preLoadImages('/i/common/header_img.jpg', '/i/common/front_img1.jpg', '/i/common/front_img2.jpg', '/i/common/front_img3.jpg', '/i/common/front_img4.jpg');

	// Initialize Cufon
	Cufon.set('fontFamily', 'TradeGothic');
		Cufon.replace('.front_text h1', { hover: true });
		Cufon.replace('.front_content h2', { hover: true });
		Cufon.replace('.front_content h3', { hover: true });
		Cufon.replace('a.button', { hover: true });
		Cufon.replace('.content_header h2', { hover: true });
		Cufon.replace('.content_body h2', { hover: true });
		Cufon.replace('.side_news h2', { hover: true });
		Cufon.replace('.header_stock ul li', { hover: true });
		Cufon.replace('.dropdown > li > a', { hover: true });
	$('.cufonized').removeClass('cufonized');
	Cufon.refresh();

   // Home page slideshow
   var path = $(location).attr('href');
   var filename = path.match(/.*\/([^/]+)\.([^?]+)/i)[1];
   filename = filename.toLowerCase();
   if (filename == 'home')
   {
		$('#slider').nivoSlider({
   		effect: 'fade',
         captionOpacity:0.8 // Universal caption opacity
		});
   }
   
   if (filename == 'hometest')
   {
		$('#slider').nivoSlider({
   		effect: 'fade',
         captionOpacity:0.8 // Universal caption opacity
		});
   }   

	// Photo Gallery script (using Gallerific)
	if	( $('#slideshow').length > 0 )
	{
		var slide_height;
		var nav_pos;
		$('#thumbs').galleriffic({
			delay:                     5000, // in milliseconds
			numThumbs:                 6, // The number of thumbnails to show page
			preloadAhead:              10, // Set to -1 to preload all images
			enableTopPager:            true,
			enableBottomPager:         false,
			maxPagesToShow:            10,  // The maximum number of pages to display in either the top or bottom pager
			imageContainerSel:         '#slideshow', // The CSS selector for the element within which the main slideshow image should be rendered
			controlsContainerSel:      '#controls', // The CSS selector for the element within which the slideshow controls should be rendered
			captionContainerSel:       '#caption', // The CSS selector for the element within which the captions should be rendered
			loadingContainerSel:       '#loading', // The CSS selector for the element within which should be shown when an image is loading
			renderSSControls:          false, // Specifies whether the slideshow's Play and Pause links should be rendered
			renderNavControls:         true, // Specifies whether the slideshow's Next and Previous links should be rendered
			playLinkText:              'Play',
			pauseLinkText:             'Pause',
			prevLinkText:              '',
			nextLinkText:              '',
			nextPageLinkText:          'Next &rsaquo;',
			prevPageLinkText:          '&lsaquo; Prev',
			enableHistory:             false, // Specifies whether the url's hash and the browser's history cache should update when the current slideshow image changes
			enableKeyboardNavigation:  true, // Specifies whether keyboard navigation is enabled
			autoStart:                 false, // Specifies whether the slideshow should be playing or paused when the page first loads
			syncTransitions:           false, // Specifies whether the out and in transitions occur simultaneously or distinctly
			defaultTransitionDuration: 1000, // If using the default transitions, specifies the duration of the transitions
			onSlideChange:             undefined, // accepts a delegate like such: function(prevIndex, nextIndex) { ... }
			onTransitionOut:           undefined, // accepts a delegate like such: function(slide, caption, isSync, callback) { ... }
			onTransitionIn:            undefined, // accepts a delegate like such: function(slide, caption, isSync) { ... }
			onPageTransitionOut:       undefined, // accepts a delegate like such: function(callback) { ... }
			onPageTransitionIn:        undefined, // accepts a delegate like such: function() { ... }
			onImageAdded:              undefined, // accepts a delegate like such: function(imageData, $li) { ... }
			onImageRemoved:            undefined  // accepts a delegate like such: function(imageData, $li) { ... }
		});
		$('a.pause, a.play, a.prev, a.next').wrapInner('<span class="text">');
	}

   // Projects map tooltip
   $('#map area').each(function(){
      $(this).qtip(
      {
         content: $(this).attr('alt'), // Use the ALT attribute of the area map
         style: {
            color: '#000',
            background: '#ffffff',
            border: {
               width: 0,
               radius: 4,
               color: '#ef5f08'
            },
            tip: true // Apply a tip at the default tooltip corner
         },
         position: { corner: { target: 'topLeft', tooltip: 'leftMiddle' } },
         hide: { when: 'mouseout', fixed: true }
      });
   });

	// Tabbed interface for Stock Quote & Chart
   $('ul.tabs').tabs('div.panes > div', { effect: 'fade' });

	// Image thumbnail processing
	$('img').load(function(){
		 var $this = $(this),
			  img_width = $this.width();
			  img_height = $this.height();
		 $this.closest('.img_thumb').css('width', img_width + 'px');
		 $this.closest('ul.image_grid_format li').css('width', img_width + 'px');
	});

	// Add back to top icon
	$('.top a').append('<div class="top_arrow">');

	// Round the button
	$('.front_text a.button').addClass('ui-corner-all');

	// Fade in front image
	$('.front_content_header_bg').delay(500).fadeTo('slow', 1);

   // Set language information and retrieve associated content
   if (lang_selector == 'de'){
      $('html').attr('lang', 'de');
      $('.header_lang a[href*="/de/"]').addClass('active');
      // Translate elements to German
   }
   else {
      $('html').attr('lang', 'en');
      $('.header_lang a[href*="/s/"]').addClass('active');
   }

	// Flush 1st occurence of h tag margin
   $('.content_body h2:first').css('margin-top', '0');

   // Add clear break
   $('.ar_format:nth-child(6n)').addClass('clear');

	// Financials, check if PDF exist
	$('.fs_format div.link a').each(function() {
		if	( $(this).attr('href').length <= 0 )
		{
			$(this).hide();
		}
	});

	// Fix Cufon :hover sticky issue
	$('#nav li a').each(function() {
		$(this).hover(
			function(){},
			function(){ Cufon.refresh();}
		);
	});

   // Photo gallery settings (Grid format)
 	$('ul.image_grid_format li a[href*="_sm"]').each(function(){
 	   var href = $(this).attr('href').replace('_sm', '');
 	   $(this).attr('href', href);
   });

});

