function full_screen(){
	window.moveTo(0,0);
	window.resizeTo(screen.width,screen.height);
	return;
}
function email_page(p){
	loadTB("Email this page to a friend", g_webroot + "read/email_friend/"+g_type+"/"+g_id+"/"+p+"/?width=650&height=420")
}
function download_pdf(){
	g_loading = false;
	if(g_previewEnabled && g_showLogin)
		alert("Sorry, you must have access to the full publication in order to download.");
	else
		loadTB("Download for offline reading", g_webroot + "issues/download/"+g_type+"/"+g_id+"/?width=380&height=200")
}
function archives(p){
	loadTB("Other issues of " + g_titleName , g_webroot + "read/archives/"+g_type+"/"+g_titleId+"/"+g_id+"/"+g_fb_version+"/?width=400&height=350")
}
function show_help(){
	loadTB("Help with the reader", g_webroot + "pages/flipbook_help_" + g_fb_version + "/?width=600&height=500");
}
function print_page(p){
	loadTB("Print this page", g_webroot + "read/print_page/"+g_type+"/"+g_id+"/"+p+"/"+g_totalPages+"/?width=550&height=250")
}

function social_bookmark(p){
	loadTB("Social bookmark this page", g_webroot + "read/social_bookmark/"+g_type+"/"+g_id+"/"+p+"/?width=550&height=260")
}

var g_selectiveDownloadIntervalID = null;
function select_download(pages, unused_value){
	//if another download has been triggered clear it off
	if(g_selectiveDownloadIntervalID)
		clearInterval(g_selectiveDownloadIntervalID);

	checkSelectiveDownload(pages);

	//check very 5 seconds to see the file has finished or not
	g_selectiveDownloadIntervalID = setInterval( "checkSelectiveDownload()", 5000);	
}

function checkSelectiveDownload(pages){
	if(!pages) pages = null;

	$j.ajax({
		type    : "POST",
		url     : g_webroot + "read/ajax_checkSelectiveDownload",
		data    : "data="+JSON.stringify({"pages":pages, "type":g_type, "id":g_id}),
		success : function(data){
			//if the file is finished then call the download function in the flipbook_util_frame
			if(data=="DONE"){
				clearInterval(g_selectiveDownloadIntervalID);
                $j('#flipbook_util_frame').attr("src",  g_webroot+ "/read/download_selected_pages/");

			} else if(data == "FAILED" ){
				clearInterval(g_selectiveDownloadIntervalID);
				alert("ERROR: download failed");

			} else if(data == "STARTED"){
				//alert("Download started");
				return;
			}
		}
	});
}

function addClipping(clippingId, zoom_level){
	
	if( empty.test(clippingId) ){
		return;
	}
	
	$j.ajax({
		type: "POST",
		data: "data="+JSON.stringify({"issue_id":g_id,"clipping_id":clippingId}),
		url: g_webroot + "read/ajax_clippingBasketSave",
		success: function(data){
			if( data == 1 ){
				
				if(typeof refreshSideBarClippings == 'function') {
					refreshSideBarClippings();
				} else {
					alert("Your clipping has been saved to your basket successfully");
				}
				
			} else if( data == 0 ){
				alert("Due to a system error we could not process your request. Please try again later");
			} else if( data == 'DUPLICATE' ) {
				alert("Your clipping basket already contains this clipping.");
			} else {
				alert("Unknown Error. Please try again later.");
			}
		}
	});
	
	
}

function printClipping(clippingId, zoom_level){
	//alert("printing");
	loadTB("Printing...", g_webroot + "read/ajax_printClipping/" + clippingId + "/?width=350&height=180");
}

function show_clippings(){
	//alert("show clippings");
	loadTB("Your clipping basket", g_webroot+"read/ajax_clippingBasketContent/?width=400&height=520");
}

function newSubscriber(){
	loadTB("Signup for Email Alerts", g_webroot+"subscribers/add/2/"+g_titleId+"/0/flipbook/?width=450&height=470");
}
function subscriberMyAccount(tab){
	if(typeof tab == "undefined") tab = "my_account";
	loadTB("My Account ("+g_sid+")", g_webroot+"subscribers/"+tab+"/"+g_titleId+"/"+g_sid+"/?width=500&height=470");
}

//--------------------------
// from read page
//--------------------------
$j(document).ready(function(){
	//authentication
	if(g_showLogin) loadAuthentication();	

	if(!g_showLogin || g_previewEnabled){	
		//if tpi
		if(g_issueType == "T"){
			
			resizeTpiFrame();
			tb_register_callback('onload', 'hideTpi');
			tb_register_callback('onremove', 'showTpi');
			onresize = resizeTpiFrame;
		
		} else {
			//do not load any of these code if the subscription has expired
			if(typeof g_subscriptionExpired == "undefined" || (typeof g_subscriptionExpired != "undefined" && !g_subscriptionExpired) ){
				//if flash detected and g_loading show 'loading' (if version 2)
				var playerVersion = swfobject.getFlashPlayerVersion();
				if(playerVersion.major > 7 && g_loading) loadLoader();
				$j("#readMagazine").show();
				
				//figure out flash version required
				var fb_flash_version = "9.0.0";
				if(g_fb_version==3)
					fb_flash_version = "10.0.0";
					
				swfobject.embedSWF(g_files_url + "/vendors/flipbook/flipbook" + g_fb_version + ".swf?v=" + g_fb_revision, "readMagazine", "100%", "100%", fb_flash_version, g_files_url + "/vendors/expressinstall/expressInstall.swf", flashvars, params, attributes);
				
				//if loading takes too long, close
				if(g_loading)
					var t = setTimeout("shutdownLoader()", 25000);
					
				positionScreen();
				
				//safari hack
				if($j.browser.safari){
					tb_register_callback('onload', 'hideFlipbook');
				}
			} else {
				subscriberMyAccount("update_subscription");
			}
		}
	}

});

function shutdownLoader(){
	if(g_loading){
		g_loading=false;
		tb_remove();
	}
}

function loadAuthentication(){
	loadTB("Login", "#TB_inline?height=350&width=400&inlineId=login_form&modal=true");
}

//general funcs
function loadLoader(){
	var h = 180;
	if(g_type=="compilation")
		h = 40;
	loadTB("loading", "#TB_inline?height=" + h + "&width=420&inlineId=read_loading&modal=true");
}

function closeLoader(){
	t=setTimeout("tb_remove()", 1500);
	g_loading = false;
}
function positionScreen(){

	//get screen dimensions
	var sWidth = parseInt($j(window).width());
	var sHeight = parseInt($j(window).height());
	
	$j("#readMagazine_div").css("top", "0px");
	$j("#readMagazine_div").css("left", g_show_left + "px");
	$j("#readMagazine_div").css("height", sHeight + "px");
	$j("#readMagazine_div").css("width", (sWidth-(g_show_left + g_show_right)) + "px");
	
	if(g_show_left){
		$j("#left_div").css("height", (sHeight) + "px").show();
		$j("#left_content_div").css("height", (sHeight-56) + "px").show();
	}
	
	if(g_show_right){
		$j("#right_div").css("height", (sHeight) + "px").css("left", (sWidth-g_show_right) + "px").show();
		$j("#right_content_div").css("height", (sHeight-56) + "px").show();
	}
	
}
//safari hack continued..
function hideFlipbook(){
	$j("#readMagazine_div").css("top", "9000px");
}
function executeOnThickboxClose(){
	positionScreen();
}
					
//TPI functions
function resizeTpiFrame(){
	$j("#tpi_frame").css("height", ($j(window).height() - 34) + "px");
	$j("#tpi_frame").css("width", $j(window).width() + "px");
}
function hideTpi(){
	//alert("hiding");
	$j("#tpi_frame").css("height", "1px");
	$j("#tpi_frame").css("width", "1px");
}
function showTpi(){
	//alert("showing");
	resizeTpiFrame();
}
