/*
 * CamargueWindow 0.2
 *
 * Copyright (c) 2010 Camargue Srl
 *
 */

var CamargueWindow = function(settings) {

	// settings
	var iTheme = (settings.theme==null) ?  'inspector' : 'classic';
	var iTitle = settings.title;
	var iDescr = settings.descr;
	var iContentHtml = settings.contentHtml; 
	var iContentWidth = settings.contentWidth;
	var iContentHeight = settings.contentHeight; 
	var iPosX = settings.posX;
	var iPosY = settings.posY;
	var $refObj = $(settings.refObj);
	var iId = 'CamargueWindow_' + (Math.ceil(Math.random() * 1000));
	var iOffesetY = 30;
	var iOffesetX = 20;
	var iContentType = 'inline';
	// if iframe..
	if ( iContentHtml.toLowerCase().indexOf('<iframe') == 0 ) {
		iContentType = 'iframe';
			var iOffesetY = 60;
	}
	// create & show inspector
	this.open = function () {
		// alert(iContentWidth + '/' + iContentHeight);
    // alert(iContentHtml);
		// build html
		var html = '';
		html += '<div class="camargue_window '+iTheme+'">';
		html += '<div class="title">';
		html += '<p>' + iTitle + ' <a class="close" title="close window">close</a></p>'; // TITLE
		html += '</div>'; // .camargue_title
		html += '<div class="body ' + iContentType + '">'; // if not inline remove this class!!
		html += '<!-- content -->';
		html += iContentHtml; // define if is inline or iframe...
		//html += '<!-- buttons -->';
		//html += '<p><a class="button bold"><span>Delete and continue</span></a> <a class="button"><span>Cancel</span></a></p>'; // actions: to set behaviours...
		html += '</div>'; // .camargue_body
		html += '<div class="footer">';
		html += '<p>' + iDescr + '</p>'; // DESCR
		html += '</div>'; // .camargue_footer
		html += '</div>'; // .camargue_window
		
		var inspector = $(html); // make jquery object
		$(inspector).attr('id',iId);  // set the attribute 
		$('body').append(inspector); // put it into the body
		
		// set dimensions
		if (iContentType=='iframe') {
			$(inspector).find('iframe').width(iContentWidth);
			$(inspector).find('iframe').height(iContentHeight);
		}else{
			$(inspector).width(iContentWidth);
			$(inspector).height(iContentHeight);
		}
		
		// set position
		if ( iPosY == null) {
			// height:
			if ( $refObj.offset().top - $(window).scrollTop() > iContentHeight + iOffesetY ) {
				// on top
				iPosY = $refObj.offset().top - iContentHeight - iOffesetY;
			}else{
				// on center
				iPosY = $refObj.offset().top - (iContentHeight/2);
				if ( iContentHeight + iOffesetY < $(window).height() - ( $refObj.offset().top - $(window).scrollTop() - $refObj.height() ) ) {
					// on bottom
					iPosY = $refObj.offset().top + $refObj.height() + iOffesetY;
					
				}
			}
		}
		if ( iPosX == null) {
			// width:
			if ( iContentWidth + iOffesetX < $(window).width() - $refObj.offset().left - $(window).scrollLeft() - $refObj.width() ) {
				// on right
				iPosX = $refObj.offset().left + $refObj.width() + iOffesetX;
			}else{
				// on center
				iPosX = $refObj.offset().left + ($refObj.width()/2) - (iContentWidth/2);
				if ( $refObj.offset().left - $(window).scrollLeft() > iContentWidth + iOffesetX ) {
					// on left
					iPosX = $refObj.offset().left - iContentWidth - iOffesetX;
				}
			}
		}
		
		// show!
		$(inspector).offset({ top: parseInt(iPosY), left: parseInt(iPosX) }).show('drop',{},300,function(){_makeDraggable();});
		
		// assign close behaviour
		$(inspector).find('.title a.close').click(this.close);
		
		// callback
		if ($.isFunction(settings.onOpen)) {
			settings.onOpen();
		}
		
	} // end: open()
	
	// destroy & hide inspector
	this.close = function () {

		// set inspector
		var inspector = $('#' + iId);
		
		// hide!
		$(inspector).effect('transfer', { to: $refObj, className: 'effects-transfer' }, 500).fadeOut('fast', function(){
			// destroy
			$(inspector).remove();
			// callback
			if ($.isFunction(settings.onClose)) {
				settings.onClose();
			}
		});
	
	} // end: close()
	
	// denied
	this.denied = function () {
		var inspector = $('#' + iId);
		$(inspector).effect('shake', { }, 100, function(){_makeDraggable();} );
	}
	
	// replace contents
	this.replaceContents = function (options) {
		// fadeout and remove contents
		// fadein new contents
		// new position
		// new dimension
	}
	
	// functions:
	function _makeDraggable(){
		$('div.camargue_window').draggable({
			opacity: 0.7,
			handle: 'div.camargue_title',
			iframeFix: true,
			containment: 'document',
			stack: { group: '.camargue_window', min: 10000 },
			start: function() {
				$(this).css({position:'absolute'});
			},
			stop: function(){
				offsetTop = $(this).offset().top - $(document).scrollTop();
				$(this).css({position:'fixed', top:offsetTop});
			}
		});
	}	
	
} // end: CamargueWindow()
