
// CUFON
Cufon.replace('h1', {
	textShadow: '#07252D 1px 1px',
	hover: true
});

Cufon.replace('h2', {
	textShadow: '#07252D 1px 1px',
	hover: true
});

Cufon.now();


// DEEPLINKING
function checkDeepLink() {
	var l=window.location;
	var condition_met = false;
	if((l.hash == "") || (l.hash == null)) 	 {condition_met = true; setiPhone('german');setVideoPanel('german'); makeTabs(0);}
	if(l.hash.match(/^#\/german\/?$/)!=null) {condition_met = true; setiPhone('german');setVideoPanel('german');changeBrowserTitle('german'); changeURLAddress('german'); makeTabs(0);}
	if(l.hash.match(/^#\/french\/?$/)!=null) {condition_met = true; setiPhone('french');setVideoPanel('french');changeBrowserTitle('french'); changeURLAddress('french'); makeTabs(1);}
	if(l.hash.match(/^#\/italian\/?$/)!=null){condition_met = true; setiPhone('italian');setVideoPanel('italian');changeBrowserTitle('italian'); changeURLAddress('italian'); makeTabs(2);}
	if(l.hash.match(/^#\/spanish\/?$/)!=null){condition_met = true; setiPhone('spanish');setVideoPanel('spanish');changeBrowserTitle('spanish'); changeURLAddress('spanish'); makeTabs(3);}
	
	if(condition_met == false) { setiPhone('german');setVideoPanel('german'); makeTabs(0); }
}

function setiPhone(lang) {
	changeiPhoneLangScreens('#iphone-viewer','html/iphone-' + lang + '.html');
}

function setVideoPanel(lang) {
	//changeVideoPanel('#video-panel','html/video-panel-' + lang + '.html');
}

function setUpVideo() {
	$("a[rel^='prettyPhoto']").prettyPhoto({theme:'light_rounded'});
}

function resumeSlideshow() {
	$('#slideshow').cycle('resume');
}

function pauseSlideshow() {
	$('#slideshow').cycle('pause');
}

// AJAX LOADING
function loadContent(elementSelector,sourceUrl) {
	
	$(""+elementSelector+"").load(sourceUrl, function() {
  		//alert('This is a callback.');
  		//setUpVideo();
	});
}


// MISC FUNCTIONS
function changeiPhoneLangScreens(elementSelector, sourceUrl) {
	$('#slideshow').cycle('stop');
	loadContent(elementSelector, sourceUrl);
}

function changeVideoPanel(elementSelector, sourceUrl) {
	loadContent(elementSelector, sourceUrl);
}

function changeBrowserTitle(lang) {
	document.title = 'Michel Thomas Apps for iPhone – ' + ucfirst(lang);
}

function changeURLAddress(lang) {
	window.location = 'http://www.michelthomasapp.com/#/' + lang;
}

function getLang(idx) {
	var langs = new Array('german','french','italian','spanish');
	return langs[idx];
}

function ucfirst(str) {
    var firstLetter = str.substr(0, 1);
    return firstLetter.toUpperCase() + str.substr(1);
}

// TABS
function makeTabs(firstSlide) {
	
	$(function() {
	    $('#tabs-content').before('<ul id="tabs-nav">').cycle({
	        fx:     'fade',
	        speed:	250,
	        timeout: 0,
	        startingSlide: firstSlide,
	        pager:  '#tabs-nav',
	        pagerClick: function(idx, slide) {
	        	lang = getLang(idx);
	          	changeiPhoneLangScreens('#iphone-viewer','html/iphone-' + lang + '.html');
	          	//changeVideoPanel('#video-panel','html/video-panel-' + lang + '.html');
	          	changeBrowserTitle(lang);
	          	changeURLAddress(lang);
	        },
	        pagerAnchorBuilder: function(idx, slide) {
	            var thumbsrc = 'assets/images/tabs/0' + (idx+1) + '-tab.png';
	            return '<li><a href="#"><img src="' + thumbsrc + '" width="153" height="46" /></a></li>';
	        }
	    });
	});
}


// SMOOTH SCROLLING
// Thanks to: http://www.position-absolute.com/articles/better-html-anchor-a-jquery-script-to-slide-the-scrollbar/
jQuery.fn.anchorAnimate = function(settings) {

 	settings = jQuery.extend({
		speed : 1200
	}, settings);	

	return this.each(function(){
		var caller = this
		$(caller).click(function (event) {	
			event.preventDefault()
			var locationHref = window.location.href
			var elementClick = $(caller).attr("href")

			var destination = $(elementClick).offset().top;
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, settings.speed, function() {
				//window.location.hash = elementClick
			});
		  	return false;
		})
	})
}

