var ajax_div = {};
ajax_div.initialize_token = 0;
ajax_div.overlay_id = 'ajax-message-popup-window-mask';
ajax_div.loading_id = 'ajax-loading';
ajax_div.base_skin;
ajax_div.loading_w;
ajax_div.loading_h;
ajax_div.comfirmation_w;
ajax_div.comfirmation_h;
ajax_div.title;


ajax_div.zindex = 300;
ajax_div.wrapper = 'ajax-message-popup-window';
ajax_div.header = 'ajax-message-popup-head';
ajax_div.message = 'ajax-message-popup-message';
ajax_div.content = 'ajax-message-popup-content';
ajax_div.response_content = 'ajax-message-popup-content-response';

ajax_div.addRayLayout = function(){
	if( !$("new_iframe") ){
		ifm=document.createElement( "iframe" );
		ifm.id="new_iframe";
		ifm.style.position = "absolute";
		ifm.style.top = "0px";
		ifm.style.left = "0px";
		ifm.style.width = "100%";
		ifm.style.height = '0px';
		
		document.body.appendChild(ifm);
	}else{
		$("new_iframe").style.display = '';
	}
   
	if( !$("zoomimglayer_bg") ){
		div = document.createElement('div');
		div.id = 'zoomimglayer_bg';
		div.style.position = 'absolute';
		div.style.left = div.style.top = '0px';
		div.style.zIndex = '10001';
		div.style.width = '100%';
		div.style.height = document.body.scrollHeight + 'px';
		div.style.backgroundColor = '#000';		
		div.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=70,finishOpacity=100,style=0)';
		div.style.opacity = 0.7;
		document.body.appendChild(div);
	}else{
		$("zoomimglayer_bg").style.display = '';
	}
}

ajax_div.removeRayLayout = function(){
	if( $("new_iframe") ){
		$("new_iframe").style.display = "none";
	}
	
	if( $("zoomimglayer_bg") ){
		$("zoomimglayer_bg").style.display = "none";
	}
	
}

ajax_div.loading = function(){
	if( !$(ajax_div.loading_id) ){
		$j('#'+ajax_div.content).append("<div class=\""+ajax_div.loading_id+"\" id=\""+ajax_div.loading_id+"\" style=\"display: none;\"><img src=\""+ajax_div.base_skin+"images/opc-ajax-loader.gif\"></div>");
		$j('#'+ajax_div.content).append("<div class=\""+ajax_div.response_content+"\" id=\""+ajax_div.response_content+"\"></div>");
	}
	
	$(ajax_div.wrapper).style.display = '';	
	$(ajax_div.loading_id).style.display = '';	
	
}

ajax_div.hideLoading = function(){
	if( $(ajax_div.loading_id) ){
		$(ajax_div.loading_id).style.display = '';	
	}	
}

ajax_div.overlay = function( display ){	
	if( display )
		ajax_div.addRayLayout();
	else ajax_div.removeRayLayout();
	/*
	if( !$(ajax_div.overlay_id) ){		
		$j('body').append("<div class=\""+ajax_div.overlay_id+"\" id=\""+ajax_div.overlay_id+"\" style=\"display: none;\">&nbsp;</div>");
	}
	
	if( display )
		$(ajax_div.overlay_id).style.display = '';
	else $(ajax_div.overlay_id).style.display = 'none';
	*/
	
}


ajax_div.init = function(){
	ajax_div.overlay( 1 );	
	if( !ajax_div.initialize_token ){
		//append overlay.
		$j('body').append("<div class=\""+ajax_div.wrapper+"\" id=\""+ajax_div.wrapper+"\" style=\"display: none;\"></div>");
		$j("#"+ajax_div.wrapper).append("<div class=\""+ajax_div.header+"\" id=\""+ajax_div.header+"\"><span class=\"title\">"+ajax_div.title+"</span><a class=\"close\" onclick=\"ajax_div.hide( ajax_div.wrapper );ajax_div.overlay(0);\"><img src=\""+ajax_div.base_skin+"images/btn_window_close.gif\"></a></div>");
		$j("#"+ajax_div.wrapper).append("<div class=\""+ajax_div.message+"\" id=\""+ajax_div.message+"\"></div>");
		$j("#"+ajax_div.wrapper).append("<div class=\""+ajax_div.content+"\" id=\""+ajax_div.content+"\"></div>");
		ajax_div.initialize_token = 1;
		ajax_div.center(ajax_div.wrapper);
	}
	ajax_div.hide( ajax_div.message );
	
}

ajax_div.hide = function( div ){
	$(div).style.display = 'none';
	
}

ajax_div.showResponse = function( response ){
	ajax_div.hide( ajax_div.loading_id );
	if( $(ajax_div.overlay_id).style.display != 'none' ){
		if( response.responseText.isJSON() ){		
			var obj = response.responseText.evalJSON();
			if( ajax_div.message ){
				ajax_div.display( ajax_div.message );
				$(ajax_div.message).innerHTML = obj.message;
			}
			
			if( obj.message_level != 'error' ){			
				$(ajax_div.response_content).innerHTML = '';
			}
			
			if( obj.js != '' ){
				eval( obj.js );
			}
		}else{	
			$(ajax_div.response_content).innerHTML = '';
			$j('#'+ajax_div.response_content).append(response.responseText);
		}
	}
}

ajax_div.get = function( url ){
	ajax_div.overlay(1);
	ajax_div.loading();	
	
	//alert(url);
	var myAjax = new Ajax.Request(
	url,
	{
		method: 'post',
		postBody: '',
		onException: function (xhr, e)
		{
			alert('Exception : ' + e);
		},
		onComplete: function (xhr)
		{			
			ajax_div.showResponse(xhr);
		}

	});
}

ajax_div.div = function( id ){
	
	ajax_div.overlay(1);
	ajax_div.loading();	
	
	ajax_div.hide( ajax_div.loading_id );
	if( $(ajax_div.overlay_id).style.display != 'none' ){		
		ajax_div.display( ajax_div.message );
		$(ajax_div.message).innerHTML = $(id).innerHTML;			
	}
}

ajax_div.submit = function( form ){
	var dataForm = new VarienForm(form, true);
	if(dataForm.validator && dataForm.validator.validate()){
		ajax_div.overlay(1);
		ajax_div.loading();
		
		url = $(form).action;
		var myAjax = new Ajax.Request(
		url,
		{
			method: 'post',
			postBody: $(form).serialize(),
			parameters : Form.serialize(form),
			onException: function (xhr, e)
			{
				alert('Exception : ' + e);
			},
			onComplete: function (xhr)
			{
				ajax_div.showResponse(xhr);
			}

		});
	}else{
		return false;
	}	
}



ajax_div.display = function( div ){
	$(div).style.display = '';
}


ajax_div.center = function (element) {	
    if($(element) != null) {

          // retrieve required dimensions
            var el = $(element);
            var elDims = el.getDimensions();
            var browserName=navigator.appName;
            if(browserName==="Microsoft Internet Explorer") {

                if(document.documentElement.clientWidth==0) {
                    //IE8 Quirks
                    //alert('In Quirks Mode!');
                    var y=(document.viewport.getScrollOffsets().top + (document.body.clientHeight - elDims.height) / 2);
                    var x=(document.viewport.getScrollOffsets().left + (document.body.clientWidth - elDims.width) / 2);
                }
                else {
                    var y=(document.viewport.getScrollOffsets().top + (document.documentElement.clientHeight - elDims.height) / 2);
                    var x=(document.viewport.getScrollOffsets().left + (document.documentElement.clientWidth - elDims.width) / 2);
                }
            }
            else {
                // calculate the center of the page using the browser andelement dimensions
                var y = Math.round(document.viewport.getScrollOffsets().top + ((window.innerHeight - $(element).getHeight()))/2);
                var x = Math.round(document.viewport.getScrollOffsets().left + ((window.innerWidth - $(element).getWidth()))/2);
            }
			
            // set the style of the element so it is centered
            var styles = {
                position: 'absolute',
                top: y + 'px'
                //left : x + 'px'
            };
			
            el.setStyle(styles);			
			$j("#"+element).css( "z-index",ajax_div.zindex++ );
			
     }	 
}


ajax_div.centerWindow = function (element) {
	if( !$( ajax_div.conformation ) ){
		$j('body').append("<div class=\""+ajax_div.conformation+"\" id=\""+ajax_div.conformation+"\" style=\"display: none;\"><div class=\"tool-bar\"><a class=\"close\" onclick=\"ajax_div.hide( ajax_div.conformation );ajax_div.overlay(0);\"><img src=\""+ajax_div.base_skin+"images/btn_window_close.gif\"></a></div><div class=\"confirmation_content\"></div></div>");
	}
	
	if($(element) != null) {
		$j("#"+ajax_div.conformation + " .confirmation_content").append( $j("#"+element).html() );		
	}
	
    if($(element) != null) {

          // retrieve required dimensions
            var el = $(element);
            var elDims = el.getDimensions();
            var browserName=navigator.appName;
            if(browserName==="Microsoft Internet Explorer") {

                if(document.documentElement.clientWidth==0) {
                    //IE8 Quirks
                    //alert('In Quirks Mode!');
                    var y=(document.viewport.getScrollOffsets().top + (document.body.clientHeight - elDims.height) / 2);
                    var x=(document.viewport.getScrollOffsets().left + (document.body.clientWidth - elDims.width) / 2);
                }
                else {
                    var y=(document.viewport.getScrollOffsets().top + (document.documentElement.clientHeight - elDims.height) / 2);
                    var x=(document.viewport.getScrollOffsets().left + (document.documentElement.clientWidth - elDims.width) / 2);
                }
            }
            else {
                // calculate the center of the page using the browser andelement dimensions
                var y = Math.round(document.viewport.getScrollOffsets().top + ((window.innerHeight - $(element).getHeight()))/2);
                var x = Math.round(document.viewport.getScrollOffsets().left + ((window.innerWidth - $(element).getWidth()))/2);
            }
			
            // set the style of the element so it is centered
            var styles = {
                position: 'absolute',
                top: y + 'px',
                left : x + 'px'
            };
			
            $( ajax_div.conformation ).setStyle(styles);			
			$j("#"+ajax_div.conformation).css( "z-index",ajax_div.zindex++ );
			
     }
	 
	ajax_div.display( ajax_div.conformation );
}
