/*
	Javascript for iBlock
 *********************
 */
var iBlockIv;
var iBlock;
var iBlockContent;
var iBlockBottom;
var iBlockClose;

function delayShow()
{
    clearInterval(iBlockIv);    
    iBlock.style.display = "block";    
}

function createIBlock()
{
    if (iBlock != null) return; 
    iBlock = document.createElement("div");
    iBlockContent = document.createElement("div");
    iBlockBottom = document.createElement("div");
    iBlockClose = document.createElement("span");    
    $(iBlockClose).addClass('close');
    iBlock.id = "iBlock";
    iBlockContent.id = "iBlockContent";
    iBlockBottom.id = "iBlockBottom";    
    iBlock.appendChild(iBlockContent);
    iBlock.appendChild(iBlockBottom);
    iBlock.appendChild(iBlockClose);
    //document.getElementsByTagName("body")[0].appendChild(iBlock);    
    $('body').append(iBlock);
}


function prepIBlocks()
{
    var elms = $(".i, .il");

    // Create iBlock div
    createIBlock();

    elms.each(function(i, val) {
        var currElm = elms[i];
        
        if ($(currElm).data('iblock')) return;

        var loadLayer = function(event) {
            if (this.className == "i loadProductSpecs")
            {
                showLayer(this, event.data.delay);
                var productCode = this.getAttribute("productcode");
                var library = this.getAttribute("library");
                var cElm = $(".c",this)[0];
                var label = this.getAttribute("label");
                loadProductSpecificationInfo(productCode, library, cElm, label, event.data.delay);
            }
            else
            {
                showLayer(this, event.data.delay);
            }
        }
        
        $(currElm).bind('click', { delay: false }, loadLayer);
        $(currElm).bind('mouseover', { delay: true }, loadLayer);

        $(currElm).mouseleave(function() {
            clearTimeout(iBlockIv);
        });
        
        $(currElm).data('iblock', true);
    });

    $(iBlock).mouseleave(function() {
        hideLayer();
        KK.Form.showSelects();	// For IE 6
    });
    
    $(iBlockClose).click(function() {
        hideLayer();
        KK.Form.showSelects();	// For IE 6
    });

    function showLayer(elm, delay)
    {
        clearTimeout(iBlockIv);
        
        var xPos = findPos(elm).x;
        var yPos = findPos(elm).y;

        KK.Form.hideSelects();	// For IE 6

        var contentsElm = $(".c", elm)[0];

        $(iBlockContent).html(contentsElm.innerHTML);
        
        iBlock.style.top = yPos + "px";
        iBlock.style.left = xPos + "px";
        if($(".c", elm).hasClass('small')) {
            iBlock.style.width = "225px";
            $('#iBlockContent').css("background-image", "url(/images/bg_iblock_small.png)");
            $('#iBlockBottom').css("background-image", "url(/images/bg_iblock_small.png)");
        } else {
            iBlock.style.width = "455px";
            $('#iBlockContent').css("background-image", "url(/images/bg_iblock.png)");
            $('#iBlockBottom').css("background-image", "url(/images/bg_iblock.png)");
        }


        // Check if the content has a header
        var headerElm = elm.getElementsByTagName("h3")[0];
        if (!headerElm)
        {
            iBlockContent.className = "noHeader";
        }
        else
        {
            iBlockContent.className = "";
        }

        if (delay === false) {
          delayShow();
        } else {
          iBlockIv = setTimeout("delayShow()",1000);
        }
    }

    function hideLayer()
    {
        iBlock.style.display = "none"
    }

    function findPos( oElement ) {
        if( typeof( oElement.offsetParent ) != 'undefined' ) {
            for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ) {
                posX += oElement.offsetLeft;
                posY += oElement.offsetTop;
            }
            return {x:posX, y:posY};
        } else {
            return {x:oElement.x, y:oElement.y};
        }
    }

    function loadProductSpecificationInfo(code, keyword, elementId, label, delay)
    {
        $(iBlockContent).append(' Even geduld aub..');

        $.ajax({
            url: "/product/specificatie_info/" + code + "/" + keyword + ".html",
            type: "GET",
            success: function(data) {
            $(elementId).html('<h3>' + label + '</h3>' + data);
            var parentElm = elementId.parentNode;
            parentElm.className = "i"
            showLayer(elementId.parentNode, delay);
        },
        error: function (data)
        {
            $(elementId).html('De informatie kon niet worden opgehaald.');
        }
        });    
    }
}

function initDialogPopup() {
	$('a.dialogpopup').each(function() {
		var $dialog = $(this);
		var $link = $(this).one('click', function() {
			$dialog
				.dialog({
					title: $link.attr('title'),
					width: 500,
					height: 300
				});

			$link.click(function() {
				$dialog.dialog('open');
				return false;
			});
			return false;
		});
	});
}

function dialogPopup() {
  $('a.dialogpopup').click(function() {
    var a = $(this);
    $.get(a.attr('href'),function(resp){
      var dialog = $('<div>').attr('id','formDialog').html(resp);
      $('body').append(dialog);
      //dialog.find(':submit').hide();
      dialog.dialog({
        title: a.attr('title') ? a.attr('title') : '',
        modal: true,
        buttons: {  
            'Register': function() {submitFormWithAjax($(this).find('form'));}
        },
        close: function() {$(this).remove();},
        width: 'auto'
      });
    }, 'html');
    return false;
  });
}


$(
    function(){ 
    	initDialogPopup();
        prepIBlocks();
    }
);





