/*
 * ALERT MESSAGE
 * Create a alert message
 * Required : <p class="ajax_warn"></p>
 * Position of .ajax_warn defined by css
 */
function alertMessage(msg){
	$messageBox = $("p.ajax_warn");
	$messageBox.text(msg);
	$messageBox.fadeIn(1000).fadeOut(1000);
}



/*
 * CLOSE BUTTON
 * Bind close function for element with ID btn_close (#btn_close)
 * ID : #btn_close
*/

function closePopup() {
	$("input#btn_close").each( function() {
		$(this).click(function() {
			self.close();
		});
	});
}

/*
 * POPUP WINDOW
 * Bind open new window function for element with class popup_[width]x[height]
 * Class: popup_[Width]x[Height]
 * To create popup 800x600, use class: popup_800x600
*/

function openPopup(href) {
	$("a[class^=popup_]").each( function() {
		$(this).click(function() {
			
			var winpop;
			var xurl = href || $(this).attr('href');
			var xtarget = '_blank';
			var thisClass = $(this).attr('class').split(' ');
			var re = new RegExp('^(popup_)');
			for(i=0;i<thisClass.length;i++) {
				if(thisClass[i].match(re)) {
					popSizes = thisClass[i].split('_');
					popSize = popSizes[1].split('x');
					popWidth = popSize[0];
					popHeight = popSize[1];
				}
			}
			
			openWindow(xurl, popWidth, popHeight);
			return false;
		});
	});
}

function openWindow(url, width, height) {
	var thisWindow;
 	if(thisWindow!=null && !thisWindow.closed){
		 thisWindow.close() }
		 thisWindow = window.open(url, '_blank','width=' + width + ',height=' + height + ',resizable=0,scrollbars=yes,menubar=no,status=no' );
		 thisWindow.focus();
}

/*
 * SELECT ELEMENT RELATED FUNCTION
 * 
 */
function renderSelect(target,arrValue,arrText,valueNull,valueText){

	var dataLength = arrValue.length;
	var optionField = getNullOption(valueNull,valueText);

	for(i=0; i<dataLength; i++){
		optionField += '<option value="' + arrValue[i] + '">' + arrText[i] + '</option>';
	}

	$("#" + target).html('').html(optionField);

}

function resetSelect(id,valueNull,textNull){

	var selectOption = getNullOption(valueNull,textNull);

	$("#" + id).html('').html(selectOption);

}


function getNullOption(valueNull,textNull){
	return '<option value="' + valueNull + '">' + textNull + '</option>';
}


// http://www.learningjquery.com/2007/10/improved-animated-scrolling-script-for-same-page-links
// major changes by Paul Armstrong and Zachary Johnson
function enable_smooth_scroll() {
    function filterPath(string) {
        return string
                .replace(/^\//,'')
                .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
                .replace(/\/$/,'');
    }

    var locationPath = filterPath(location.pathname);

    var scrollElement = 'html, body';
    $('html, body').each(function () {
        var initScrollTop = $(this).attr('scrollTop');
        $(this).attr('scrollTop', initScrollTop + 1);
        if ($(this).attr('scrollTop') == initScrollTop + 1) {
            scrollElement = this.nodeName.toLowerCase();
            $(this).attr('scrollTop', initScrollTop);
            return false;
        }    
    });
    
    $('a[href*=#]').each(function() {
        var thisPath = filterPath(this.pathname) || locationPath;
        if  (   locationPath == thisPath
                && (location.hostname == this.hostname || !this.hostname)
                && this.hash.replace(/#/, '')
            ) {
                if ($(this.hash).length) {
                    $(this).click(function(event) {
                        var targetOffset = $(this.hash).offset().top;
                        var target = this.hash;
                        event.preventDefault();
                        $(scrollElement).animate({scrollTop: targetOffset}, 500, function() {
                            location.hash = target;
                        });
                    });
                }
        }
    });
}