/*
| harus ada div dengan id = apankForm
*/

$(document).ready(function(){
	
	setPopUp = [ null, null];
	urlConfirm = [];
	
	(function( $ ) {
		$.fn.popUp = function(option){
			
			$(this).each(function(){
				idForm = this.id;
			})
			
			var defaults = $.extend({
				typePopUp : 'form',
				url : '',
				width : 500,
				height : 700,
				top : 200,
				classButton : '',
				title: 'Confirmasi',
				locDisplay: '',
				focus: '.apank',
				newVar : [],
			})
			
			setPopUp[idForm] = $.extend({}, defaults, option);
			formId 	= 'apankForm'+idForm;
			
			if(setPopUp[idForm]['typePopUp'] === 'form'){
				$(this).click(function(){
					idForm = this.id;
					makeCreateApankForm(this.id, setPopUp[idForm]);
					makeLoadContent(setPopUp[idForm]['url'], this.id, setPopUp[idForm]['top']);
					
				})
			}else if(setPopUp[idForm]['typePopUp'] === 'confirm'){
			
				jum = urlConfirm.length;
				urlConfirm[jum] = [setPopUp[idForm]['classButton'], setPopUp[idForm]['url'], setPopUp[idForm]['title']];
			
				$('.'+setPopUp[idForm]['classButton']).live('click',(function(){
					id = this.id;
					id = id.split('_');
					
					idForm = id[0];
					newId  = id[1];
					
					confirmClass = $(this).attr('class').split(' ');
					for(i = 0; i < urlConfirm.length; i++){
						if(urlConfirm[i][0] === confirmClass[0]){
							url = urlConfirm[i][1];
							title = urlConfirm[i][2];
						}
					}

					makeCreateApankPopUpConfirm(this.id, setPopUp[idForm], url, title);
				}))
				$('.'+setPopUp[idForm]['classButton']).live('keyup',(function(e){
					if(e.which === 13){
						id = this.id;
						id = id.split('_');
						
						idForm = id[0];
						newId  = id[1];
						
						confirmClass = $(this).attr('class').split(' ');
						for(i = 0; i < urlConfirm.length; i++){
							if(urlConfirm[i][0] === confirmClass[0]){
								url = urlConfirm[i][1];
								title = urlConfirm[i][2];
							}
						}

						makeCreateApankPopUpConfirm(this.id, setPopUp[idForm], url, title);
					}
				}))
			}else if(setPopUp[idForm]['typePopUp'] === 'alert_succes'){
				makeCreateApankPopUpAlertSucess(setPopUp[idForm]);
			}else if(setPopUp[idForm]['typePopUp'] === 'alert_error'){
				makeCreateApankPopUpAlertError(setPopUp[idForm]);
			}else if(setPopUp[idForm]['typePopUp'] === 'alert_error_focus'){
				makeCreateApankPopUpAlertErrorFocus(setPopUp[idForm]);
			}else if(setPopUp[idForm]['typePopUp'] === 'alert_error_pilihan'){
				makeCreateApankPopUpAlertErrorPilihan(setPopUp[idForm]);
			}
			
			
		};
	})( jQuery );
	
	makeCreateApankForm = function(id, set){
		minimize = '<input type="button" id="apankFormMinimizeButton" class="apankFormMButton" title="Minimize Form" alt="true">';

		$('#apankForm').html('<div id="apankForm_1" style="width: '+set['width']+'px;"></div>');
		$('#apankForm_1').html('<div id="apankForm_2" style="margin-top: '+set['top']+'px;"></div>');
		$('#apankForm_2').html('<div id="apankForm_3"></div>');
		$('#apankForm_3').html('<div id="apankForm_close"></div>');
		$('#apankForm_close').html(minimize+'<input type="button" id="apankFormCloseButton" title="Close Form"><div id="apankFormContent"></div>');
		$('#apankFormContent').html('<div id="apankFormContentLogin"></div>');
	}
	
	$('#apankFormCloseButton').live('click',(function(){
		$('#apankForm').empty();
	}))
	
	$('#apankFormMinimizeButton').live('click', (function(){
		
		status = $('#apankFormMinimizeButton').attr('alt');
		
		if(status === 'true'){
			$('#apankFormMinimizeButton').attr('alt', 'false');
			$('#apankFormContent').css('display', 'none');
		}else{
			$('#apankFormMinimizeButton').attr('alt', 'true');
			$('#apankFormContent').removeAttr('style');
		}
		
	}))
	
	makeLoadContent = function(url, id, top){
        $.ajax({
        type: 'GET',
            url: url+'/'+id,
            data: $(this).serialize(),
            success: function(data) {
				$('#apankFormContent').html(data);
				$('#apankForm_2').css('margin-top', top+'px');
            }
        })
	}
	
	makePageForm = function(url, top){
		$('#textPageForm').change(function(){
			
			val = 'no/'+$('#'+this.id).val();
			
			$('#apankFormContent').html('<div id="apankFormContentLogin"></div>');
			$('#apankForm_2').css('margin-top', top+'px');
			makeLoadContent(url, val, top);
		})
		$('.gridButton').click(function(){
			id = this.id;
			id = id.split(' ');
			
			val = 'no/'+id[1];
			$('#apankFormContent').html('<div id="apankFormContentLogin"></div>');
			$('#apankForm_2').css('margin-top', top+'px');
			makeLoadContent(url, val, top);
		})
	}
	makePagePopUpDouble = function(url, top, div){
		$('#textPageForm').change(function(){
			
			val = 'no/'+$('#'+this.id).val();
			
			$('#'+div).html('<div id="apankFormContentLogin"></div>');
			makeLoadContentDouble(url, val, top, div);
		})
		$('.gridButton').click(function(){
			id = this.id;
			id = id.split(' ');
			
			val = 'no/'+id[1];
			$('#'+div).html('<div id="apankFormContentLogin"></div>');
			makeLoadContentDouble(url, val, top, div);
		})
	}
	makeLoadContentDouble = function(url, id, top, div){
        $.ajax({
        type: 'GET',
            url: url+'/'+id,
            data: $(this).serialize(),
            success: function(data) {
				$('#'+div).html(data);
            }
        })
	}
	
	makeCreateApankPopUpConfirm = function(id, set, url, title){
		
		buttonYes = '<input type="button" class="apankPopUpConfirm confYes" value="Yakin" id="apank_PopUpConfirmYes" alt="ya">';
		buttonNo  = '<input type="button" class="apankPopUpConfirm  confNo"" value="Tidak" id="apank_PopUpConfirmNo" alt="tidak">';
	
		id = id.split('_');
		if(id[2] != null){
			title = title+' '+id[2];
		}
	
		$('#apankForm').html('<div id="apankConfirm_1" style="width: 100%;"></div>');
		$('#apankConfirm_1').html('<div id="apankConfirm_2" style="width: '+set['width']+'px;"></div>');
		$('#apankConfirm_2').html('<div id="apankForm_2" style="margin-top: '+set['top']+'px"></div>');
		$('#apankForm_2').html('<div id="apankForm_3"></div>');
		$('#apankForm_3').html('<div id="apankForm_close"></div>');
		$('#apankForm_close').html('<input type="button" id="apankFormCloseButton" title="Close Form"><div id="apankFormConfirm"></div>');
		$('#apankFormConfirm').html(title+'<div class="divConfirmButton">'+buttonYes+buttonNo+'</div>');
		
		$('#apank_PopUpConfirmYes').click(function(){
			$.ajax({
			type: 'POST',
				url: url+id[1]+'/'+id[2],
				data: $(this).serialize(),
				success: function(data) {
					$('#apankForm').empty();
					$('#'+set['locDisplay']).html(data);
				}
			})
		}).focus();
		
		$('#apank_PopUpConfirmYes').keyup(function(e){
			if(e.which == 37 || e.which == 39){
				$('#apank_PopUpConfirmNo').focus();
			}
		})
		$('#apank_PopUpConfirmNo').keyup(function(e){
			if(e.which == 37 || e.which == 39){
				$('#apank_PopUpConfirmYes').focus();
			}
		})
		
		$('#apank_PopUpConfirmNo').click(function(){
			$('#apankForm').empty();
		})
		$('.confYes').focus();
	}

	$('#_closePopUp').live('keyup', (function(e){
		if(e.which === 13){
			$('#apankForm').empty();
		}
	})).live('click', (function(){
		$('#apankForm').empty();
	}))

	makeCreateApankPopUpAlertSucess = function(set){
		_button = '<p style="text-align: center; margin-top: -5px"><input id="_closePopUp" type="button" value="Close" style="padding: 2px;"></p>';
		$('#apankForm').html('<div id="apankConfirm_1" style="width: 100%;"></div>');
		$('#apankConfirm_1').html('<div id="apankConfirm_2" style="width: '+set['width']+'px;"></div>');
		$('#apankConfirm_2').html('<div id="apankForm_2" style="margin-top: '+set['top']+'px"></div>');
		$('#apankForm_2').html('<div id="apankForm_3"></div>').effect("bounce", { times:7 }, 250);
		$('#apankForm_3').html('<div id="apankForm_close"></div>');
		$('#apankForm_close').html('<input type="button" id="apankFormCloseButton" title="Close Form"><div id="apankFormConfirmSucess"></div>'+_button);
		$('#apankFormConfirmSucess').html(set['title']);
		$('#_closePopUp').focus()
	}
	
	makeCreateApankPopUpAlertError = function(set){
		_button = '<p style="text-align: center; margin-top: -5px"><input id="_closePopUp" type="button" value="Close" style="padding: 2px;"></p>';
		$('#apankForm').html('<div id="apankConfirm_1" style="width: 100%;"></div>');
		$('#apankConfirm_1').html('<div id="apankConfirm_2" style="width: '+set['width']+'px;"></div>');
		$('#apankConfirm_2').html('<div id="apankForm_2" style="margin-top: '+set['top']+'px"></div>');
		$('#apankForm_2').html('<div id="apankForm_3"></div>').effect("bounce", { times:7 }, 250);
		$('#apankForm_3').html('<div id="apankForm_close"></div>');
		$('#apankForm_close').html('<input type="button" id="apankFormCloseButton" title="Close Form"><div id="apankFormConfirmError"></div>'+_button);
		$('#apankFormConfirmError').html(set['title']);
		$('#_closePopUp').focus()
	}

	makeCreateApankPopUpAlertErrorFocus = function(set){
		_button = '<p style="text-align: center; margin-top: -5px"><input id="_closePopUp" class="textButton" type="text" value="Close" style="padding: 2px; width: 60px; text-align: center" readonly></p>';
		$('#apankForm').html('<div id="apankConfirm_1" style="width: 100%;"></div>');
		$('#apankConfirm_1').html('<div id="apankConfirm_2" style="width: '+set['width']+'px;"></div>');
		$('#apankConfirm_2').html('<div id="apankForm_2" style="margin-top: '+set['top']+'px"></div>');
		$('#apankForm_2').html('<div id="apankForm_3"></div>');
		$('#apankForm_3').html('<div id="apankForm_close"></div>');
		$('#apankForm_close').html('<input type="button" id="apankFormCloseButton" title="Close Form"><div id="apankFormConfirmError"></div>'+_button);
		$('#apankFormConfirmError').html(set['title']);
		$('#_closePopUp').focus();

		$('#apankFormCloseButton').click(function(){
			$(set['focus']).focus();
		})
		$('#_closePopUp').click(function(){
			$(set['focus']).focus();
		})
		$('#_closePopUp').keyup(function(e){
			if(e.which === 13){
				$('#_closePopUp').focus();
				$(set['focus']).focus();
			}
		})
	}

	makeCreateApankPopUpAlertErrorPilihan = function(set){
		_button = '<p style="text-align: center; margin-top: -5px">';
		_button+= '<input id="_yaPopUp" type="text" value="Ya" class="textButton" style="padding: 2px; width: 60px; text-align: center" readonly>';
		_button+= '<input id="_closePopUp" type="text" value="Tidak" class="textButton" style="padding: 2px; width: 60px; text-align: center" readonly>';
		_button+= '</p>';
		
		$('#apankForm').html('<div id="apankConfirm_1" style="width: 100%;"></div>');
		$('#apankConfirm_1').html('<div id="apankConfirm_2" style="width: '+set['width']+'px;"></div>');
		$('#apankConfirm_2').html('<div id="apankForm_2" style="margin-top: '+set['top']+'px"></div>');
		$('#apankForm_2').html('<div id="apankForm_3"></div>');
		$('#apankForm_3').html('<div id="apankForm_close"></div>');
		$('#apankForm_close').html('<input type="button" id="apankFormCloseButton" title="Close Form"><div id="apankFormConfirmError"></div>'+_button);
		$('#apankFormConfirmError').html(set['title']);
		$('#_yaPopUp').focus();

		$('#apankFormCloseButton').click(function(){
			$(set['focus']).focus();
		})
		$('#_closePopUp').click(function(){
			$(set['focus']).focus();
			makePopUpNo();
		})
		$('#_closePopUp').keyup(function(e){
			if(e.which === 13){
				$('#_closePopUp').focus();
				$(set['focus']).focus();
				makePopUpNo();
			}
			if(e.which === 37){
				$('#_yaPopUp').focus();
			}
		})
		$('#_yaPopUp').keyup(function(e){
			if(e.which === 13){
				$('#_closePopUp').focus();
				$(set['focus']).focus();
				$('#apankForm').empty();
				makePopUpYes();
			}
			if(e.which === 39){
				$('#_closePopUp').focus();
			}
		})
		$('#_yaPopUp').click(function(){
			$('#apankForm').empty();
			$(set['focus']).focus();
			makePopUpYes();
		})
		
	}
})

