/**
 * @author Erol Soyöz
 */

/**
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * @class CMS_Module_Template
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 */
var CMS_Module_Template = Class.create( {
	initialize : function() {

	},

	getTemplate : function(templateId, division, parameter, module) {
		var parameterToQueryString = Object.toQueryString(parameter);

		if ($(division) != null) {
			var postParameter = 'templateId=' + templateId + '&'
					+ parameterToQueryString;
			new Ajax.Request(absolutePath + '/cms/template/get', {
				method : 'post',
				parameters : postParameter,
				onSuccess : function(transport) {
					$(division).innerHTML = transport.responseText;

					if (templateId == 5){
					
					} else {
						makeEditable(parameter,module);
						startEditor();
						makeClickButton(parameter,module);
					}
				},
				onFailure : function() {

				}
			});
		}
	}
});

function startEditor(){
	
	if(document.getElementById('CONTENT-1')) {
        var oFCKeditor = new FCKeditor('CONTENT-1') ;
        //alert('http://'+location.host + absolutePath + '/public/scripts/library/fckeditor/');
        oFCKeditor.BasePath = 'http://'+location.host + absolutePath + '/public/scripts/library/fckeditor/';
        oFCKeditor.Height = 450;
        oFCKeditor.Width = 1000;
        oFCKeditor.ReplaceTextarea() ; 
    }	
}	

function makeClickButton(parameter,module){
	jQuery('.textareabutton').click(function(){
		oldvalue = parameter.value ;
		oldid = parameter.id;
		this.disabled = 'disabled';
		parameter.id = this.id;
		var oEditor = FCKeditorAPI.GetInstance(this.id) ;
		parameter.value = oEditor.GetHTML();
		parameter.id =  this.id;
		textAreaUpdate(parameter,module);
		parameter.value = oldvalue;
		parameter.id = oldid;
	});
}

function textAreaUpdate(parameter,module){
	new Ajax.Request(absolutePath + '/admin/'+ module +'/update', {
		method : 'post',
		parameters : parameter,
		onSuccess : function(transport) {
			jQuery(":button").attr("disabled","");
		},
		onFailure : function() {}
	});
}

/**
 * @member CMS_Module_Template
 */
function makeEditable(parameter,module) {
	module = (module) ? module : 'news';
	jQuery('.editableMeta').editable(absolutePath + '/admin/'+ module +'/update', {
		submitdata : parameter,
		type : 'textarea',
		cancel : 'Cancel',
		submit : 'Update',
		width : '310px',
		height : '80px',
		event : "dblclick",
		style : "inherit",
		tooltip : 'Click to edit...'
	});
	
	jQuery('.editableInput').editable(absolutePath + '/admin/'+ module +'/update', {
		submitdata : parameter,
		type : 'text',
		cancel : 'Cancel',
		submit : 'Update',
		width : '500px',
		height : '13px',
		event : "dblclick",
		style : "inherit",
		tooltip : 'Click to edit...'
	});

	jQuery('.editableTextarea').editable(absolutePath + '/admin/'+ module +'/update', {
		submitdata : parameter,
		type : 'wysiwyg',
		cancel : 'Cancel',
		submit : 'Update',
		width : '600px',
		height : '150px',
		onblur : 'ignore',
		wysiwyg : {
			controls : {
				separator04 : {
					visible : true
				},
				insertOrderedList : {
					visible : true
				},
				insertUnorderedList : {
					visible : true
				}
			}
		}
	});
}

/**
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * @class CMS_Module_Image
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 */
var CMS_Module_Image = Class.create( {
	initialize : function() {

	},

	cropDialog : function(division, imageId, width, height, isStandalone,
			imageAspectRatioX, imageAspectRatioY, gotoController, redirect) {
		jQuery( function() {
			jQuery("#" + division).dialog(
					{
						bgiframe : true,
						resizable : false,
						modal : true,
						draggable : true,
						width : width + 175,
						height : height + 175,
						buttons : {
							Ok : function() {
								var sourceX = $('sourceX').value;
								var sourceY = $('sourceY').value;
								var width = $('width').value;
								var height = $('height').value;

								var parameter = 'sourceX=' + sourceX
										+ '&sourceY=' + sourceY + '&width='
										+ width + '&height=' + height;

								if (isStandalone == false) {
									new CMS_Module_Image().add(division,
											parameter, gotoController, redirect);
								}

								if (isStandalone == true) {
									new CMS_Module_Image().standalone(division,
											parameter);
								}
							}
						}
					});
		});
		new CMS_Module_Image().crop(imageId, width, height, imageAspectRatioX,
				imageAspectRatioY);
	},

	crop : function(imageId, width, height, imageAspectRatioX,
			imageAspectRatioY) {
		/**
		 * Eger image management'dan geliyorsa, imageAspectRatioX ile
		 * imageAspectRatioY tanimlanmaz.
		 * 
		 * Sadece template tarafindan gelen yuklemelerde imageAspectRatioX ve
		 * imageAspectRatioY degerleri gonderilir.
		 */
		if (imageAspectRatioX == undefined && imageAspectRatioY == undefined) {
			var imageAspectRatioX = 4;
			var imageAspectRatioY = 3;
		}

		jQuery( function() {
			jQuery('#' + imageId).Jcrop( {
				aspectRatio : imageAspectRatioX / imageAspectRatioY,
				setSelect : [ 10, 10, width / 2, height / 2 ],
				bgOpacity : .6,
				bgColor : '#343434',
				onChange : cropEventHandler,
				onSelect : cropEventHandler
			});
		});
	},

	add : function(division, parameter, gotoController, redirect) {
		new Ajax.Request(absolutePath + '/cms/' + gotoController + '/add', {
			method : 'post',
			parameters : parameter,
			onSuccess : function(transport) {
				jQuery("#" + division).dialog('destroy');
				jQuery("#" + division).dialog('close');

				$(division).innerHTML = transport.responseText;

				completedDialog(division, redirect);
			},
			onFailure : function() {

			}
		});
	},

	standalone : function(division, parameter) {
		new Ajax.Request(absolutePath + '/cms/image/add', {
			method : 'post',
			parameters : parameter,
			onSuccess : function(transport) {
				jQuery("#" + division).dialog('destroy');
				jQuery("#" + division).dialog('close');

				$(division).innerHTML = transport.responseText;

				standaloneCompletedDialog(division);
			},
			onFailure : function() {

			}
		});
	},

	showHideCopyright : function(division) {
		if ($(division).style.display == 'none') {
			$(division).show();
		} else {
			$(division).hide();
		}
	}
});

/**
 * @member CMS_Module_Image
 */
function completedDialog(division, gotoController) {
	jQuery( function() {
		jQuery("#" + division).dialog(
				{
					bgiframe : true,
					resizable : false,
					modal : true,
					draggable : true,
					width : 350,
					height : 200,
					buttons : {
						Ok : function() {
							jQuery("#" + division).dialog('destroy');
							jQuery("#" + division).dialog('close');

							window.location.href = '' + absolutePath
									+ '/admin/' + gotoController + '/list';
						}
					}
				});
	});
}

/**
 * @member CMS_Module_Image
 */
function standaloneCompletedDialog(division) {
	jQuery( function() {
		jQuery("#" + division)
				.dialog(
						{
							bgiframe : true,
							resizable : false,
							modal : true,
							draggable : true,
							width : 350,
							height : 200,
							buttons : {
								Ok : function() {
									jQuery("#" + division).dialog('destroy');
									jQuery("#" + division).dialog('close');

									$(division).innerHTML = '<div id="dialogDescription"></div><div id="dialogCrop" style="padding-top: 20px;"></div>';
								}
							}
						});
	});
}

/**
 * @member CMS_Module_Image
 */
function asyncUploadOnStart() {

}

/**
 * @member CMS_Module_Image
 */
function asyncUploadOnComplete(response) {
	response = response.evalJSON();

	if (response.error == 'false') {
		var dialogDivision = response.dialogDivision;
		var dialogCropImageId = response.dialogCropImageId;
		var dialogCropDivision = response.dialogCropDivision;
		var dialogDescriptionDivision = response.dialogDescriptionDivision;
		var gotoController = response.gotoController;
		var image = response.image;
		var title = response.title;
		var description = response.description;
		var width = response.width;
		var height = response.height;
		var imageAspectRatioX = response.imageAspectRatioX;
		var imageAspectRatioY = response.imageAspectRatioY;
		var redirect = response.redirect;

		$(dialogDivision).title = title;
		$(dialogCropDivision).innerHTML = '<img src="' + image + '" id="'
				+ dialogCropImageId + '">';
		$(dialogDescriptionDivision).innerHTML = description;

		new CMS_Module_Image().cropDialog(dialogDivision, dialogCropImageId,
				width, height, false, imageAspectRatioX, imageAspectRatioY,
				gotoController, redirect);
	} else {
		alert(response.description);
	}
}

/**
 * @member CMS_Module_Image
 */
function cropEventHandler(coordination) {
	var sourceX = coordination.x;
	var sourceY = coordination.y;
	var width = coordination.w;
	var height = coordination.h;

	$('sourceX').value = sourceX;
	$('sourceY').value = sourceY;
	$('width').value = width;
	$('height').value = height;
}

/**
 * @member CMS_Module_Content
 * 
 * 
 */
var CMS_Module_Content = Class
.create( {
	initialize : function() {

	},

	imageListDialog : function(division, targetDivision, parameter) {
		jQuery( function() {
			this.dene = 10;
			jQuery("#" + division).load(
					absolutePath + '/cms/image/list?division='
							+ division + '&targetDivision='
							+ targetDivision + '&' + parameter).dialog(
					{
						bgiframe : true,
						resizable : false,
						modal : true,
						width : 640,
						height : 480,
						draggable : true,
						closeOnEscape : false,
						buttons : {
							Cancel : function() {
								jQuery(this).dialog('destroy');
								jQuery(this).dialog('close');
							}
						}
					});
		});
	},

	addImageToModule : function(division, targetDivision, imagePath,
			imageId, contentId, field) {
		var parameter = 'imageId=' + imageId + '&contentId=' + contentId
				+ '&field=' + field;

		new Ajax.Request(absolutePath + '/admin/content/image', {
			method : 'post',
			parameters : parameter,
			onSuccess : function(transport) {

			},
			onFailure : function() {

			}
		});

		jQuery('#' + division).dialog('destroy');
		jQuery('#' + division).dialog('close');

		$(targetDivision).innerHTML = '<img src="'
				+ imagePath
				+ '" hspace="5" vspace="5" align="left" onclick="new CMS_Module_Content().imageListDialog(\''
				+ division + '\', \'' + targetDivision
				+ '\', \'moduleName=Content&moduleId=' + contentId + '&field=' + field
				+ '\');">';
	},

	neverExpire : function(startDateId, endDateId, status) {
		if ($(startDateId) != null && $(endDateId) != null) {
			if (status == '1') {
				$(startDateId).style.color = '#707070';
				$(endDateId).style.color = '#707070';
				$(startDateId).disabled = true;
				$(endDateId).disabled = true;
			} else {
				$(startDateId).style.color = '';
				$(endDateId).style.color = '';
				$(startDateId).disabled = false;
				$(endDateId).disabled = false;
			}
		}
	}
});


/**
 * @member CMS_Module_Content
 */
function asyncStandaloneUploadOnStart() {

}

/**
 * @member CMS_Module_Content
 */
function asyncStandaloneUploadOnComplete(response) {
	response = response.evalJSON();

	if (response.error == 'false') {
		var imageAspectRatioX = response.imageAspectRatioX;
		var imageAspectRatioY = response.imageAspectRatioY;
		var dialogDivision = response.dialogDivision;
		var dialogCropImageId = response.dialogCropImageId;
		var dialogCropDivision = response.dialogCropDivision;
		var dialogDescriptionDivision = response.dialogDescriptionDivision;
		var image = response.image;
		var title = response.title;
		var description = response.description;
		var width = response.width;
		var height = response.height;
		var redirect = response.Redirect;
		
		$(dialogDivision).title = title;
		$(dialogCropDivision).innerHTML = '<img src="' + image + '" id="'
				+ dialogCropImageId + '">';
		$(dialogDescriptionDivision).innerHTML = description;

		new CMS_Module_Image().cropDialog(dialogDivision, dialogCropImageId,
				width, height, true, imageAspectRatioX, imageAspectRatioY);
	} else {
		alert(response.description);
	}
}

/**
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * @class Text
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 */
var Text = Class.create( {
	initialize : function() {
	},

	/**
	 * @example
	 * 
	 * var text = new Text();
	 * 
	 * text.pulsate('my_division');
	 */
	pulsate : function(division) {
		Effect.Pulsate(division, {
			pulses : 5,
			duration : 2
		});
	}
});

/**
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * @class Validate
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 */
var Validate = Class.create( {
	initialize : function() {
	},

	/**
	 * @example
	 * 
	 * var validate = new Validate();
	 * 
	 * validate.all();
	 */
	all : function(form,type) {
		var isValidated = true;
		var getParameterList = $(form).serialize().toQueryParams();

		Object.keys(getParameterList).each( function(field) {
			if ($(field + '_validate')) {
				$(field + '_validate').style.display = 'none';

				if ($(field).value == '') {
					$(field + '_validate').style.color = '#CE0000';
					$(field + '_validate').style.display = 'block';
					$(field).focus();

					isValidated = false;
				}
			}

			if ($(field + '_validate_email')) {
				$(field + '_validate_email').style.display = 'none';

				if (new Helper_Common().validateEmail($(field).value) == false) {
					$(field + '_validate_email').style.color = '#CE0000';
					$(field + '_validate_email').style.display = 'block';
					$(field).focus();

					isValidated = false;
				}
			}			
		});

		if (isValidated == true) {
			if (type){
				return true;
			} else {
				$(form).submit();
			}
		} else {
			return false;
		}
	},

	/**
	 * @example
	 * 
	 * var validate = new Validate();
	 * 
	 * validate.confirmation();
	 */
	confirmation : function(division, link) {
		jQuery( function() {
			jQuery('#' + division).dialog( {
				bgiframe : true,
				resizable : false,
				height : 170,
				modal : true,
				draggable : false,
				overlay : {
					backgroundColor : '#000',
					opacity : 0.5
				},
				buttons : {
					'Yes' : function() {
						jQuery(this).dialog('destroy');
						document.location.href = link;
					},
					Cancel : function() {
						jQuery(this).dialog('destroy');
						jQuery(this).dialog('close');
					}
				}
			});
		});
	}
});

function nl2br(str, is_xhtml) {
	var breakTag = '';

	breakTag = '<br />';
	if (typeof is_xhtml != 'undefined' && !is_xhtml) {
		breakTag = '<br>';
	}

	return (str + '').replace(/([^>]?)\n/g, '$1' + breakTag + '\n');
}

function returnFalse() {
	return false;
}

function slide(clickDivision, targetDivision) {
	jQuery(clickDivision).click(function() {
		if (jQuery(targetDivision).is(":hidden")) {
			jQuery(targetDivision).slideDown("slow");
		} else {
			jQuery(targetDivision).slideUp("slow");
		}
	});
}

/**
 * contentlerin son halini gormek icin
 * @param contentId
 * @return
 */
function previewContent(contentId) {
	
	try {
		var translateId = $('translateId').value;
	} catch(e){
		var translateId = 1;
	}
	
	var link = absolutePath + '/default/content/preview/id/' + contentId
	Modalbox.show(link, {method: 'post', width: 640, title: 'Content Preview', params: 'previewstatus=1&translateId='+translateId}); return false;
}

