﻿var isIE6 = navigator.userAgent.toLowerCase().indexOf('msie 6') != -1;
var shoutOutError = false;

$(document).ready(function() {
    $(".post-shout span").mouseenter(function() {
        ShowShoutOutForm();
    })
    $("#PopupShoutClose").click(function() {
        HideShoutOutForm();
    })

    $("#PopupThankClose").click(function() {
        $("#PopupThank").hide();
        if (shoutOutError)
        {
            shoutOutError = false;
            ShowShoutOutFormQucik();
        }
    })

    MoveTicker();
    $(window).scroll(function() {
        MoveTicker();
    });

});

function LimitTextLength(elem)
{
    if (elem.value.length > 140)
    {
        var scrTop = elem.scrollTop;
        elem.value = elem.value.substring(0, 140);
        elem.scrollTop = scrTop;
    }
}

function MoveTicker()
{
    var windowHeight = $(window).height();
    var windowWidth = $(window).width();
    var windowScrollTop = $(window).scrollTop();
    var imainHeight = parseInt($("#inner-main").css("height").replace("px"));
    var documentHeight = getDocHeight(); //$(document).height();
    var tickerDiv = $("#ticker_div");
    var tickerLeft = Math.round((windowWidth - 939-20) / 2);
    //alert(windowHeight + ":" + windowScrollTop + ":" + documentHeight);
    tickerDiv.css("left", tickerLeft + "px");
    //if (windowHeight + windowScrollTop < imainHeight + 160 + 46)
    if (windowHeight + windowScrollTop < documentHeight - 264)
    {
    	if (isIE6)
    	{
       		tickerDiv.css("top", (windowHeight + windowScrollTop - 224) + "px");
    	}
    	else
    	{
    		tickerDiv.css("position", "fixed");
    		tickerDiv.css("bottom", "0");
    	}
    }
    else
    {
        tickerDiv.css("position", "absolute");
       	tickerDiv.css("top", null);
        tickerDiv.css("bottom", "-29px");
    }
	tickerDiv.css("visibility", "visible");
}

function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}

function ShowShoutOutForm()
{
    var popupShout = $('#PopupShout');
    if (popupShout.css("display") == "none")
    {
        var popupThank = $("#PopupThank");
        if (popupThank.css("display") != "none")
        {
            popupThank.hide();
        }
        
	    popupShout.show().animate({
		    height: "273px"
	    },500,function(){
		    $(this).animate({
			    height: "301px",
			    bottom: "-29px"
		    },0,function(){
			    $(this).find(".popup-buts").fadeIn(200);
		    })
	    });
	    OnNameLeave(document.getElementById("YourNameBox"));
	    OnZipLeave(document.getElementById("YourZipCodeBox"));
	    OnShoutOutBoxLeave(document.getElementById("ShoutOutBox"));
        document.getElementById("SubmitShoutOutResultLabel").innerHTML = "";
	}
}

function ShowShoutOutFormQucik() {
    $("#PopupShout").css("bottom", "-29px");
    $("#PopupShout").css("height", "301px");
    $("#PopupShout").show();
    $(".ps-popup .popup-buts").show();
}

function HideShoutOutForm() {
    $(".ps-popup .popup-buts").fadeOut(200, function() {
        $("#PopupShout").animate({
            bottom: "0",
            height: "285px"
        }, 0, function() {
            $(this).animate({
                height: "83px"
            }, 500, function() {
                $(this).fadeOut(200);
            })
        })
    })
}

function HideShoutOutFormQuick() {
    $(".ps-popup .popup-buts").hide();
    $("#PopupShout").css("bottom", "0");
    $("#PopupShout").css("height", "83px");
    $("#PopupShout").hide();
}



/*****************************/
/**     Ajax callbacks      **/
/*****************************/
function SubmitShoutOutCallback(result)
{
	//unblockViewport();
	if (!result)
	{
	    document.getElementById("ShoutOutSubmitResult").innerHTML = "An error occured on server";
	    HideShoutOutFormQuick();
        shoutOutError = true;
	    $("#PopupThank").show();
	}
	else
	{
		if (result.value.ResultType == 0)
		{
			/*HidePopup("ShoutOutControl");*/
		    /*ShowPopupThankYou("ShoutOutControl");*/
		    //$('#PopupShout').hide();
		    
		    document.getElementById("ShoutOutSubmitResult").innerHTML = "Thank You!";
		    HideShoutOutFormQuick();
		    $("#PopupThank").show();
		    
	        document.getElementById("YourNameBox").value = "";
	        document.getElementById("YourZipCodeBox").value = "";
	        document.getElementById("ShoutOutBox").value = "";
	        OnNameLeave(document.getElementById("YourNameBox"));
	        OnZipLeave(document.getElementById("YourZipCodeBox"));
	        OnShoutOutBoxLeave(document.getElementById("ShoutOutBox"));
		    
		}
		else
		{
		    document.getElementById("ShoutOutSubmitResult").innerHTML = "Error: " + result.value.Message;
		    HideShoutOutFormQuick();
            shoutOutError = true;
		    $("#PopupThank").show();
		}
	}
}
/*****************************/
/**  Ajax calls validation  **/
/*****************************/
function SubmitShoutOutValidate() {
	var result = "";
	var separator = "";
	var lineSeparator = "";
	if (trim(document.getElementById("YourNameBox").value).length == 0 || document.getElementById("YourNameBox").value == 'Your Name')
	{
		result += separator + "Your Name";
		separator = ", ";
	}
	if (trim(document.getElementById("YourZipCodeBox").value).length == 0 || document.getElementById("YourZipCodeBox").value == 'ZIP Code')
	{
		result += separator + "Your zip-code";
		separator = ", ";
	}
	if (trim(document.getElementById("ShoutOutBox").value).length == 0 || document.getElementById("ShoutOutBox").value == 'Shout Out (140 characters)')
	{
		result += separator + "Your shout out";
		separator = ", ";
	}	
	if (result.length != 0)
	{
		result = "The following fields are required: " + result;
		lineSeparator = "<br>";
	}
	if (trim(document.getElementById("YourNameBox").value).length > 50)
	{
		result += "<br />Your Name length exceeds the limit in 50 symbols";
	}	
	if (trim(document.getElementById("ShoutOutBox").value).length > 140)
	{
		result += "<br />Your shout out length exceeds the limit in 140 symbols";
	}	
	return result;
}
/*****************************/
/**       Ajax calls        **/
/*****************************/

function SubmitShoutOut()
{
	var validationMessage = SubmitShoutOutValidate();
	if (validationMessage.length != 0)
	{
		document.getElementById("ShoutOutSubmitResult").innerHTML = validationMessage;
	    HideShoutOutFormQuick();
        shoutOutError = true;
	    $('#PopupThank').show();
	}
	else
	{
		//blockViewport(document.getElementById("ShoutOutControlContent"), ["ShoutOutSubmitButton"]);
		WebSite.AjaxAccessor.SubmitShoutOut(
			document.getElementById("YourNameBox").value,
			document.getElementById("YourZipCodeBox").value,
			document.getElementById("ShoutOutBox").value,
			SubmitShoutOutCallback);
	}
	return false;
}

function trim(str)
{
	return str.replace(/^\s*/, "").replace(/\s*$/, "");
}

