
var $j = jQuery.noConflict();


$j.modal.defaults.position = ['25%'];
$j.modal.defaults.minWidth = 400;
$j.modal.defaults.escClose = false;

 

$j.blockUI.defaults.css = {
	padding:        10,
	margin:         0,
	width:          '30%',
	top:            '40%',
	left:           '35%',
	textAlign:      'center',
	color:          '#000',
	border:         '3px solid #000',
	backgroundColor:'#fff',
	cursor:         'wait'
};

$j.blockUI.defaults.overlayCSS = {
	backgroundColor:'#000',  
	opacity:        '0.5'  
};

$j.blockUI.defaults.message = 'please wait...'; 

function handlePageFormSubmit(form){
	$j.blockUI();
	form.submit();
}
function handleEmailFormSubmit(form){
	$j('form.validatedForm').append('<input type="hidden" id="sendmail" name="sendmail" value="true"/>');
	$j.blockUI();
	form.submit();
}

function doSearch(strSearchURL){
	$j.blockUI({message: 'searching...'});
	$j.ajax({
	  url: strSearchURL,
	  cache: false,
	  type: 'post',
	  success: function(data){
		$j('#searchpanelresults').html(data);
		initSearchPrevNextActions();
		if ($j('#searchpanelcontainer').css('display')=='none'){
			$j('#searchpanelcontainer').show('slide', {direction: 'up'}, 'slow', function(){
				$j(document).bind('keydown', 'esc', function(){
					$j('#searchpanelcontainer').hide('slide', {direction: 'up'}, 'slow', function(){
						$j(document).unbind('keydown', 'esc');
					});
				});
			});
		}
	  }
	});
}

function showLogin(){
	
			$j('#loginpanel').modal({onOpen: formModalOpen, onClose: cancelModal, escClose: false});

/*	
	$j.blockUI({message: 'null'});
	if ($j('#loginpanelcontainer').css('display')=='none'){
		$j('#loginpanelcontainer').show('slide', {direction: 'up'}, 'slow', function(){
			$j(document).bind('keydown', 'esc', function(){
				$j('#loginpanelcontainer').hide('slide', {direction: 'up'}, 'slow', function(){
					$j(document).unbind('keydown', 'esc');
				});
			});
		});
	}
*/
}

/*
function doSearch(strSearchURL){
	$j.blockUI({message: 'searching...'});
	$j('#searchpanelresults').load(strSearchURL, function(){
		initSearchPrevNextActions();
		if ($j('#searchpanelcontainer').css('display')=='none'){
			$j('#searchpanelcontainer').show('slide', {direction: 'up'}, 'slow', function(){
				$j(document).bind('keydown', 'esc', function(){
					$j('#searchpanelcontainer').hide('slide', {direction: 'up'}, 'slow', function(){
						$j(document).unbind('keydown', 'esc');
					});
				});
			});
		}
	});
}
*/
function doSearchPage(strSearchQueryString){
	$j.blockUI({message: null, overlayCSS: {backgroundColor: '#ffffff', opacity: '0'}});
	$j.ajax({
		url: '/lib/jsp/request/do_search.jsp?'+strSearchQueryString,
		cache: false,
		success: function(data){
			$j('#searchpanelresults').html(data);
			initSearchPrevNextActions();
		}
	});

	
}

function initSearchPrevNextActions(){
	$j.unblockUI();
	$j('#searchpanelresults a.google_mini_prev_next').each(function(i){
		$j(this).click(function(e){
			e.preventDefault();
			doSearchPage($j(this).attr('href'));
		});
	});
}

function getFormModal(strURL){
	$j.ajax({
		url: strURL, 
		cache: false,
		processData: false,
		success:function(responseText){
			openModal(responseText);
		}
	});
}

function openModal(responseText){
	if ($j(responseText).is('div')) {
		$j('<span>'+responseText+'</span>').modal({onOpen: ajaxFormModalOpen, onClose: cancelModal, escClose: false});
	} else {
		$j('body').append(responseText);	
	}
}

function ajaxFormModalOpen(dialog){
	dialog.overlay.show(1, function (){
		dialog.data.show(1, function(){	 
			dialog.container.fadeIn('fast', function (){
				$j("#simplemodal-container form").validate({
					rules: {
						new_password:'required rmdpassword',
						confirm_new_password:'required rmdpassword',
						confirm_new_password:{equalTo:'#new_password'},
						new_email:'required email',
						confirm_new_email:'required email',
						confirm_new_email:{equalTo:'#new_email'},
						j_username: 'required email',
						j_password: 'required rmdpassword',
						email: 'required email',
						verification_code: 'required'
					},
//					submitHandler: function(form) {
//						$(form).ajaxSubmit();
//					}
					submitHandler: handleFormSubmit
				});


				$j('#simplemodal-container #redirecttologin').click(redirectToLogin);
				$j('#simplemodal-container #redirecttohome').click(redirectToHome);
				$j('#simplemodal-container #reloadpage').click(reloadPage);
				$j('#simplemodal-container #submitform').click(submitForm);
				$j('#simplemodal-container #cancelform').click(simplyCloseModal);
				
//				$j('#simplemodal-container .returnkeybutton[id=cancelform]').bind('keydown', 'return', simplyCloseModal);
//				$j('#simplemodal-container .returnkeybutton[id=redirecttologin]').bind('keydown', 'return', redirectToLogin);
//				$j('#simplemodal-container .returnkeybutton[id=reloadpage]').bind('keydown', 'return', reloadPage);
//				$j('#simplemodal-container .returnkeybutton[id=redirecttohome]').bind('keydown', 'return', redirectToHome);

//				$j('#simplemodal-container .escapekeybutton[id=cancelform]').bind('keydown', 'esc', simplyCloseModal);
//				$j('#simplemodal-container .escapekeybutton[id=reloadpage]').bind('keydown', 'esc', reloadPage);
//				$j('#simplemodal-container .escapekeybutton[id=redirecttohome]').bind('keydown', 'esc', redirectToHome);


//					if ($j('#simplemodal-container .returnkeybutton').attr('id')=='submitform'){
//						$j(document).bind('keydown', 'return', submitForm);


				if ($j('#simplemodal-container .returnkeybutton').length){

					if ($j('#simplemodal-container .returnkeybutton').attr('id')=='submitform'){
						$j(document).bind('keydown', 'return', submitForm);
					} else if ($j('#simplemodal-container .returnkeybutton').attr('id')=='cancelform'){
						$j(document).bind('keydown', 'return', simplyCloseModal);
					}else if ($j('#simplemodal-container .returnkeybutton').attr('id')=='redirecttologin'){
						$j(document).bind('keydown', 'return', redirectToLogin);
					}else if ($j('#simplemodal-container .returnkeybutton').attr('id')=='reloadpage'){
						$j(document).bind('keydown', 'return', reloadPage);
					}else if ($j('#simplemodal-container .returnkeybutton').attr('id')=='redirecttohome'){
						$j(document).bind('keydown', 'return', redirectToHome);
					}
				}
				if ($j('#simplemodal-container .escapekeybutton').length){
					if ($j('#simplemodal-container .escapekeybutton').attr('id')=='cancelform'){
						$j(document).bind('keydown', 'esc', simplyCloseModal);
					}else if ($j('#simplemodal-container .escapekeybutton').attr('id')=='reloadpage'){
						$j(document).bind('keydown', 'esc', reloadPage);
					}else if ($j('#simplemodal-container .escapekeybutton').attr('id')=='redirecttohome'){
						$j(document).bind('keydown', 'esc', redirectToHome);
					}
				}
				

				$j('#captchaimage').click(function(e){
					e.preventDefault();
					$j.post('/lib/jsp/request/captcha.jsp', function(data){
						$j('#captchaimage').attr('src', data);
					});
				});
				$j('#simplemodal-container').draggable({handle:'.header', delay:'10', containment:'body', cursor:'move'});
				$j('#simplemodal-container form :input:first').focus();
			});
		});
	});
}


function formModalOpen(dialog){
	dialog.overlay.show(1, function (){
		dialog.data.show(1, function(){	 
			dialog.container.fadeIn('fast', function (){
				$j("#simplemodal-container form").validate({
					rules: {
						j_username: 'required email',
						j_password: 'required rmdpassword'
					}
				});
				$j('#simplemodal-container #submitform').click(submitForm);
				$j('#simplemodal-container #cancelform').click(simplyCloseModal);
				
//				$j('#simplemodal-container .returnkeybutton[id=cancelform]').bind('keydown', 'return', simplyCloseModal);
//				$j('#simplemodal-container .returnkeybutton[id=redirecttologin]').bind('keydown', 'return', redirectToLogin);
//				$j('#simplemodal-container .returnkeybutton[id=reloadpage]').bind('keydown', 'return', reloadPage);
//				$j('#simplemodal-container .returnkeybutton[id=redirecttohome]').bind('keydown', 'return', redirectToHome);

//				$j('#simplemodal-container .escapekeybutton[id=cancelform]').bind('keydown', 'esc', simplyCloseModal);
//				$j('#simplemodal-container .escapekeybutton[id=reloadpage]').bind('keydown', 'esc', reloadPage);
//				$j('#simplemodal-container .escapekeybutton[id=redirecttohome]').bind('keydown', 'esc', redirectToHome);


//					if ($j('#simplemodal-container .returnkeybutton').attr('id')=='submitform'){
//						$j(document).bind('keydown', 'return', submitForm);


				if ($j('#simplemodal-container .returnkeybutton').length){

					if ($j('#simplemodal-container .returnkeybutton').attr('id')=='submitform'){
						$j(document).bind('keydown', 'return', submitForm);
					} else if ($j('#simplemodal-container .returnkeybutton').attr('id')=='cancelform'){
						$j(document).bind('keydown', 'return', simplyCloseModal);
					}
				}
				if ($j('#simplemodal-container .escapekeybutton').length){
					if ($j('#simplemodal-container .escapekeybutton').attr('id')=='cancelform'){
						$j(document).bind('keydown', 'esc', simplyCloseModal);
					}
				}
				$j('#simplemodal-container').draggable({handle:'.header', delay:'10', containment:'body', cursor:'move'});
				$j('#simplemodal-container form :input:first').focus();
			});
		});
	});
}




function simplyCloseModal(e){
	e.preventDefault();
	closeModal();
}

function closeModal(){
	$j(document).unbind('keydown', 'esc');
	$j(document).unbind('keydown', 'return');
	$j.modal.close();
}

function cancelModal(){
	var bReloadPage = $j('#simplemodal-container .escapekeybutton').attr('id')=='reloadpage'
	closeModal();
	if (bReloadPage) {
		$j.blockUI({message: 'reloading page...'});
		winOpen(g_strCurrentURL, '_self');
	}
}



function redirectToLogin(e){
	e.preventDefault();
	closeModal();
	showLogin();
//	$j.blockUI({message: 'redirecting to login page...'});
//	winOpen(g_strLoginURL, '_self');

//	window.open(g_strLoginURL, '_self');
}
function redirectToHome(e){
	e.preventDefault();
	closeModal();
//	$j.modal.close();
	$j.blockUI({message: 'redirecting to home page...'});
	winOpen(g_strHomePageURL, '_self');
//	window.open(g_strHomePageURL, '_self');
}
function reloadPage(e){
	e.preventDefault();
	closeModal();
//	$j.modal.close();
	$j.blockUI({message: 'reloading page...'});
	winOpen(g_strCurrentURL, '_self');
//	window.open(g_strCurrentURL, '_self');
}
function submitForm(e){
	if (e) {
		e.preventDefault();
	}
	$j('#simplemodal-container form').submit();
}
function handleFormSubmit(form){
//	var jForm = $j(form);
	var options = { 
		beforeSubmit: showRequest,  // pre-submit callback 
		success: showResponse,  // post-submit callback 
		type: 'post',
//		clearForm: true,
//		resetForm: true
		// other available options: 
		//url:       url         // override for form's 'action' attribute 
		//type:      type        // 'get' or 'post', override for form's 'method' attribute 
		//dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
		clearForm: true,        // clear all form fields after successful submit 
		resetForm: true        // reset the form after successful submit 
 
		// $.ajax options can be used here too, for example: 
		//timeout:   3000 
	}; 
	// bind form using 'ajaxForm' 
//	 $j(form).ajaxForm(options); 
	$j(form).ajaxSubmit(options);
	return false;
}

// pre-submit callback 
function showRequest(formData, jqForm, options){ 
//	alert(jqForm.val());
    // formData is an array; here we use $.param to convert it to a string to display it 
    // but the form plugin does this for you automatically when it submits the data 
//    var queryString = $j.param(formData); 
 
    // jqForm is a jQuery object encapsulating the form element.  To access the 
    // DOM element for the form do this: 
    // var formElement = jqForm[0]; 
//	$j.modal.close();
//	jqForm.ajaxSubmit(options);

	closeModal();
//	$j.modal.close();
	$j.blockUI();

//	$j.blockUI();
//	$j('#simplemodal-container .modalData').block(); 
	
//	modalClearBindings();
//	modalHideContent();
//    alert('About to submit: \n\n' + queryString); 
 
    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue 
    return true; 
} 
 
// post-submit callback 
function showResponse(responseText, statusText, dataType)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
	$j.unblockUI();
 	openModal(responseText);
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
 
//    alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
//       '\n\nThe output div should have already been updated with the responseText.'); 
} 

function doAutoReload(strURL, strWaitMessage) {
	if (strWaitMessage) {
		$j.blockUI({message: strWaitMessage}); 
	}
	winOpen(strURL, '_self');
//	window.open(strURL, '_self');
}

function winOpen(strUrl, strTarget) {
	window.setTimeout(doWinOpen(strUrl, strTarget), 1000);
}

function doWinOpen(strUrl, strTarget){
   return function(){   // no  params
      window.open(strUrl, strTarget);
   }
}

function rotateFeatureMessage() {
	window.setTimeout(doRotation, 7000);
}

function doRotation() {
	if ($j('div#message2').css('display')=='none') {
		$j('div#message1').fadeOut(300, function(){$j('div#message2').fadeIn(1500, function(){rotateFeatureMessage();});});
	} else {
		$j('div#message2').fadeOut(300, function(){$j('div#message1').fadeIn(1500, function(){rotateFeatureMessage();});});
	}
}

//function LanguageStrings(strLanguageCode) {
//		Default Messages Strings in jquery.validate - This function not limited to these strings
//		required: 'This field is required',
//		remote: "Please fix this field.",
//		email: "Please enter a valid email address.",
//		url: "Please enter a valid URL.",
//		date: "Please enter a valid date.",
//		dateISO: "Please enter a valid date (ISO).",
//		dateDE: "Bitte geben Sie ein gültiges Datum ein.",
//		number: "Please enter a valid number.",
//		numberDE: "Bitte geben Sie eine Nummer ein.",
//		digits: "Please enter only digits",
//		creditcard: "Please enter a valid credit card number.",
//		equalTo: "Please enter the same value again.",
//		accept: "Please enter a value with a valid extension.",
//		maxlength: $.validator.format("Please enter no more than {0} characters."),
//		minlength: $.validator.format("Please enter at least {0} characters."),
//		rangelength: $.validator.format("Please enter a value between {0} and {1} characters long."),
//		range: $.validator.format("Please enter a value between {0} and {1}."),
//		max: $.validator.format("Please enter a value less than or equal to {0}."),
//		min: $.validator.format("Please enter a value greater than or equal to {0}.")
//	switch(strLanguageCode)
//	{
//	case 'nor':
//		this.strings = new Object;
//		this.strings.languagecode = strLanguageCode;
//		this.strings.validator = new Object;
//		this.strings.validator.email_message = 'Din epostadresse MÅ ha formatet navn@domene.no';
//		this.strings.validator.email_username_message = 'Ditt brukernavn må være en epostadresse med formatet navn@domene.no';
//		this.strings.validator.required = 'Dette feltet MÅ fylles ut';
//		this.strings.validator.minlength = 'Vennligst benytt minimum {0} tegn';
//		break;
//	default:
//		this.strings = new Object;
//		this.strings.languagecode = strLanguageCode;
//		this.strings.validator = new Object;
//		this.strings.validator.email_message = 'Your email address must be in the format of name@domain.com';
//		this.strings.validator['email_username_message'] = 'Your username must be an email address in the format of name@domain.com';
//		this.strings.validator.required = 'This field is required.';
//		this.strings.validator.minlength = 'Please enter at least {0} characters.';
//	}
//}


//function LocaleStrings(strLocaleCode) {
//	switch(strLocaleCode)
//	{
//	case 'au':
//		this.strings = new Object;
//		break;
//	default:
//		this.strings = new Object;
//	}
//}

//function CountryStrings(strCountryCode) {
//	switch(strCountryCode)
//	{
//	case 'au':
//		this.strings = new Object;
//		break;
//	default:
//		this.strings = new Object;
//	}
//}


