// Search for "tx_ddimain_pi1" inside this javascript. It is a global variable in global.inc that is not in a variable here.

// Global variable
var prefixId = "tx_ddimain_pi1";
	
// JavaScript Function Document

function clearThisField(obj) {
	obj.value = "";
}

function check_category(currentbox){
	var x=document.getElementById("combo_0");
	if ((currentbox.value == 16) || (currentbox.value == 17) || (currentbox.value == 18) || (currentbox.value == 19)) {
		x.disabled=true;
		x.value="";
	} else {
		x.disabled=false;
		x.value="";
	}
}

function submitForm_p1(formid) {
	var x=document.getElementById(formid);
	x.submit();
}

function switchImage(main, span, image, text) {
	document.getElementById(main).src = 'fileadmin/_temp_/' + image;
	document.getElementById(span).innerHTML = text;
}

function enswitchImage(main, span, image, text) {
	document.getElementById(main).src = 'fileadmin/_entemp_/' + image;
	document.getElementById(span).innerHTML = text;
}

function rslShow(elemId) {
	var rsl_elem = document.getElementById(elemId);
	rsl_elem.style.display = "inline";
}

function rslHide(elemId) {  
	var rsl_elem = document.getElementById(elemId);
	rsl_elem.style.display = "none";
}

function rslChk(elemId) {
	var rsl_elem = document.getElementById(elemId);
	if (rsl_elem.checked == true) {
		rsl_elem.checked = false;
	} else {
		rsl_elem.checked = true;	
	}
}

function rslCheck(elemId, chekId, formName) {
	var rsl_elem = document.getElementById(elemId);
	var rsl_chek = document.forms[formName].elements[chekId];

	if (rsl_chek.checked == 1) {
		rsl_elem.style.display = "inline";
	} else {
		rsl_elem.style.display = "none";
	}
}

function set_hidden_value(elemId) {
	var rsl_elem = document.getElementById(elemId);
	
	if (rsl_elem.value == 1) {
		rsl_elem.value = '';
	} else {
		rsl_elem.value = 1;
	}
}

function manageTextField(textId, chekId) {
	var rsl_text = document.getElementById(textId);

	if (chekId.checked == 1) {
		rsl_text.disabled = false;
		rsl_text.focus();
	} else {
		rsl_text.disabled = true;
	}
}

function disableTextField(textId) {
	var rsl_text = document.getElementById(textId);

	rsl_text.disabled = true;
}

function enableTextField(textId) {
	var rsl_text = document.getElementById(textId);

	rsl_text.disabled = false;
	rsl_text.focus();
}

function showRss(){	

	$("#alert_link_rss").live("click",  function(event) {
		event.preventDefault();
		
		var url = $(this).attr('href');
		
		url = url + '?tx_ddimain_pi1[type]=rss';
		
		$.getJSON(url, '', function (data){
			$("#rss_td").html(data.content);
		});
	});
	
	$("#alert_link_mail").live("click",  function(event) {
		event.preventDefault();
		
		var url = $(this).attr('href');
		url = url + "?tx_ddimain_pi1[type]=mail&tx_ddimain_pi1[courriel]=" + document.getElementById("courriel").value + "&tx_ddimain_pi1[mdp]=" + document.getElementById("mdp").value + "&tx_ddimain_pi1[tmp]=" + document.getElementById("hidden").value + "&jsoncallback=?";
		
		$.getJSON(url, '', function (data){
			var content = data.content.replace('\"', '"');
			$("#rss_td").html(content);
		});
	});;
	
}

// Facebook
function ddiFacebookLogout(url) {
	FB.Connect.logout(function(){
     // logout link
     window.location.href = url;
   })	
}

function fbPermissionMessage(permission) {
	var perm = false;
	FB.Facebook.apiClient.users_hasAppPermission(permission, function(res,ex) {
		if (res) {
			//we have the permission.
			perm = true;
			window.location.reload(); // just reload for now since we are not ajax.
		} else {
			//no permission, ask for it.
			FB.ensureInit(function() { FB.Connect.showPermissionDialog(permission, function(result) {
				if (result == permission) {
					perm = true;
					window.location.reload(); // just reload for now since we are not ajax.
				} else {
					window.location.reload(); // just reload for now since we are not ajax.
				}
			});
			});
		}
	});
	return perm;
}
// End Facebook

//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	// select all desired input fields and attach tooltips to them 
	$("#address_ship_phone").tooltip({ 
	 
		// place tooltip on the right edge 
		position: "center right", 
	 
		// a little tweaking of the position 
		offset: [-2, 10], 
	 
		// use a simple show/hide effect 
		effect: "fade", 
		 
		// custom opacity setting 
		opacity: 0.7, 
		 
		// use this single tooltip element 
		tip: '.tooltip' 
		 
	});
	$("#address_billing_phone").tooltip({ 
	 
		// place tooltip on the right edge 
		position: "center right", 
	 
		// a little tweaking of the position 
		offset: [-2, 10], 
	 
		// use a simple show/hide effect 
		effect: "fade", 
		 
		// custom opacity setting 
		opacity: 0.7, 
		 
		// use this single tooltip element 
		tip: '.tooltip' 
		 
	});
	
	$("#website").tooltip({
		// place tooltip on the right edge
		position: "center right",
		
		// a little tweaking of the position
		offset: [-2, 10],
		
		// use the built-in fadeIn/fadeOut effect
		effect: "fade",
		
		// custom opacity setting
		opacity: 0.7,
		
		// use this single tooltip element
		tip: '.tooltip'
	});
	
	//OVERLAY
	$(".triggers[rel]").overlay({
		left	: 'center',
		top		: '15%',
		expose: { 
			color: '#000', 
			loadSpeed: 200, 
			opacity: 0.7
		}, 
		closeOnClick: true,
		
		// hide trigger when the overlay is opened 
		onBeforeLoad: function(event) {
			$("body").css({
				"overflow": "hidden"
			});
		},
		// hide trigger when the overlay is opened 
		onBeforeClose: function(event) { 
			$("body").css({
				"overflow": "scroll",
				"overflow-x": "hidden"
			});
		}
	});
	
	detailPageLoaded();
});

/* ****************************************

/* Get comments pagination */
function getPageList(data){
	$("#page_comment").html(data.content);
}
/* ****************************************

/* Comment FORM validation */
function detailPageLoaded() {

	/* Activate CLICK detection for VOTING */
	$(".thumbsDOWN").live("click",  function(event) {
		event.preventDefault();
		
		
		var uidNum = $(this).parent('div').attr('id');
		var url = $(this).attr('href') + '?ts=' + getTimeStamp() + '&tx_ddimain_pi1[uid_vote]=' + uidNum + '&tx_ddimain_pi1[vote]=thumbsDOWN';
		
		$.getJSON(url, {}, parseInfo);
	});
	$(".thumbsUP").live("click",  function(event) {
		event.preventDefault();
		
		var uidNum = $(this).parent('div').attr('id');
		var url = $(this).attr('href') + '?ts=' + getTimeStamp() + '&tx_ddimain_pi1[uid_vote]=' + uidNum + '&tx_ddimain_pi1[vote]=thumbsUP';
		
		$.getJSON(url, {}, parseInfo);
	});
	
	/* Activate CLICK detection for PAGE SWITCHING */
	$(".comment_number_page_row a").live("click",  function(event) {
		$("#voteError").css({'display':'none'});
		
		event.preventDefault();
		
		showLoading($("#pageAjaxLoading"), $("#user_comment"));
		
		var page = $(this).attr('rel');
		
		var currentPage = parseInt($('#cmdCurrentPAGE').val());
		if (page=="previous"){
			page = currentPage - 1;
			$('#cmdCurrentPAGE').val(page);
		} else if (page=="next") {
			page = currentPage + 1;
			$('#cmdCurrentPAGE').val(page);
		} else {
			$('#cmdCurrentPAGE').val(page);
		}
		
		var i = 1;
		var nbrPage = $('#cmdPAGE').val();
		
		while (i<=nbrPage) {
			var links = 'comment_page_link_' + i;
			document.getElementById(links).style.fontWeight = "normal";
			document.getElementById(links).style.textDecoration = "underline";
			i++;
		}
		
		var aToShow		= 'comment_page_link_' + page;
		document.getElementById(aToShow).style.fontWeight = "bold";
		document.getElementById(aToShow).style.textDecoration = "none";
		
		if (page>1){
			document.getElementById('comment_page_link_previous').style.display = "inline";
		} else {
			document.getElementById('comment_page_link_previous').style.display = "none";
		}
		
		if(page<nbrPage){
			document.getElementById('comment_page_link_next').style.display = "inline";
		} else {
			document.getElementById('comment_page_link_next').style.display = "none";
		}
		
		var url = '';
		url = $(this).attr('href') + '?tx_ddimain_pi1[action]=switchPage' + '&tx_ddimain_pi1[thePage]=' + $('#cmdCurrentPAGE').val();
		$.post(	url, 
					{}, 
					function (data){
						$('#user_comment').html(data.content);
						hideLoading($("#pageAjaxLoading"), "slow");
					}, "json"
		);
	});

	if($('#facebook_user').length == 0){
		$("#commentaireForm").validate({
			errorContainer: "#commentMESSAGE, #commentMESSAGE2",
			errorLabelContainer: "#commentMESSAGE .divError",
			wrapper: "div",
			rules: {
				"tx_ddimain_pi1[courriel]": {
						required: true,
						email: true
				},
				"tx_ddimain_pi1[mdp]": {
						required: true
				},
				"tx_ddimain_pi1[RcommentText]": {
						required: true
				}
			},
			submitHandler: function() {
				showLoading($("#pageAjaxLoading"), $("#new_comment"));
				
				var url = $("#commentaireForm").attr('action');
				url = url + '?tx_ddimain_pi1[type]=auth&jsoncallback=?';
				
				var dataToBeSent = $("#commentaireForm").serialize();
				
				$.getJSON(url, dataToBeSent, function (data){
					
					var url = 'http://www.drummondhouseplans.com/';
					url = url + data.url + '?tx_ddimain_pi1[auth]=' + data.auth + '&tx_ddimain_pi1[userID]=' + data.userID + '&tx_ddimain_pi1[name]=' + data.name;
					
					$.post(url, dataToBeSent, function (data){
						
						if(data.success != ""){
							$('#commentMESSAGE').attr('class', 'messageStackSuccess_td');
							$('#commentMESSAGE').css({'display':'block'});
							$('#commentMESSAGE').children('div').css({'display':'block'});
							$('#commentMESSAGE .divError').css({'display':'block'});
							$('#commentMESSAGE .divError').html('<div><label style="display: block;" class="success" generated="true">'+ data.success +'</label></div>');
							$("#commentMESSAGE2").children('img').attr("src","fileadmin/user_upload/success.gif");
							$("#commentMESSAGE2").children('img').attr("alt", data.successTitle);
							$('#commentMESSAGE2').children('label').html(data.successTitle);
							$('.comment_new_comment_form').css({'display':'none'});
						} else if (data.error != ""){
							$('#commentMESSAGE').attr('class', 'messageStackError_td');
							$('#commentMESSAGE').css({'display':'block'});
							$('#commentMESSAGE').children('div').css({'display':'block'});
							$('#commentMESSAGE .divError').css({'display':'block'});
							$('#commentMESSAGE .divError').html('<div><label style="display: block;" class="error" generated="true">'+ data.error +'</label></div>');
						}
						hideLoading($("#pageAjaxLoading"), "slow");
						
					}, "json");
					
				});
			}
		})
	} else {
		$("#commentaireForm").validate({
			errorContainer: "#commentMESSAGE, #commentMESSAGE2",
			errorLabelContainer: "#commentMESSAGE .divError",
			wrapper: "div",
			rules: {
				"tx_ddimain_pi1[RcommentText]": {
						required: true
				}
			},
			submitHandler: function() {
			
				showLoading($("#pageAjaxLoading"), $("#new_comment"));
				
				var url = $("#commentaireForm").attr('action');
				var dataToBeSent = $("#commentaireForm").serialize();
				$.post(url, dataToBeSent, function (data){
					if(data.success != ""){
						$('#commentMESSAGE').attr('class', 'messageStackSuccess_td');
						$('#commentMESSAGE').css({'display':'block'});
						$('#commentMESSAGE').children('div').css({'display':'block'});
						$('#commentMESSAGE .divError').css({'display':'block'});
						$('#commentMESSAGE .divError').html('<div><label style="display: block;" class="success" generated="true">'+ data.success +'</label></div>');
						$("#commentMESSAGE2").children('img').attr("src","fileadmin/user_upload/success.gif");
						$("#commentMESSAGE2").children('img').attr("alt", data.successTitle);
						$('#commentMESSAGE2').children('label').html(data.successTitle);
						$('.comment_new_comment_form').css({'display':'none'});
					} else if (data.error != ""){
						$('#commentMESSAGE').attr('class', 'messageStackError_td');
						$('#commentMESSAGE').css({'display':'block'});
						$('#commentMESSAGE').children('div').css({'display':'block'});
						$('#commentMESSAGE .divError').css({'display':'block'});
						$('#commentMESSAGE .divError').html('<div><label style="display: block;" class="error" generated="true">'+ data.error +'</label></div>');
					}
					hideLoading($("#pageAjaxLoading"), "slow");
					
				}, "json");
			}
		})
	}
	
	/* Form validation for comment entry - Anonymous */
	$("#AcommentaireForm").validate({
		errorContainer: "#AcommentMESSAGE, #AcommentMESSAGE2",
		errorLabelContainer: "#AcommentMESSAGE .divError",
		wrapper: "div",
		ignore: "ignore",
		rules: {
			"tx_ddimain_pi1[full_name]": {
					required: true,
					maxlength: 80
			},
			"tx_ddimain_pi1[courriel_anonym]": {
					required: true,
					email: true
			},
			"tx_ddimain_pi1[website]": {
					required: false,
					url: true
			},
			"tx_ddimain_pi1[AcommentText]": {
					required: true
			}
		},
		submitHandler: function() {
			showLoading($("#pageAjaxLoading"), $("#Anew_comment"));
			
			var url = $("#AcommentaireForm").attr('action');
			var dataToBeSent = $("#AcommentaireForm").serialize();
			$.post(url, dataToBeSent, function (data){
				if(data.success != ""){
					$('#AcommentMESSAGE').attr('class', 'messageStackSuccess_td');
					$('#AcommentMESSAGE').css({'display':'block'});
					$('#AcommentMESSAGE').children('div').css({'display':'block'});
					$('#AcommentMESSAGE .divError').css({'display':'block'});
					$('#AcommentMESSAGE .divError').html('<div><label style="display: block;" class="success" generated="true">'+ data.success +'</label></div>');
					$("#AcommentMESSAGE2").children('img').attr("src","fileadmin/user_upload/success.gif");
					$("#AcommentMESSAGE2").children('img').attr("alt", data.successTitle);
					$('#AcommentMESSAGE2').children('label').html(data.successTitle);
					$('.comment_new_comment_form').css({'display':'none'});
				} else if (data.error != ""){
					$('#AcommentMESSAGE').attr('class', 'messageStackError_td');
					$('#AcommentMESSAGE').css({'display':'block'});
					$('#AcommentMESSAGE').children('div').css({'display':'block'});
					$('#AcommentMESSAGE .divError').css({'display':'block'});
					$('#AcommentMESSAGE .divError').html('<div><label style="display: block;" class="error" generated="true">'+ data.error +'</label></div>');
				}
				hideLoading($("#pageAjaxLoading"), "slow");
				
			}, "json");
		}
	})
}
/* *****************************************/

/* Function that return VOTE value */
function parseInfo(data){
	if(data.result==1){
		$("#voteError").css({'display':'none'});
		var td = '#uid_' + data.uid;
		
		$(td).children('.thumbsDOWN').html('<b>' + data.nbrVoteDown + '</b> ' + data.disagree);
		$(td).children('.thumbsUP').html('<b>' + data.nbrVoteUp + '</b> ' + data.agree);
	} else {
		$("#voteError").css({'display':'block'});
	}
}
/* *****************************************/

/* Comment Page switching - LOADING script */
function showLoading(loadElem, overElem) {
	positionLoading(loadElem, overElem);
	loadElem.show();
}
function positionLoading(loadElem, overElem) {
   loadElem.width(overElem.width()+10);
   loadElem.height(overElem.height());
   loadElem.css("position", "absolute");
   loadElem.css("top", overElem.position().top);
   loadElem.css("left", overElem.position().left);
}
function hideLoading(loadElem, speed) {
   loadElem.fadeOut(speed);  
}
function getTimeStamp(){
	return new Date().getTime();
}
/* *****************************************/
// PLAN COMMENT SCRIPT END


//CHECK FOR IE6
function is_ie6(){
	return ((window.XMLHttpRequest == undefined) && (ActiveXObject != undefined));
}

function set_height(){
	if( is_ie6()){
		$(".popupInsideDiv[id]").each(function(index) {
			var newHeight = 500;
			
			$(this).height(newHeight);
		});
	} else {
		var $maxHeightPrct = 0.75;
		var $pageHeight = $(window).height();
		
		$(".popupOverlay[id]").each(function(index) {
			if($(this).attr("id").substring(0, 5) == "infos") {
				if($(this).height() > ($pageHeight * $maxHeightPrct)) {
					$(this).height($pageHeight * $maxHeightPrct);
				}
				var $marginTop = ((($pageHeight * 0.85) * $maxHeightPrct) - $(this).height()) / 2;
				$(this).css("margin-top", $marginTop);
			}
		});
	}
}