// Bindings
$(document).ready(function(){
	// Bind the menu links
	$('#Menu > a').click(changeDossier);
	
	
	// Init dialoges
	$('.dialog').dialog({
		autoOpen: false,
		width: 500,
		height: 500,
		modal: true,
		show: 'clip',
		hide: 'clip',
		dialogClass: 'notitle'
	});
	
	$('#movie_overlay').dialog({
		autoOpen: false,
		width: 640,
		height: 391,
		modal: true,
		resizable: false,
		show: 'clip',
		hide: 'clip',
		dialogClass: 'movie_dialog'
	});
	
	$('.dialog_closer').click(function(event) {
		$(event.target).parent().parent().dialog('close');
	});
	
	$('#close_dossier_button').html('&nbsp;');
	$('#close_dossier_button').click(toggleFlash); 

    $("#Wrapper a:not('.sbox'):not('[target]'):not('#nlsubmit')").not('[href^="http"]').click(function(event){
		event.preventDefault();
		toggleFlash(null, 'close', $(this).attr('href'));
	});
    
});

/*
function closeFlash(event) {
        var nexthref = this.href;
        
//        $("#Dossier").animate({height:200,opacity:'show' },'slow',function(){
//            location.href = nexthref;
//        });
        if ($('#Dossier').css('height') == '490px') {
            toggleFlash(event, 'close');
        }             
        
        return false;
}*/

function changeDossier(event, url) {
	// url is set from flash
	if (!url) {
		event.preventDefault();
		
		var target = event.target;
		var id = $('#Menu > a').index(target);
		url = $(target).attr("href");
		
		// Call Flash to open dossier by id
		getFlashMovie("DossierFlash").openDossier(id);
	}
	
	if (url.indexOf('/dossier') != -1) {
		// remove all "mark" classes and mark current
		$('#Menu > a').removeClass("mark");
		$('#Menu > a[href="' + url + '"]').addClass("mark");
		toggleFlash(null, 'open');
	} else {
		toggleFlash(null, 'close');
	}
	
	// load the url
	$("#Wrapper").load(url, function() {
	    var as = $('a.sbox');
    	if(as.length) { Shadowbox.setup(as); }
		
		$("#Wrapper a:not('.sbox'):not('[target]'):not('#nlsubmit')").not('[href^="http"]').click(function(event){
			event.preventDefault();
			toggleFlash(null, 'close', $(this).attr('href'));
		});
	});
	

	
	//console.log($(target).attr("href"), id);
}

function toggleFlash(event, cmd, url) {
	if (event) {
		event.preventDefault();
		if ($('#Dossier').css('height') == '200px') {
			cmd = 'open';
		} else {
			cmd = 'close';
		}
	}
	
	if (
		((cmd == 'close') && ($('#Dossier').css('height') == '200px'))
		|| ((cmd == 'open') && ($('#Dossier').css('height') == '490px'))
		) {
		check_navigate();
		return;
	}
	
	var targetHeigth;
	
	if (cmd == 'open') {
		targetHeigth = 490;
		$('#close_dossier_button').css('background-image', 'url(/media/img/design/up.png)');
		$('#close_dossier_button').attr('href', 'closeDossier/');
	} else if (cmd == 'close') {
		targetHeigth = 200;
		$('#close_dossier_button').css('background-image', 'url(/media/img/design/down.png)');
		$('#close_dossier_button').attr('href', 'openDossier/');
	}

	if ($.browser.mozilla) {
		// Mozilla Workaround because of flash reload
		$('#Dossier').css('height', targetHeigth + 'px');
		check_navigate();
	} else {
		// nicer with .switchClass('DossierOpen', 'DossierHidden'), but buggy in webkit in current version (jquery ui 1.7.2)
		$("#Dossier").animate({
			height: targetHeigth + 'px'
		}, 600, 'easeOutSine', check_navigate);
	}
	
	function check_navigate() {
		if (url) {
			window.location = url;
		}
	}
}


// Call from Flash
function openDossier(dossierPath) {
	changeDossier(new Object(), dossierPath);
}

function getFlashMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}

function startMovie(url, preview) {
	//alert(url);
	
	//console.log(url, preview);
	var flashvars = {};
	flashvars.image = preview;	
	
	flashvars.movie = url;
	//flashvars.movie = '/media/movies/URANSTORY-atomabfaelle.mp4';
	flashvars.autoplay = "true";			
	flashvars.loop = "false";
	
	flashvars.autohide = "false";
	
	flashvars.fullscreen = "true";
	
	flashvars.color_text = "0x9bf95d";
	flashvars.color_seekbar = "0x79b84d";
	flashvars.color_loadingbar = "0x828282";
	flashvars.color_seekbarbg = "0x333333";
	
	flashvars.color_button_out = "0x60a730";
	flashvars.color_button_over = "0x9bf95d";
	flashvars.color_button_highlight = "0xffffff";
	
	var params = {};
	params.allowfullscreen = "true";
	params.allowscriptaccess = "always";
	params.bgcolor = "#000000";
    params.wmode = "transparent";
	
	var attributes = {};
	attributes.align = "middle";
	
	swfobject.embedSWF("/media/swf/player.swf", "player_normal", "640", "391", "9.0.45", "", flashvars, params, attributes);
	$('#movie_overlay').dialog('open');
}

function share(type, url, title) {
	//console.log(url, type);
	var fqdnUrl = 'http://' + window.location.hostname + url;
    
	switch (type) {
		case 'twitter':
			window.open('http://twitter.com/home?status=URANSTORY: ' + title + ' ' + fqdnUrl);
			break;
		case 'facebook':
			//$("#fb_share_iframe").attr("src", 'http://www.facebook.com/sharer.php?u=' + fqdnUrl + 'film' + '&t=' + title); 
			//$("#fb_share").dialog('open');
            //$("#fbshare_button").attr('share_url', fqdnUrl + 'film');
            //$("#fbshare_button").click();
            share_url = 'http://www.facebook.com/sharer.php?u=' + fqdnUrl + 'film' + '&t=' + title;
            window.open( share_url, "Share Uranstory on Facebook", 
                "status = 1, height = 400, width = 500, resizable = 0" );

			break;
		case "embed":
			$('#embed_url').html(url);
			$('#embed_share').dialog('open');
	}
}

