var progress = 0;
var iOS = false;
var showing = "gle1";

function mp_pad(s,l) { return( l.substr(0, (l.length - s.length)) + s ); }

function mp_a(){
	$('cover').appear({ duration: .5 });
	new Effect.Move('av', { x: 164, y: -650, mode: 'absolute', queue: 'end' });
	$('ainfo').appear({ duration: .5, queue: 'end' });
	if(!iOS) setTimeout(mp_p, 1500);
	_gaq.push(['_trackPageview', "/main/apple-of-my-eye/play"]);
	showing = "apple";
}
function mp_p(){
	$('moogaloop').api_setVolume(80);
	$('moogaloop').api_play();
}
function mp_p2(){
	$('moogaloop2').api_setVolume(80);
	$('moogaloop2').api_play();
}
function mp_ac(){
	if(!iOS) $('moogaloop').api_pause();
	if(!iOS) $('moogaloop').api_unload();
	new Effect.Move('av', { x: 164, y: -1250, mode: 'absolute' });
	$('cover').fade({ duration: .5 });
	$('ainfo').fade({ duration: .5 });
	if(!iOS) _gaq.push(['_trackPageview', "/main/apple-of-my-eye/close/" + mp_pad(progress,"000")]);
}

function mp_glc(){
	if(!iOS) $('moogaloop2').api_pause();
	if(!iOS) $('moogaloop2').api_unload();
	new Effect.Move('glv', { x: 164, y: -1250, mode: 'absolute' });
	$('cover').fade({ duration: .5 });
	$('glinfo').fade({ duration: .5 });
	if(!iOS) _gaq.push(['_trackPageview', "/main/gle1/close/" + mp_pad(progress,"000")]);
}

function vimeo_player_loaded() {
	moogaloop = document.getElementById('moogaloop');
	moogaloop.api_addEventListener('onProgress', 'vimeo_on_progress');
	moogaloop.api_addEventListener('onFinish', 'vimeo_on_finish');
}
function vimeo_player_loaded2() {
	moogaloop = document.getElementById('moogaloop2');
	moogaloop.api_addEventListener('onProgress', 'vimeo_on_progress2');
	moogaloop.api_addEventListener('onFinish', 'vimeo_on_finish2');
	setTimeout(mp_p2, 1500);
}

function vimeo_on_progress(time, swf_id) {
	var prev = progress;
	progress = Math.ceil(time);
	if(progress % 5 == 0 && progress > 0 && prev != progress){
		_gaq.push(['_trackPageview', "/main/apple-of-my-eye/progress/" + mp_pad(progress,"000")]);
	}
}

function vimeo_on_finish(swf_id) {
	_gaq.push(['_trackPageview', "/main/apple-of-my-eye/complete"]);
}
function vimeo_on_progress2(time, swf_id) {
	var prev = progress;
	progress = Math.ceil(time);
	if(progress % 5 == 0 && progress > 0 && prev != progress){
		_gaq.push(['_trackPageview', "/main/gle1/progress/" + mp_pad(progress,"000")]);
	}
}

function vimeo_on_finish2(swf_id) {
	_gaq.push(['_trackPageview', "/main/gle1/complete"]);
}

function mp_b(event){
	if(event) pos_y = event.offsetY?(event.offsetY):event.pageY-document.getElementById("b").offsetTop;
	
	if(!pos_y || pos_y < 166){
		// $('cover').appear({ duration: .5 });
		// new Effect.Move('glv', { x: 164, y: -650, mode: 'absolute', queue: 'end' });
		// $('glinfo').appear({ duration: .5, queue: 'end' });
		// if(!iOS) setTimeout(mp_p2, 1500);
		// 
		// // $('txtc').hide();
		// // $('txtb').show();
		// // $('txtd').hide();
		// // $('mimg').setStyle({backgroundImage:'url(/images/mframe.png)'});
		// // $('cover').appear({ duration: .5 });
		// // $('minfo').appear({ duration: .5 });
		// _gaq.push(['_trackPageview', "/main/gle1/click"]);
		// showing = "gle1";
		location.href = "http://www.goldilocksmobileseries.com";
	} else {
		_gaq.push(['_trackPageview', "/main/app/gle1"]);
		location.href = "http://itunes.apple.com/us/app/majek-pictures/id395176643?mt=8";
	}
}

function banner_click(){
	_gaq.push(['_trackPageview', "/main/app/gle1"]);
	location.href = "http://itunes.apple.com/us/app/majek-pictures/id395176643?mt=8";
	return false;
}

function mp_c(){
	$('txtc').show();
	$('txtb').hide();
	$('txtd').hide();
	$('mimg').setStyle({backgroundImage:'url(/images/mframe.png)'});
	$('cover').appear({ duration: .5 });
	$('minfo').appear({ duration: .5 });
	_gaq.push(['_trackPageview', "/main/picture-this/click"]);
	showing = "majek";
}

function mp_d(){
	$('txtc').hide();
	$('txtb').hide();
	$('txtd').show();
	$('mimg').setStyle({backgroundImage:'url(/images/mframe.png)'});
	$('cover').appear({ duration: .5 });
	$('minfo').appear({ duration: .5 });
	_gaq.push(['_trackPageview', "/main/majek/click"]);
	showing = "majek";
}

function mp_dc(){
	$('cover').fade({ duration: .5 });
	$('minfo').fade({ duration: .5 });
}

var flashvars = {
    clip_id: 12819723,
    show_portrait: 0,
    show_byline: 0,
    show_title: 0,
	js_api: 1, // required in order to use the Javascript API
	js_onLoad: 'vimeo_player_loaded', // moogaloop will call this JS function when it's done loading (optional)
	js_swf_id: 'moogaloop' // this will be passed into all event methods so you can keep track of multiple moogaloops (optional)
};
var flashvars2 = {
    clip_id: 18957251,
    show_portrait: 0,
    show_byline: 0,
    show_title: 0,
	js_api: 1, // required in order to use the Javascript API
	js_onLoad: 'vimeo_player_loaded2', // moogaloop will call this JS function when it's done loading (optional)
	js_swf_id: 'moogaloop' // this will be passed into all event methods so you can keep track of multiple moogaloops (optional)
};
var params = {
	allowscriptaccess: 'always',
	allowfullscreen: 'true'
};
var attributes = {};


function detect(){
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) { 
		iOS = true;
		$('moogaloop2').update("<video src='http://www.vimeo.com/play_redirect?clip_id=12819723' controls='controls' width='524' height='295'></video>");
		$('moogaloop').update("<video src='http://www.vimeo.com/play_redirect?clip_id=12819723' controls='controls' width='524' height='295'></video>");
		$('a').setStyle({left:'166px'});
		$('b').setStyle({left:'398px'});
		$('c').setStyle({left:'699px'});
		$('d').setStyle({left:'201px'});
	} else {
		swfobject.embedSWF("http://vimeo.com/moogaloop.swf", 'moogaloop2', "524", "295", "9.0.0","expressInstall.swf", flashvars2, params, attributes);
		swfobject.embedSWF("http://vimeo.com/moogaloop.swf", 'moogaloop', "524", "295", "9.0.0","expressInstall.swf", flashvars, params, attributes);
	}
}

function cover(){
	switch(showing){
		case "goldilocks":
			$('goldilocks').fade();
			$('cover').fade();
		break;

		case "gle1":
			mp_glc();
		break;

		case "apple":
			mp_ac();
		break;
		
		case "majek":
			mp_dc();
		break;
	}
	showing = "";
}

function setCookie(c_name, value, expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires=" + exdate.toUTCString());
}

function getCookie(c_name) {
	if (document.cookie.length > 0){
		c_start = document.cookie.indexOf(c_name + "=");
		if (c_start != -1) {
			c_start = c_start + c_name.length + 1;
			c_end = document.cookie.indexOf(";",c_start);
			if (c_end == -1) c_end = document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}
