$(document).ready(function(){
	var PAGE_NAME = $('body').attr('id');
	initGlobalNav();
	switch (PAGE_NAME){
		case 'index':
			initSifr();
			break;
		case 'products':
			initSifr();
			initProduct();
			break;
		case 'real-stories':
			initScrollable();
			if (getQueryParam('section') == 'videos')
				initVideos();
			else
				initRealStories();
			break;
		case 'about-us':
			if (getQueryParam('section') != 'news')
				initAboutUs();
			break;
		case 'worldwide':
			
			break;
		case 'order':
			
			break;
		case 'contact':
			
			break;
	}
});
var ltie7 = $.browser.msie && $.browser.version < 7;


function initGlobalNav(){
  $('#nav-main > li').after('<li><img src="/img/ui/nav-div.png" alt="" width="1" height="33" /></li>');
  $('#nav-main > li:first').before('<li><img src="/img/ui/nav-div.png" alt="" width="1" height="33" /></li>');
	$('#drop-products').bgiframe();
  $('#nav-products').hoverIntent(
    function(){
			$('#drop-products').show();
    },
    function(){
      $('#drop-products').hide();
    }
  );
}

function initScrollable(){
	$(".scrollable").scrollable({size:3,horizontal:true});
}

function initRealStories(){
	$('#scroll-images .items img').click(function(e){
		$('#scroll-images .items img').removeClass('active');
		$(this).addClass('active');
		$('#preview-images').attr('href', this.src.replace(/thumb/, 'full')).find('img').attr('src', this.src.replace(/thumb/, 'preview'));
	});
	$('#images-jump').change(function(e){
		$('#scroll-images').scrollable('seekTo', parseInt(this.value));
		var selector = parseInt(this.value) == parseFloat(this.value) ? ':first' : ':last';
		$('#scroll-images .items li').eq(parseInt(this.value)).children(selector).trigger('click');
	});
	$('#preview-images').click(function(){
		$('#pop-images img').attr('src', this.href);
		$('#pop-images').show();
		return false;
	});
	$('#pop-images').click(function(){
		$('#pop-images').hide();
		return false;
	});
}

function initVideos(){
	var videoConfig = {
		videoFile: $('#scroll-videos .items a:first').attr('href'),
		autoPlay: false,
	  autoBuffering: true,
	  initialScale: 'scale',
		loop: false,
		controlBarGloss:'high',
		showMenu: false,
		showFullScreenButton: false,
	  usePlayOverlay: true,
		controlsOverVideo: 'ease',
		backgroundColor: 0xffffff,
	  timeDisplayFontColor: 0xbbbbbb
	  //controlBarBackgroundColor: 0xffffff,
		//splashImageFile: '/img/video-splash.jpg'
	}
	var htmlConfig = {
		src: '/swf/FlowPlayerLight.swf',
		height: '100%',
		width: '100%',
		wmode: 'transparent',
		flashvars: {
			config: videoConfig
		}
	}
	var flashConfig = {version: 8}
	//var videoPlayer = $('#player-video').flash(htmlConfig, flashConfig).children(':first').get(0);
	$('#player-video').flash(htmlConfig, flashConfig);
	$('#scroll-videos .items a').click(function(e){
		$('#scroll-videos .items img').removeClass('active');
		$(this).find('img').addClass('active');
		videoConfig.autoPlay = true;
		videoConfig.videoFile = this.href;
		$('#player-video').children(':first').get(0).setConfig(videoConfig);  
		return false;
	});
	return true;
}

function initAboutUs() {
	initScrollable();
	$('#scroll-photos .items a').click(function(){
		$('#pop-images img').attr('src', this.href);
		$('#pop-images').show();
		return false;
	});
	$('#pop-images').click(function(){
		$('#pop-images').hide();
		return false;
	});
}

function initProduct(){
	$('#thumb-product').click(function(){
		$('#pop-product img').attr('src', this.href);
		$('#pop-product').show();
		return false;
	});
	$('#pop-product').click(function(){
		$('#pop-product').hide();
		return false;
	});
}

function initSifr(){
	$('.sifr').each(function(){
		var flashvars = {
			css: "* { color: "+rgbToHex($(this).css('color'))+"; }", 
			txt: this.innerHTML
		};
		var params = {
			src: '/swf/century-gothic.swf',
			wmode: 'transparent',
			flashvars: flashvars
		};
		var plugin = {version: 7,update: false};
		$(this).flash(params, plugin, function(htmlOptions) {
			this.innerHTML = '<span class="alt">'+this.innerHTML+'</span>';
			var alt = $(this.firstChild);
			htmlOptions.height = alt.height();
			htmlOptions.width = alt.width();
			$(this).prepend($.fn.flash.transform(htmlOptions));				
		}).css('visibility', 'visible');
	});
}

/*
 * UTILITY FUNCTIONS
 */

// takes RGB string in format of "rgb(12, 34, 56)" and returns css hex equivalent "#00aa33", returns other formats as is
function rgbToHex(str){
	if (/^rgb\(/.test(str)){
		var val = '#', hex = "0123456789ABCDEF", rgb = str.replace(/rgb\(|\)/g, '').split(', ');
		for (i=0; i<rgb.length; i++){
			val += String(hex.charAt(Math.floor(rgb[i] / 16))) + String(hex.charAt(rgb[i] - (Math.floor(rgb[i] / 16) * 16)));
		}
		return val;
	} 
	return str;
}

// get querystring parameter value
function getQueryParam(key){
	var pairs = window.location.search.substring(1).split(/[&]/);
  for (var i=0, j=pairs.length; i<j; i++) {
    var n = pairs[i].split("=");
		if (key == unescape(n[0].replace(/^\s+|\s+$/g,''))){
			return unescape(n[1].replace(/\+/g, ' '));
		} else {
			return false;
		}
  }
}
