// JavaScript Document
/*$(window).scroll(function()
{
	if($.browser.msie && parseInt(jQuery.browser.version) <= 6)
	{
		var scrollTop = $(window).scrollTop();
		var animationTime = 2;
		
		$("#header").css("left",$("#maincontainer").offset().left);
		$("#header").animate({marginTop: scrollTop}, animationTime, "linear");
	}
});*/

var deviceIphone = "iphone";
var deviceIpod = "ipod";
var deviceAndroid = "android";
var uagent = navigator.userAgent.toLowerCase();


//**************************
// Detects if the current device is an Android OS-based device.
function DetectAndroid()
{
   if (uagent.search(deviceAndroid) > -1)
      return true;
   else
      return false;
}

// Detects if the current device is an iPhone.
function DetectIphone()
{
   if (uagent.search(deviceIphone) > -1)
      return true;
   else
      return false;
}

//**************************
// Detects if the current device is an iPod Touch.
function DetectIpod()
{
   if (uagent.search(deviceIpod) > -1)
      return true;
   else
      return false;
}

//**************************
// Detects if the current device is an iPhone or iPod Touch.
function DetectIphoneOrIpod()
{
    if (DetectIphone())
       return true;
    else if (DetectIpod())
       return true;
    else
       return false;
}

//**************************
// Detects if the current device is an Android OS-based device and
//   the browser is based on WebKit.
function DetectAndroidWebKit()
{
   if (DetectAndroid())
   {
     //if (DetectWebkit())
        return true;
     /*else
        return false;*/
   }
   else
      return false;
}


function onWindowResize()
{
	
}

var defaultTopAddComment = 0;
var defaultTopNetworkPanel = 0;
$(document).ready(
	function()
	{
		if(navigator.platform.indexOf("iPad") != -1) jQuery(document).scrollTop();
		if(jQuery(".commentspanelinsert").length > 0 || jQuery(".networkpanel").length > 0) 
		{
			var animationTime = 200;
			var heightBrowser = jQuery(window).height();
			defaultTopAddComment = parseInt(jQuery(".commentspanelinsert").css('top'), 10);
			if(jQuery(".networkpanel").length > 0)
				defaultTopNetworkPanel = parseInt(jQuery(".networkpanel").css('top'), 10);
				
			//if((defaultTopAddComment + PADDIN_BOTTOM_PANEL + jQuery(".commentspanelinsert").height()) > heightBrowser) 
			//	jQuery(".commentspanelinsert").css('top', scrollTop + heightBrowser - jQuery(".commentspanelinsert").height() - PADDIN_BOTTOM_PANEL + "px");
			jQuery(window).bind("resize", resizeWindowInsertComment);
		}
		if($.browser.msie && parseInt(jQuery.browser.version) <= 6)
		{	
			$("#header").makeFloat({x: "current", y: "current", speed: "fast"});
		} else {
			$("#header").makeFloat({x: "current", y: "current", speed: "fast"});
		}
	}
);

var currentScrollTop;
var scrollinertPanelInterval = null;
var scrollLigthboxInterval = null;
var PADDIN_BOTTOM_PANEL = 20;
var minimalNumberScroll = 0;
jQuery(window).scroll(
	function () 
	{
		if(jQuery(".ac_results").length > 0 && jQuery(".ac_results").css('display') != "none")
			jQuery(".ac_results").hide();
		
		currentScrollTop = jQuery(window).scrollTop();
		//if(jQuery("#gallery_ligthbox_container").length > 0 && jQuery("#gallery_ligthbox_container").css('display') != "none" && scrollLigthboxInterval == null)
		//	scrollLigthboxInterval = setInterval('checkScrollLigthbox()', 10);
		if(jQuery("#gallery_ligthbox_container").length > 0 && jQuery("#gallery_ligthbox_container").css('display') != "none") return;
		//if(jQuery(".commentspanelinsert").length == 0) return;
		//if(jQuery("#addcommentblockscroll").length == 0) return;
		minimalNumberScroll = 0;
		if($(window).width() < $("#totalcontainer").innerWidth() || (jQuery.browser.msie && jQuery.browser.version <= 6) || navigator.platform.indexOf("iPad") != -1)
		{
			if(jQuery(".commentspanelinsert").length > 0)
				jQuery(".commentspanelinsert").css('position', 'absolute');
				
			if(jQuery(".networkpanel").length > 0)
				jQuery(".networkpanel").css('position', 'absolute');
				
			if(scrollinertPanelInterval == null) scrollinertPanelInterval = setInterval('checkScrollPanelComment()', 10);
		}else if((jQuery.browser.msie && jQuery.browser.version <= 6) || navigator.platform.indexOf("iPad") != -1)
		{
			if(scrollinertPanelInterval == null) scrollinertPanelInterval = setInterval('checkScrollPanelComment()', 10);	
		}
	}
);

function resizeWindowInsertComment( e ) 
{
	var scrollTop = jQuery(window).scrollTop();
	var heightBrowser = jQuery(window).height();
	var animationTime = 200;
	
	//if((jQuery.browser.msie && jQuery.browser.version <= 6) || navigator.platform.indexOf("iPad") != -1) return;
	if($(window).width() < $("#totalcontainer").innerWidth() && scrollinertPanelInterval == null)
	{
		if(jQuery(".networkpanel").length > 0)
		{
			jQuery(".networkpanel").css('position', 'absolute');
			jQuery(".networkpanel").css('top', (scrollTop + defaultTopNetworkPanel) + 'px');
		}
		
		jQuery(".commentspanelinsert").css('position', 'absolute');
		if((defaultTopAddComment + jQuery(".commentspanelinsert").height() + PADDIN_BOTTOM_PANEL) < jQuery(window).height()) jQuery(".commentspanelinsert").css('top', defaultTopAddComment+'px');
		else jQuery(".commentspanelinsert").animate({top: scrollTop + heightBrowser - jQuery(".commentspanelinsert").height() - PADDIN_BOTTOM_PANEL}, animationTime, "linear");
			
		if(jQuery("#login_annotation").length > 0)
		{
			jQuery("#login_annotation").css('position', 'absolute');
			var left = parseInt(jQuery("#comments").offset().left) - jQuery("#login_annotation").width(); // jQuery("#comments").width() +
			jQuery("#login_annotation").css('left', left+'px');
			
			if((defaultTopAddComment + jQuery("#login_annotation").height() + PADDIN_BOTTOM_PANEL) < jQuery(window).height()) jQuery("#login_annotation").css('top', defaultTopAddComment+'px');
			else
			{
				if(jQuery.browser.msie && jQuery.browser.version <= 6) jQuery("#login_annotation").css('top', scrollTop + heightBrowser - jQuery("#login_annotation").height() - 18 - PADDIN_BOTTOM_PANEL);
				else jQuery("#login_annotation").css('top', scrollTop + heightBrowser - jQuery("#login_annotation").height() - PADDIN_BOTTOM_PANEL);
			}
		}
		if(jQuery("#insert_panel_annotation").length > 0)
		{
			jQuery("#insert_panel_annotation").css('position', 'absolute');
			var left = parseInt(jQuery("#comments").offset().left) - jQuery("#insert_panel_annotation").width(); // jQuery("#comments").width() + 
			jQuery("#insert_panel_annotation").css('left', left+'px');
			if((defaultTopAddComment + jQuery("#insert_panel_annotation").height() + PADDIN_BOTTOM_PANEL) < jQuery(window).height()) jQuery("#insert_panel_annotation").css('top', defaultTopAddComment+'px');
			else
			{
				if(jQuery.browser.msie && jQuery.browser.version <= 6) jQuery("#insert_panel_annotation").css('top', (scrollTop + heightBrowser - jQuery("#insert_panel_annotation").height() - 18 - PADDIN_BOTTOM_PANEL));
				else jQuery("#insert_panel_annotation").css('top', (scrollTop + heightBrowser - jQuery("#insert_panel_annotation").height() - PADDIN_BOTTOM_PANEL));
			}
		}
		if(scrollinertPanelInterval == null) scrollinertPanelInterval = setInterval('checkScrollPanelComment()', 10);
	}else if($(window).width() >= $("#totalcontainer").innerWidth())
	{
		if(scrollinertPanelInterval != null) clearInterval(scrollinertPanelInterval);
		scrollinertPanelInterval = null;	
		
		if((defaultTopAddComment + jQuery(".commentspanelinsert").height() + PADDIN_BOTTOM_PANEL) < jQuery(window).height()) jQuery(".commentspanelinsert").css('top', defaultTopAddComment+'px');
		else jQuery(".commentspanelinsert").animate({top: heightBrowser - jQuery(".commentspanelinsert").height() - PADDIN_BOTTOM_PANEL}, animationTime, "linear");
		
		if(!((jQuery.browser.msie && jQuery.browser.version <= 6) || navigator.platform.indexOf("iPad") != -1))
		{
			jQuery(".commentspanelinsert").css('position', 'fixed');
			if(jQuery(".networkpanel").length > 0)
				jQuery(".networkpanel").css('position', 'fixed');
			jQuery(".networkpanel").css('top', defaultTopNetworkPanel+'px');
		}else
		{
			jQuery(".networkpanel").css('top', (defaultTopNetworkPanel + scrollTop)+'px');
		}
		
		if(jQuery("#login_annotation").length > 0)
		{
			var left = parseInt(jQuery("#comments").offset().left) - jQuery("#login_annotation").width(); // jQuery("#comments").width() +
			jQuery("#login_annotation").css('left', left+'px');
			if((defaultTopAddComment + jQuery("#login_annotation").height() + PADDIN_BOTTOM_PANEL) < jQuery(window).height()) jQuery("#login_annotation").css('top', defaultTopAddComment+'px');
			else
			{
				if(jQuery.browser.msie && jQuery.browser.version <= 6) jQuery("#login_annotation").css('top', (jQuery(window).height() - jQuery("#login_annotation").height() - 18 - PADDIN_BOTTOM_PANEL));
				else jQuery("#login_annotation").css('top', (jQuery(window).height() - jQuery("#login_annotation").height() - PADDIN_BOTTOM_PANEL)); 
			}
			if(!((jQuery.browser.msie && jQuery.browser.version <= 6) || navigator.platform.indexOf("iPad") != -1))
				jQuery("#login_annotation").css('position', 'fixed');
		}
		
		if(jQuery("#insert_panel_annotation").length > 0)
		{
			var left = parseInt(jQuery("#comments").offset().left) - jQuery("#insert_panel_annotation").width(); // jQuery("#comments").width() +
			jQuery("#insert_panel_annotation").css('left', left+'px');
			if((defaultTopAddComment + jQuery("#insert_panel_annotation").height() + PADDIN_BOTTOM_PANEL) < jQuery(window).height()) jQuery("#insert_panel_annotation").css('top', defaultTopAddComment+'px');
			else
			{
				if(jQuery.browser.msie && jQuery.browser.version <= 6) jQuery("#insert_panel_annotation").css('top', (scrollTop + heightBrowser - jQuery("#insert_panel_annotation").height() - 18 - PADDIN_BOTTOM_PANEL));
				else jQuery("#insert_panel_annotation").css('top', (scrollTop + jQuery(window).height() - jQuery("#insert_panel_annotation").height() - PADDIN_BOTTOM_PANEL));
			}
			if(!((jQuery.browser.msie && jQuery.browser.version <= 6) || navigator.platform.indexOf("iPad") != -1))
				jQuery("#insert_panel_annotation").css('position', 'fixed');
		}
	}
}

function checkScrollLigthbox()
{
	var scrollTop = jQuery(window).scrollTop();
	if(scrollTop != currentScrollTop) return;
	clearInterval(scrollLigthboxInterval);
	scrollLigthboxInterval = null;
	setTimeout('scrollLigthboxGallery()', 100);
}

function scrollLigthboxGallery()
{
	if(jQuery("#gallery_ligthbox_container").length > 0 && jQuery("#gallery_ligthbox_container").css('display') != "none")
	{
		var animationTime = 200;
		if(jQuery.browser.msie && jQuery.browser.version >= 7) animationTime = 200;
		if(jQuery.browser.msie && jQuery.browser.version <= 6) animationTime = 100;
		var scrollTop = jQuery(window).scrollTop();
		var heightBrowser = jQuery(window).height();
		var heightAlert = jQuery("#gallery_ligthbox_container").height();
		if(heightAlert < heightBrowser)
		{
			topAlertDefault = Math.ceil((heightBrowser - heightAlert) / 2);
			jQuery("#gallery_ligthbox_container").animate({top: topAlertDefault + scrollTop}, animationTime, "linear");
		}
	}
}

function checkScrollPanelComment()
{
	minimalNumberScroll = minimalNumberScroll + 1;
	if(minimalNumberScroll < 5) return;
	var scrollTop = jQuery(window).scrollTop();
	if(scrollTop != currentScrollTop) return;
	clearInterval(scrollinertPanelInterval);
	scrollinertPanelInterval = null;
	setTimeout('scrollPanelAddComment()', 50);
}

function scrollPanelAddComment()
{
	var scrollTop = jQuery(window).scrollTop();
	var animationTime = 500;
	if(jQuery.browser.msie && jQuery.browser.version >= 7) animationTime = 400;
	if(jQuery.browser.msie && jQuery.browser.version <= 6) animationTime = 300;
	var heightBrowser = jQuery(window).height();
	
	var heightAlert = 0;
	if(jQuery(".commentspanelinsert").length > 0)
		heightAlert = jQuery(".commentspanelinsert").height();
	if(heightAlert < heightBrowser)
	{
		if(jQuery("#login_annotation").length > 0)
		{
			if((defaultTopAddComment + jQuery("#login_annotation").height() + PADDIN_BOTTOM_PANEL) < heightBrowser)
			{
				if(jQuery.browser.msie && jQuery.browser.version <= 6) jQuery("#login_annotation").animate({top: scrollTop + defaultTopAddComment - 18}, animationTime, "linear");
				else jQuery("#login_annotation").animate({top: scrollTop + defaultTopAddComment}, animationTime, "linear");
			}else
			{
				if(jQuery.browser.msie && jQuery.browser.version <= 6) jQuery("#login_annotation").animate({top: scrollTop + heightBrowser - jQuery("#login_annotation").height() - 18 - PADDIN_BOTTOM_PANEL}, animationTime, "linear");
				else 
					jQuery("#login_annotation").animate({top: scrollTop + heightBrowser - jQuery("#login_annotation").height() - PADDIN_BOTTOM_PANEL}, animationTime, "linear");
			}
		}
		
		if(jQuery("#insert_panel_annotation").length > 0)
		{
			if((defaultTopAddComment + jQuery("#insert_panel_annotation").height() + PADDIN_BOTTOM_PANEL) < heightBrowser)
			{
				if(jQuery.browser.msie && jQuery.browser.version <= 6) jQuery("#insert_panel_annotation").animate({top: scrollTop + defaultTopAddComment - 18}, animationTime, "linear");
				else jQuery("#insert_panel_annotation").animate({top: scrollTop + defaultTopAddComment}, animationTime, "linear");
			}else
			{
				if(jQuery.browser.msie && jQuery.browser.version <= 6) jQuery("#insert_panel_annotation").animate({top: scrollTop + heightBrowser - jQuery("#insert_panel_annotation").height() - 18 - PADDIN_BOTTOM_PANEL}, animationTime, "linear");
				else jQuery("#insert_panel_annotation").animate({top: scrollTop + heightBrowser - jQuery("#insert_panel_annotation").height() - PADDIN_BOTTOM_PANEL}, animationTime, "linear");
			}
		}
		
		if(jQuery(".commentspanelinsert").length > 0)
		{
			if((defaultTopAddComment + jQuery(".commentspanelinsert").height() + PADDIN_BOTTOM_PANEL) < heightBrowser)
				jQuery(".commentspanelinsert").animate({top: scrollTop + defaultTopAddComment - PADDIN_BOTTOM_PANEL}, animationTime, "linear");
			else
				jQuery(".commentspanelinsert").animate({top: scrollTop + heightBrowser - jQuery(".commentspanelinsert").height() - PADDIN_BOTTOM_PANEL}, animationTime, "linear");
		}
		
		if(jQuery(".networkpanel").length > 0)
		{
			//console.log("MOVE");
			//jQuery(".networkpanel").css('top', (scrollTop + defaultTopNetworkPanel)+'px');
			jQuery(".networkpanel").animate({top: scrollTop + defaultTopNetworkPanel}, animationTime, "linear");
		}
	}
}
