// JavaScript Document

$(document).ready(function(){
	$("#peel-content").click(function(){
		 window.location.replace("http://www.facebook.com/pages/ITM-systems/324373198482?ref=ts#");
	});
	$("#peel-corner img").click(function(){
		 window.location.replace("http://www.facebook.com/pages/ITM-systems/324373198482?ref=ts#");
	});
	function overlay_watcher() {
		if ($("#peel-container").width()<=125) {
			$("#peel-container .overlay:hidden").show();
		} else {
			$("#peel-container .overlay:visible").hide();	
		}
		setTimeout(overlay_watcher, 10);
	}
						   
	function flipCorner() {
		if (!$("#peel-container").hasClass("open")) {
			$("#peel-container").animate({width:125, height:125}, 300, function() {
				$(this).animate({width:100, height:100}, 300);												   
			});	
		}
	}
	
	var peelInterval = setInterval(flipCorner, 7500);
	
	$("body").append('<a href="http://www.facebook.com/pages/ITM-systems/324373198482?ref=ts#" target="_new"><div id="peel-container"><div class="relative"><div id="peel-content-mask"><div id="peel-content"><iframe style="height:287px;float:right;width: 180px;background:white;z-index:100;" src="http://www.facebook.com/plugins/likebox.php?id=324373198482&amp;width=180&amp;connections=10&amp;stream=false&amp;header=true&amp;height=287" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:180px; height:287px;" allowTransparency="true"></iframe><div class="overlay"></div></div></div><div id="peel-corner"><img src="/fileadmin/template/images/corner.png" style="width:100%;height:100%;" /></div><div id="itm-button"></div></div></div></a>');
	
	/*$("#peel-container").hover(function(){
		//$(this).find(".overlay").hide();
		$(this).animate({width:500, height:500}, 500, function() {
			$(this).addClass("open");
			clearInterval(peelInterval);
			//$("#itm-button").fadeIn(200);
		});									
	}, function(){
		//$("#itm-button").fadeOut(100);
        if($(this).hasClass("open")) {
    		$(this).animate({width:100, height:100}, 300, function()	{
    			$(this).removeClass("open");
    			//$(this).find(".overlay").show();
    			peelInterval = setInterval(flipCorner, 7500);
    		});	
        }
	});*/
    
    function open_corner(){
        $("#peel-container").animate({width:500, height:500}, 500, function() {
			$("#peel-container").addClass("open");
			clearInterval(peelInterval);
			//$("#itm-button").fadeIn(200);
		});	
    }
    
    function close_corner(){
        if($("#peel-container").hasClass("open")) {
    		$("#peel-container").animate({width:100, height:100}, 300, function()	{
    			$("#peel-container").removeClass("open");
    			//$(this).find(".overlay").show();
    			peelInterval = setInterval(flipCorner, 7500);
    		});	
        }    
    }
    
    var config = {    
        sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
        interval: 200, // number = milliseconds for onMouseOver polling interval    
        over: open_corner, // function = onMouseOver callback (REQUIRED)    
        timeout: 500, // number = milliseconds delay before onMouseOut    
        out: close_corner // function = onMouseOut callback (REQUIRED)    
    };
    
    $("#peel-container").hoverIntent( config );

	
	overlay_watcher();
});
