var fde_window_content_id=0, fde_ask_bid_timer= new Array(), fde_ask_bid_refresh_time=new Array(),
fde_ask_bid_block_refresh_time, fde_ask_bid_block_timer,
fde_flash_graphic_1=new Array(), fde_flash_graphic_2=new Array(), fde_symbols=new Array();
var getdata_url='http://quotes.nordfx.com/getdata2.php';

jQuery.fn.center = function () {
    this.css("position","absolute");
	v=((jQuery(window).height() - this.outerHeight()) / 2) + jQuery(window).scrollTop();
    this.css("top",  v + "px");
	v=((jQuery(window).width() - this.outerWidth()) / 2) + jQuery(window).scrollLeft();
    this.css("left", v+ "px");
    return this;
}

function fde_show_window(symbol)
{
	var el, ret;
	
	var doc_client_height = document.body.clientHeight;
	var doc_width = document.body.scrollWidth;
	var doc_height = document.body.scrollHeight;
	var window_width = 520, window_height = 450, window_left, window_top;
	var doc_top = document.body.scrollTop;
	
	window_left = (doc_width/2 - window_width/2);
	//window_top = 50;//(doc_top+ doc_client_height/2 - window_height/2);	
	window_top = ((jQuery(window).height() - window_height) / 2) + jQuery(window).scrollTop();
	el = document.createElement('a');
	el.setAttribute('id', 'fde_contaner_id'+ fde_window_content_id);
	document.getElementById('fde_contaner_id').appendChild(el);
	jQuery('#'+ 'fde_contaner_id'+ fde_window_content_id).newWindow({
		 windowTitle: lang['askbid'],
		 content: 'ee',
		 
		 width: window_width, 
		 height: window_height,
		 posx: window_left,
		 posy: window_top
	});

	document.getElementById('fde_window_'+ fde_window_content_id).innerHTML = '<iframe id="frame11" src="http://quotes.nordfx.com/window.php?id='+symbol+'">';
	ret = fde_window_content_id;
	fde_window_content_id++;
	return ret;
}

function fde_refresh_ask_bid(id)
{
	if(document.getElementById('ask_bid_symbol_value_'+ id)!=null)
	{
		jQuery.ajax({
	  url: getdata_url+"?symbol="+document.getElementById('ask_bid_symbol_value_'+ id).value+"&ajax=1&do=ask_bid&r="+Math.random()*100,
	  type: 'GET',
	  success: function(data){
		var k='<table style=\"margin-top:3px;\" Border=\"0\" class=\"ask_bid_module_single\"><tr><th>'+lang['bid']+'</th><th>'+lang['size']+'</th><th>'+lang['ask']+'</th></tr>';
		var counter=0;
		k+=data;
			  k+='</table>';

	  
	  		jQuery("#nord_fx_contaner_"+ id).html(k);
			}
			 
	});
				 
		if(typeof(fde_symbols[id])=='undefined')
		{
			fde_symbols[id] = document.getElementById('ask_bid_symbol_value_'+ id).value;
		}
		
		if(fde_symbols[id]!=document.getElementById('ask_bid_symbol_value_'+ id).value)
		{
			if(typeof(fde_flash_graphic_1[id])!='undefined' || typeof(fde_flash_graphic_2[id])!='undefined')
			{
				fde_show_graphics(id, true);
			}
			fde_symbols[id] = document.getElementById('ask_bid_symbol_value_'+ id).value
		}
		else
		{
			if(typeof(fde_flash_graphic_1[id])!='undefined' || typeof(fde_flash_graphic_2[id])!='undefined')
			{
				document.getElementById('chart_1_' + id).rebuild();
			}
		}
	}
	return false;
}
function fde_ask_bid_refresh(id)
{
	var new_refresh_time = document.getElementById('ask_bid_refresh_value_'+id).value;
	if(
		typeof(fde_ask_bid_timer[id])=='undefined' 	|| 
		fde_ask_bid_timer[id]==false || 
		typeof(fde_ask_bid_refresh_time[id])=='undefined' || 
		fde_ask_bid_refresh_time[id]!=new_refresh_time
	)
	{
		fde_ask_bid_refresh_time[id]=new_refresh_time;
		clearInterval(window.fde_ask_bid_timer[id]);
		if(new_refresh_time!=0)
		{
			fde_ask_bid_timer[id] = window.setInterval("fde_refresh_ask_bid("+ id+ ")", fde_ask_bid_refresh_time[id]);
		}
		else
		{
			fde_ask_bid_timer[id] = false;
		}		
	}
	fde_refresh_ask_bid(id);
}
function fde_write_ask_bid_window_contaner(content_id,symbol)
{
	data = '<div>\n<table cellspacing="0" cellpadding="0" border="0">\n	<tr>\n		<td style="padding:2px;"><label for="ask_bid_symbol_value_{id}">'+lang['symbol']+'</label>:</td>\n		<td style="padding:2px;">	\n			<form method="post" id="ask_bid_symbol_{id}">\n				<select name="symbol" id="ask_bid_symbol_value_{id}" onchange="fde_ask_bid_refresh(\'{id}\');">\n				<option value="AUDCAD" >AUD/CAD</option><option value="AUDCHF" >AUD/CHF</option><option value="AUDJPY" >AUD/JPY</option><option value="AUDNZD" >AUD/NZD</option><option value="AUDUSD" >AUD/USD</option><option value="CHFJPY" >CHF/JPY</option><option value="EURCAD" >EUR/CAD</option><option value="EURCHF" >EUR/CHF</option><option value="EURGBP" >EUR/GBP</option><option value="EURJPY" >EUR/JPY</option><option value="EURNZD" >EUR/NZD</option><option value="EURUSD" >EUR/USD</option><option value="GBPCAD" >GBP/CAD</option><option value="GBPCHF" >GBP/CHF</option><option value="GBPJPY" >GBP/JPY</option><option value="GBPUSD" >GBP/USD</option><option value="NZDJPY" >NZD/JPY</option><option value="NZDUSD" >NZD/USD</option><option value="USDCAD" >USD/CAD</option><option value="USDCHF" >USD/CHF</option><option value="USDJPY" >USD/JPY</option>\n				</select>\n			</form>\n		</td>\n	</tr>\n	<tr>\n		<td style="padding:2px;"><label for="ask_bid_refresh_value_{id}">'+lang['refresh']+'</label>:</td>\n		<td style="padding:2px;"> \n			<select id="ask_bid_refresh_value_{id}" onchange="fde_ask_bid_refresh(\'{id}\');">\n				<option value="1000">1 '+lang['sec2']+'</option>\n				<option value="5000">5 '+lang['sec']+'</option>\n				<option value="10000">10 '+lang['sec']+'</option>\n				<option value="30000" selected>30 '+lang['sec']+'</option>\n				<option value="60000">1 '+lang['min2']+'</option>\n				<option value="0">'+lang['never']+'</option>\n			</select>\n		</td>\n	</tr>\n</table>\n</div>';
	data = data.replace(new RegExp('\{id\}', 'igm'), content_id);
	
	document.getElementById('fde_window_'+ content_id).innerHTML = 
	'<div style="margin-top:5px;text-align:center;">\
	<table cellspacing="0" cellpadding="0" border="0" width="100%" class="window-content">\
		<tr>\
			<td>\
	<center>'+ data+
	'<div id="nord_fx_contaner_'+ content_id+ '"></div>' +
	'</center>\
			</td>\
			<td>\
	<center>'+ 
	'<div id="flash_content_1_'+ content_id+ '"></div>\
	</center>\
			</td>\
		</tr>\
	</div>';
	fde_show_graphics(content_id, true,symbol);
}
function fde_show_graphics(id, rewrite,symbol)
{
	if(typeof(fde_flash_graphic_1[id])=='undefined' || rewrite)
	{
		var so = new SWFObject("http://quotes.nordfx.com/graphics/amcolumn.swf", "chart_1_"+ id, "250", "360", "8", "#FFFFFF");
		so.addVariable("path", "http://quotes.nordfx.com/graphics/");
		so.addVariable("settings_file", escape("http://quotes.nordfx.com/graphics/ask_bid_1/graph_properties.xml"));
		var myString2 = document.getElementById('ask_bid_symbol_value_'+ id).value;
		myString2 =  myString2.replace(/[^a-zA-Z 0-9]+/g,'');
		if (symbol) {
		myString2=symbol;
		}
		so.addVariable("data_file", escape("http://quotes.nordfx.com/graphics/ask_bid_1/"+ myString2+".xml"));
		so.addVariable("chart_id", "chart_1_"+ id);
		so.addParam("wmode", "opaque");
		so.addParam("allowScriptAccess", "always");
		so.write('flash_content_1_'+ id);
		
		fde_flash_graphic_1[id] = true;
	}
}
function showFlashPram(symbol)
{
	var content_id = fde_show_window(symbol);
	symbol =  symbol.replace(/[^a-zA-Z0-9]+/g,'');
	document.getElementById('ask_bid_symbol_value_'+ content_id).value=symbol;
	fde_ask_bid_refresh(content_id);
}
function fde_clear_interval(close_id)
{
	if((matches=close_id.match(new RegExp('fde_close_button_window_([\d]+)', 'im')))!=null)
	{
		close_id = matches[1];
		clearInterval(window.fde_ask_bid_timer[close_id]);
	}
}
function fde_ask_bid_block_refresh()
{
	var new_refresh_time = document.getElementById('ask_bid_block_refresh_value').value;
	if(
		typeof(fde_ask_bid_block_timer)=='undefined' 	|| 
		fde_ask_bid_block_timer==false || 
		typeof(fde_ask_bid_block_refresh_time)=='undefined' || 
		fde_ask_bid_block_refresh_time!=new_refresh_time
	)
	{
		fde_ask_bid_block_refresh_time=new_refresh_time;
		clearInterval(window.fde_ask_bid_block_timer);
		if(new_refresh_time!=0)
		{
			fde_ask_bid_block_timer = window.setInterval("fde_refresh_ask_bid_block();", fde_ask_bid_block_refresh_time);
		}
		else
		{
			fde_ask_bid_block_timer = false;
		}
		fde_refresh_ask_bid_block();
	}
}
function fde_refresh_ask_bid_block(){
	jQuery.ajax({
	  url: getdata_url+"?ajax=1&do=ask_bid&act=ask_bid_mini&r="+Math.random()*100,
	   type: 'GET',
	  success: function(data){
	  var ttt=data.responseText;
		var pos=ttt.search("<body>")+6;
		var pos2=ttt.search("</body>");
		ttt=ttt.substr(pos,pos2-pos);
		alert(ttt);
		jQuery("#ask_bid_block_contaner").html(ttt);
	  }
	});
}

/**
 *  jQuery Windows Engine Plugin
 *@requires jQuery v1.2.6
 *  http://www.socialembedded.com/labs
 *
 *  Copyright (c)  Hernan Amiune (hernan.amiune.com)
 *  Dual licensed under the MIT and GPL licenses:
 *  http://www.opensource.org/licenses/mit-license.php
 *  http://www.gnu.org/licenses/gpl.html
 * 
 *  Version: 0.6
 */
 

var jqWindowsEngineZIndex = 999999; 
(function(jQuery){ 

/**
 * @option string windowTitle, the tile to display on the window
 * @option HTML content, the content to display on the window
 * @option string ajaxURL, URL address to load the content, this has priority over content
 * @option int width, the initial width of the window
 * @option int height, the initial height of the window
 * @option int posx, the initial x position of the window
 * @option int posy, the initial y position of the window
 * @option function onDragBegin: onDragBegin callback function,
 * @option function onDragEnd: onDragEnd callback function,
 * @option function onResizeBegin: onResizeBegin callback function,
 * @option function onResizeEnd: onResizeEnd callback function,
 * @option function onAjaxContentLoaded: onAjaxContentLoaded callback function,
 * @option boolean statusBar, enable or disable the window status bar
 * @option boolean minimizeButton, enable or disable the window minimize button
 * @option HTML minimizeIcon, an html text to display as the minize icon
 * @option boolean maximizeButton, enable or disable the window maximize button
 * @option HTML maximizeIcon, an html text to display as the maximize icon
 * @option boolean closeButton, enable or disable the window close button
 * @option HTML closeIcon, an html text to display as the close icon
 * @option boolean draggable, enable or disable the window dragging
 * @option boolean resizeable, enable or disable the window resize button
 * @option HTML resizeIcon, an html text to display as the resize icon
 * @option string windowType, a string "normal", "video", or "iframe"
 *
 * @type jQuery
 *
 * @name jqWindowsEngine
 * @cat Plugins/Windows
 * @author Hernan Amiune (amiune@gmail.com)
 */ 
jQuery.fn.newWindow = function(options){

    var lastMouseX = 0;
    var lastMouseY = 0;

    var defaults = {
        windowTitle : "",
        content: "",
        ajaxURL: "",
        width : 200,
        height : 200,
        posx : 50,
        posy : 50,
        onDragBegin: null,
        onDragEnd: null,
        onResizeBegin: null,
        onResizeEnd: null,
        onAjaxContentLoaded: null,
        statusBar: true,
        minimizeButton: true,
        minimizeIcon: "-",
        maximizeButton: true,
        maximizeIcon: "O",
        closeButton: true,
        closeIcon: "X",
        draggable: true,
        resizeable: true,
        resizeIcon: "&nbsp;",
        windowType: "standard"
    };
//resizeIcon: "<div style='background-image:url(/js/resizer_arrow.gif);font-size:30px;background-repeat:no-repeat;cursor:se-resize;float:right;height:11px;margin-top:2px;overflow:hidden;width:11px;'>&nbsp; &nbsp; &nbsp; &nbsp;</div>",
          
    var options = jQuery.extend(defaults, options);
    
    jQuerywindowContainer = jQuery('<div class="window-container" id="fde_window_container_'+ fde_window_content_id+ '"></div>');
    jQuerywindowTitleBar = jQuery('<div class="window-titleBar"></div>');        
    jQuerywindowMinimizeButton = jQuery('<div class="window-minimizeButton"></div>');
    jQuerywindowMaximizeButton = jQuery('<div class="window-maximizeButton"></div>');
    jQuerywindowCloseButton = jQuery('<div class="window-closeButton" id="fde_close_button_window_'+ fde_window_content_id+ '"></div>');
    jQuerywindowContent = jQuery('<div class="window-content" id="fde_window_'+ fde_window_content_id+ '"></div>');
    jQuerywindowStatusBar = jQuery('<div class="window-statusBar"></div>');
    jQuerywindowResizeIcon = jQuery('<div class="window-resizeIcon"></div>');
    
    if(options.windowType=="video" || options.windowType=="iframe")
      jQuerywindowContent.css("overflow","hidden");
    
    var setFocus = function(jQueryobj){
        jQueryobj.css("z-index",jqWindowsEngineZIndex++);
    }
    
    var resize = function(jQueryobj, width, height){
        
        width = parseInt(width);
        height = parseInt(height);
        
        jQueryobj.attr("lastWidth",width)
            .attr("lastHeight",height);
        
        width = width+"px";
        height = height+"px";
        
        jQueryobj.css("width", width)
            .css("height", height);
        
        if(options.windowType=="video"){
          jQueryobj.children(".window-content").children("embed").css("width", width)
                   .css("height", height);
          jQueryobj.children(".window-content").children("object").css("width", width)
                   .css("height", height);
          jQueryobj.children(".window-content").children().children("embed").css("width", width)
                   .css("height", height);
          jQueryobj.children(".window-content").children().children("object").css("width", width)
                   .css("height", height);
        }
        
        if(options.windowType=="iframe")        
          jQueryobj.children(".window-content").children("iframe").css("width", width)
                   .css("height", height);
                   
    }
    
    var move = function(jQueryobj, x, y){
        
        x = parseInt(x);
        y = parseInt(y);
        
        jQueryobj.attr("lastX",x)
            .attr("lastY",y);
        
        x = x+"px";
        y = y+"px";        
            
        jQueryobj.css("left", x)
            .css("top", y);
    }

    var dragging = function(e, jQueryobj){
        if(options.draggable){
        e = e ? e : window.event;
        var newx = parseInt(jQueryobj.css("left")) + (e.clientX - lastMouseX);
        var newy = parseInt(jQueryobj.css("top")) + (e.clientY - lastMouseY);
        lastMouseX = e.clientX;
        lastMouseY = e.clientY;
      
        move(jQueryobj,newx,newy);
        }
    };
    
    var resizing = function(e, jQueryobj){
      
      e = e ? e : window.event;
      var w = parseInt(jQueryobj.css("width"));
      var h = parseInt(jQueryobj.css("height"));
      w = w<100 ? 100 : w;
      h = h<50 ? 50 : h;
      var neww = w + (e.clientX - lastMouseX);
      var newh = h + (e.clientY - lastMouseY);
      lastMouseX = e.clientX;
      lastMouseY = e.clientY;
      
      resize(jQueryobj, neww, newh);
    };
    
    jQuerywindowTitleBar.bind('mousedown', function(e){
        jQueryobj = jQuery(e.target).parent();
        setFocus(jQueryobj);
        
        if(jQueryobj.attr("state") == "normal"){
            e = e ? e : window.event;
            lastMouseX = e.clientX;
            lastMouseY = e.clientY;
            
            jQuery(document).bind('mousemove', function(e){
                dragging(e, jQueryobj);
            });
            
            
            jQuery(document).bind('mouseup', function(e){
                if(options.onDragEnd != null)options.onDragEnd();
                jQuery(document).unbind('mousemove');
                jQuery(document).unbind('mouseup');
            });
            
            if(options.onDragBegin != null)options.onDragBegin();
        }
    });
    
    jQuerywindowResizeIcon.bind('mousedown', function(e){
        jQueryobj = jQuery(e.target).parent().parent();
        setFocus(jQueryobj);
        
        if(jQueryobj.attr("state") == "normal"){
            e = e ? e : window.event;
            lastMouseX = e.clientX;
            lastMouseY = e.clientY;

            jQuery(document).bind('mousemove', function(e){
                resizing(e, jQueryobj);
            });

            jQuery(document).bind('mouseup', function(e){
                if(options.onResizeEnd != null)options.onResizeEnd();
                jQuery(document).unbind('mousemove');
                jQuery(document).unbind('mouseup');
            });
            
            if(options.onResizeBegin != null)options.onResizeBegin();
        }
      
    });
    
    jQuerywindowMinimizeButton.bind('click', function(e){
        jQueryobj = jQuery(e.target).parent().parent();
        setFocus(jQueryobj);
        if(jQueryobj.attr("state") == "normal"){
            jQuery(e.target).parent().next().slideToggle("slow");
        }
    });
    
    jQuerywindowMaximizeButton.bind('click', function(e){
      jQueryobj = jQuery(e.target).parent().parent();
      setFocus(jQueryobj);
      if(jQueryobj.attr("state") == "normal"){
          if(options.windowType=="standard"){
            jQueryobj.animate({
              top: "5px",
              left: "5px",
              width: jQuery(window).width()-15,
              height: jQuery(window).height()-45
            },"slow");
          }
          else{
            tmpx = jQueryobj.attr("lastX");
            tmpy = jQueryobj.attr("lastY");
            tmpwidth = jQueryobj.attr("lastWidth");
            tmpheight = jQueryobj.attr("lastHeight");
            move(jQueryobj, 5, 5);
            resize(jQueryobj,jQuery(window).width()-15,jQuery(window).height()-45);
            jQueryobj.attr("lastX",tmpx);
            jQueryobj.attr("lastY",tmpy);
            jQueryobj.attr("lastWidth",tmpwidth);
            jQueryobj.attr("lastHeight",tmpheight);
          }
          jQueryobj.attr("state","maximized")
      }
      else if(jQueryobj.attr("state") == "maximized"){
        if(options.windowType=="standard"){ 
          jQueryobj.animate({
            top: jQueryobj.attr("lastY"),
            left: jQueryobj.attr("lastX"),
            width: jQueryobj.attr("lastWidth"),
            height: jQueryobj.attr("lastHeight")
          },"slow");
        }
        else{
          resize(jQueryobj,jQueryobj.attr("lastWidth"),jQueryobj.attr("lastHeight"));
          move(jQueryobj,jQueryobj.attr("lastX"),jQueryobj.attr("lastY"));
        }
        jQueryobj.attr("state","normal")
      }
      
    });
    
    jQuerywindowCloseButton.bind('click', function(e){
      fde_clear_interval(this.id);                               
      jQuery(e.target).parent().parent().fadeOut();
      jQuery(e.target).parent().parent().children(".window-content").html("");
    });
    
    jQuerywindowContent.click(function(e){
      setFocus(jQuery(e.target).parent());
    });
    jQuerywindowStatusBar.click(function(e){
      setFocus(jQuery(e.target).parent());
    });
    
    move(jQuerywindowContainer,options.posx,options.posy);
    resize(jQuerywindowContainer,options.width,options.height);
    jQuerywindowContainer.attr("state","normal");
    jQuerywindowTitleBar.append(options.windowTitle);
    
    if(options.minimizeButton)
        jQuerywindowTitleBar.append(jQuerywindowMinimizeButton)
    if(options.maximizeButton)
        jQuerywindowTitleBar.append(jQuerywindowMaximizeButton)
    if(options.closeButton)  
        jQuerywindowTitleBar.append(jQuerywindowCloseButton);
    
    if(options.resizeable)
        jQuerywindowStatusBar.append(jQuerywindowResizeIcon);
    
    jQuerywindowContainer.append(jQuerywindowTitleBar)
    jQuerywindowContainer.append(jQuerywindowContent)
    
    if(options.statusBar)
        jQuerywindowContainer.append(jQuerywindowStatusBar);
    
    jQuerywindowContainer.css("display","none");
    
    return this.each(function(index) {
        var jQuerythis = jQuery(this);          
        
        jQuerywindowMinimizeButton.html(options.minimizeIcon);
        jQuerywindowMaximizeButton.html(options.maximizeIcon);
        jQuerywindowCloseButton.html(options.closeIcon);
        jQuerywindowResizeIcon.html(options.resizeIcon);
        
        jQuerythis.data("window",jQuerywindowContainer);
        jQuery('body').append(jQuerywindowContainer);
        /*
        jQuerythis.click(function(event){
            event.preventDefault();   
            jQuerywindow = jQuerythis.data("window")
            if(options.ajaxURL != ""){ 

                 jQuery.ajax({
                   type: "GET",
                   url: options.ajaxURL,
                   dataType: "html",
                   //data: "header=variable",
                   success: function(data){
                     jQuerywindow.children(".window-content").html(data);
                     if(options.onAjaxContentLoaded != null) options.onAjaxContentLoaded(); 
                   }
                 });
            
            }
            else jQuerywindow.children(".window-content").html(options.content);
            if(!options.draggable)
                jQuerywindow.children(".window-titleBar").css("cursor","default");
            setFocus(jQuerywindow);
            jQuerywindow.fadeIn();            
        });
        */

        {
            jQuerywindow = jQuerythis.data("window")
            if(options.ajaxURL != ""){ 

                 jQuery.ajax({
                   type: "GET",
                   url: options.ajaxURL,
                   dataType: "html",
                   //data: "header=variable",
                   success: function(data){
                     jQuerywindow.children(".window-content").html(data);
                     if(options.onAjaxContentLoaded != null) options.onAjaxContentLoaded(); 
                   }
                 });
            
            }
            else jQuerywindow.children(".window-content").html(options.content);
            if(!options.draggable)
                jQuerywindow.children(".window-titleBar").css("cursor","default");
            setFocus(jQuerywindow);
            jQuerywindow.fadeIn();            
        }
        

    });

  
}})(jQuery); 
