/**
 * brainGuide.com
 *
 * (en) 410-publication-detail: Include/Init of all relevant JS Files Functions
 * (de) 410-publication-detail: Einf�gen/Initialisierung aller relevanten JS Dateien/Funktionen
 *
 * @copyright       Copyright 2008, Thomas Kalteis brainGuide AG
 * @version         1.0
 * @revision        $Revision: 10 $
 * @lastmodified    $Date: 2008-04-09
 */
 /****** 1 Global variables ******/
 /****** 2 Include needed JS files ******/
 /****** 3 Init all JS Datasets and Obersver ******/
 /****** 4 Footer init all widgets after page is loaded  ******/
 /****** 5 Page specific custom functions ******/
 
 
 
 
 /****** 1 Global variables ******/
	var publicationId;
	var tp_publication_detail;
	var tp_publication_detail_selected;
	var publicationsPage = 1;
	var publicationsPages;
	var intorductionToggle;
	var newsFade;
	var statusReviewFade;
	var helpSP;
	
	var tp_statistik;
	
	var clickHeatSite;
	var clickHeatGroup;
	var clickHeatServer;


 /****** 2 Include needed JS files ******/ 
/* (en) 410-publication-detail: Include needed JS Files */
/* (de) 410-publication-detail: Einf�gen der ben�tigte JS Dateien */





 /****** 3 Init all JS Datasets and Obersver ******/
/* (en) Calls the function to init all databindings */ 
/* (de) Ruft die Funktion zur Initalisierung der Datenverbindungen auf */

function initJS(id) {
	// initialize publication id
	publicationId = id;
	// init fade for status message block
	//statusReviewFade = new Spry.Effect.Fade('status-message-review',{duration: 1000, from: 0, to: 100});
}
	



 /****** 4 Footer init all widgets after page is loaded  ******/
function initFooter() {
	
/* (en) Systemnavigation: Init Layer System Navigation */
/* (de) Systemnavigation: Initialisierung der Systemnavigation  */	
	var sysnav = new Spry.Widget.MenuBar("sysnav");
	
	
/* (en) Help: init the help introduction panel */
/* (de) Hilfe: Initialisierung des Hilfe Panels */
	//var sp = new Spry.Widget.SlidingPanels("help");
	//newsFade = new Spry.Effect.Fade('news', {duration: 1500, from: 20, to: 100, toggle:true});
	//intorductionToggle = new Spry.Effect.Slide('intorductionToggle', {duration: 1000, from: '0px', to: '195px', toggle: true});
	
	sendToAFriendFade = new Spry.Effect.Fade('sendToAFriend', {toggle:true});
	helpSP = new Spry.Widget.SlidingPanels("help");
	
	
	/* Hides Static Lists */
	//Spry.Utils.addClassName('li_experts_static','hide');
	
	
/* (en) Toolbar: Spry widgets in #col1  */
/* (de) Toolbar: Initialisiert die Spry Widgets von #Col1 */
	
	if(document.getElementById('cp_publication_themes')) {
		var cp_publication_themes = new Spry.Widget.CollapsiblePanel("cp_publication_themes", {enableAnimation:false});
	}
	
	
/* (en) Content: Spry widgets in #col3  */
/* (de) Content: Initialisiert die Spry Widgets von #Col3 */

	
/* (en) Init of the heatmap */
/* (de) Initialisierung der Heatmap */	
	//try {configHeatMap();} catch(err) { }
	
	// check the anchors (var params defined in global.js)
	params = Spry.Utils.getLocationParamsAsObject();
	if(params.panel) {
		initPublicationNavigation(params.panel);
	}
	
	/* (en) Init validation of send to a friend form fields */
	/* (de) Initialisierung der Validierung f�r Sendt2Friend Formularfelder */
	initSendToFriendValidation();
	initPublicationReviewValidation();
	
/* (en) Applys round Corners to the specified elements */
/* (de) Runded die Ecken der ausgew�hlten Container ab */
	roundAll();
	
}




 /****** 5 Page specific custom functions ******/
/* (en) Site specific Functions */ 
/* (de) Seitenspezifische Funktionen */


/* (en) Configuration of the HeatMap */ 
/* (de) Konfiguration der Heatmap */

function configHeatMap() {
	
	/* Set Local File Name */
	clickHeatSite = '';
	clickHeatGroup = 'publication_detail';
	clickHeatServer = '/clickheat/click.php';
	initClickHeat();
	
}


/**
 *  Initializes validation for publication review form fields
 */
function initPublicationReviewValidation() {
	var title = new Spry.Widget.ValidationTextField("pr-title");
	var text = new Spry.Widget.ValidationTextarea("pr-text", {isRequired:true});
}

/**
 * Function for navigation handling. This function keeps track of the publication context navigation panel.
 */
function initPublicationNavigation(id) {
	// remember active tab
	tp_publication_detail_selected = id;
	switch (tp_publication_detail_selected) {
	  case 5:	// review
		// hide form and show loading image
		document.getElementById("review_login_loader").style.display = "block";
		document.getElementById("review_login_request").style.display = "none";
		document.getElementById("status-message-review").style.display = "none";
		document.getElementById("reviewForm").style.display = "none";
	    break;
	  default:
	    break;
	}
	// show the panel
	//tp_publication_detail.showPanel(id);
	// check if a user is registered
	isUserRegistered(isUserRegisteredCallback);
}

/* callback function to handle the response of the isUserRegistered() call */
function isUserRegisteredCallback(req) {
	// check status code
	
	var ds = new Spry.Data.JSONDataSet();
	// load the returned json data into a dataset
	ds.loadDataIntoDataSet(req.xhRequest.responseText);
	// display content
	if(ds.getData()[0]["email"]) {		// logged in user found
		switch (tp_publication_detail_selected) {
		  case 5:	// review
			// hide loading image and show form
			document.getElementById("review_login_loader").style.display = "none";
			document.getElementById("reviewForm").style.display = "block";
		    break;
		  default:
		    break;
		}
	} else {	// no logged in user found...
		switch (tp_publication_detail_selected) {
		  case 5:	// review
			// hide loading image and show form
			document.getElementById("review_login_loader").style.display = "none";
			document.getElementById("review_login_request").style.display = "block";
		    break;
		  default:
		    break;
		}
	}
}

/**
 * Validates the publication review form and submits it if validation succeeds
 */
function validatePublicationReview(form){
	if (Spry.Widget.Form.validate(form) == true){
		document.getElementById('loading-review').style.display = "inline";
		Spry.Utils.submitForm(form, reviewSuccessCallback, {additionalData:'publicationId=' + publicationId, errorCallback: reviewErrorCallback});
	}
	return false;
}

/**
 * Callback function to handle the response of the publication
 * review form after submit via XHR.
 */
function reviewSuccessCallback(req) {
	var ds = new Spry.Data.JSONDataSet();
	
	
	
	// load the returned json data into a dataset
	ds.loadDataIntoDataSet(req.xhRequest.responseText);
	// set the css class appropriate to the returned message type
	if(ds.getData()[0]["name"] == "message") {
		Spry.Utils.removeClassName(document.getElementById("status-message-review"), "error");
		Spry.Utils.addClassName(document.getElementById("status-message-review"), "confirmation");
	}
	if(ds.getData()[0]["name"] == "error") {
		Spry.Utils.removeClassName(document.getElementById("status-message-review"), "confirmation");
		Spry.Utils.addClassName(document.getElementById("status-message-review"), "error");
	}
	// display returned message
	document.getElementById("status-message-review").innerHTML = ds.getData()[0]["message"];
	document.getElementById("status-message-review").style.display = "block";
	statusReviewFade.start();
	// reset form
	document.getElementById("reviewForm").reset();
	// hide loading image
	document.getElementById('loading-review').style.display = "none";
}

/**
 * Callback function to handle the response of the publication
 * review form after submit via XHR and a system error occurs.
 */
function reviewErrorCallback(req) {
	// display an error message
	Spry.Utils.removeClassName(document.getElementById("status-message-review"), "confirmation");
	Spry.Utils.addClassName(document.getElementById("status-message-review"), "error");
	document.getElementById("status-message-review").innerHTML = "Es ist ein Systemfehler aufgetreten! Bitte versuchen Sie es später noch einmal.";
	statusReviewFade.start();
}

/**
 * Check the availability of the current publication.
 * 
 * @return The availability of the current publication
 */
function checkAvailability() {
	// hide link and display loading image
	document.getElementById('availability-check').style.display = "none";
	document.getElementById('availability-load').style.display = "block";
	// get the availability
	var req = Spry.Utils.loadURL("GET", "/json/controller.do?path=/publications&method=availability&publicationId=" + publicationId, true, availabilitySuccessCallback, {errorCallback: availabilityErrorCallback});
}

/**
 * Callback function to handle the response of the publication
 * availability code check.
 */
function availabilitySuccessCallback(req) {
	var ds = new Spry.Data.JSONDataSet();
	// load the returned json data into a dataset
	ds.loadDataIntoDataSet(req.xhRequest.responseText);
	// check what style class to apend
	var cssClass;
	switch (ds.getData()[0]["availabilityCodeId"]) {
	  case 1:	// in stock
		  cssClass = "ac-available";
		  break;
	  case 2:	// available in 1-2 weeks
		  cssClass = "ac-12-weeks";
		  break;
	  case 3:	// available in 3-4 weeks
		  cssClass = "ac-34-weeks";
		  break;
	  case 4:	// available for preorder
		  cssClass = "ac-preorder";
		  break;
	  case 5:	// not available
		  cssClass = "ac-not-available";
		  break;
	  default:
	    break;
	}
	// set the returned availability code
	document.getElementById('availability-check').innerHTML = "<p class=" + cssClass + ">" + ds.getData()[0]["description"] + "</p><p>" + ds.getData()[0]["hint"] + "</p>";
	document.getElementById('availability-check').style.display = "block";
	// hide loading image
	document.getElementById('availability-load').style.display = "none";
}

/**
 * Callback function to handle the response of the publication
 * availability code check and a system error occurs.
 */
function availabilityErrorCallback(req) {
	alert("check failed");
}

/**
 * Redirects the user to the cas login screen and appends the current detail page URL
 * as service URL to the login request.
 * 
 * @param panel The active publication navigation panel
 */
function ssoRedirect(panel){
	var service = window.location.protocol + "//" + window.location.hostname;
	
	if(panel) {
		var queryString = "/controller.do?path=/publication&method=detail&area=3&publicationId=" + publicationId;
		var hash = window.location.hash;
		
		if(queryString) {
			params = Spry.Utils.getLocationParamsAsObject();
			// check if panel param available
			if(!params.panel) {
				service += queryString + "&panel=" + panel;
			} else {
				service += queryString;
			}
		} else {
			service += "?panel=" + panel;
		}
		// add hash to service url
		if(hash) {
			service += hash;
		}
	}
	window.location.href = "https://secure.brainguide.com:443/cas/login?service=" + escape(service);
}