﻿var cI;
var cTO;
var tI;
var IsPlaying = false;
var IsAnimating = false;
var aT;

function ForgotPassword() {
    $('#formForgot').css('display', 'block');
    $('#frmOverlay').css('display', 'block');
    $('#tbxForgotMail').val('').focus();
}

function SendPassword() {
    $('#imgForgotLoad').css('display', 'block');
    var email = $('#tbxForgotMail').val();

    validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
    if (email.search(validRegExp) == -1) {
        ShowAlert('Invalid Email Address');
        $('#tbxForgotMail').val('').focus();
        $('#imgForgotLoad').css('display', 'none');
    }
    else {

        $.ajax({ type: 'POST', dataType: 'json', contentType: 'application/json; charset=utf-8', url: 'WebServices/Chuzi.asmx/ForgotPassword', data: "{'Email':'" + email + "'}", success: function(msg) {
            if (msg.d == true) {
                Closeforgot();
                ShowAlert("Password has been emailed");
            }
            else {
                ShowAlert("Email does not exist");
                $('#tbxForgotMail').val('').focus();
            }
            $('#imgForgotLoad').css('display', 'none');
        }
        });
    }
}

function CloseForm() {
    $('#frmOverlay').css('display', 'none');
    $('.form').css('display', 'none');
}

function ShowReferral(Name) {
    $('#frmOverlay').css('display', 'block');
    $('.form').css('display', 'block');
    $('.rinviter').html(Name);
}

function VLogin() {
    $('#imgLoader').css('display', 'block');
    validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
    var strEmail = $('#tbxEmail').val();
    if (strEmail.search(validRegExp) == -1) {
        ShowAlert('Invalid Email Address');
        $('#imgLoader').css('display', 'none');
        return false;
    }
    else
        return true;
}
$(document).ready(function() {
    $('#alert').center();
    $('#formForgot,#formReferral').center();
    eval(todo);
});
$(window).load(function() {    
    flowplayer("a.myPlayer", "Content/Flash/flowplayer-3.2.1.swf", {
        key: '$b6eb72a0f2f1e29f3d4',
        logo: null,
        onBeforeClick: function() {
            IsPlaying = true;
        },
        clip: {
            autoBuffering: true,
            bufferLength: 1,
            onBegin: function() {
                IsPlaying = true;
            },
            onFinish: function() {
                IsPlaying = false;
            }
        }
    });
    $('#ticker01').liScroll({travelocity: .085});
    cI = setInterval(function() {
        Next();
    }, 9000);

    GetInitialTweets(3);
    tI = setInterval(function() {
        GetTweets(3);
    }, 15000);
});

function ShowAlert(Message) {
    PauseIntervals = true;
    try {
        flowplayer().unload();
    }
    catch (ex) { };
    $('.overlayAlert').show();
    var alert = $('.frmAlert');
    alert.find('.alert').html(Message);
    alert.fadeIn('slow', function() {
        aT = setTimeout(HideAlert, 1500);
    });
}

function HideAlert() {
    clearTimeout(aT);
    $('.frmAlert').fadeOut('slow', function() {
        PauseIntervals = false;
        $('.overlayAlert').hide();
    })
}

function Next() {
    if (!IsPlaying) {
        var value = $(".scroll-bar").slider("value");
        if (value == 9) {
            value = -1;
        }
        $(".scroll-bar").slider("value", [value + 1]);
        Changed();
    }
}

function Changed() {
    if (!IsPlaying) {
        if (!IsAnimating) {
            clearInterval(cI);
            cI = setInterval(function() {
                Next();
            }, 9000);

            var index = $(".scroll-bar").slider("value");
            var current = $('.slides').children('div:visible');
            var show = $('.slides').children('div')[index];
            IsAnimating = true;
            $(current).fadeOut('slow', function() {
                $(show).fadeIn('slow', function() {
                    flowplayer().unload();
                    IsAnimating = false;
                });
            });
        }
        else {
            clearTimeout(cTO);
            cTO = setTimeout(function() {
                Changed();
            }, 1500);
        }
    }
}

$(function() {
    $('.scroll-content').children('div').each(function() {
        $(this).click(
                function() {
                    try {
                        var x = ($(this).attr('id')).replace('i', '');
                        value = parseInt(x) - 1;
                        $(".scroll-bar").slider("value", [value]);
                        IsPlaying = false;
                        IsAnimating = false;
                        Changed();
                    }
                    catch (ex) {
                        alert(ex);
                    }
                });
    });
    var scrollPane = $('.scroll-pane');
    var scrollContent = $('.scroll-content');
    var scrollbar = $(".scroll-bar").slider({
        max: 9,
        min: 0,
        slide: function(e, ui) {
            IsAnimating = true;
        },
        change: function(event, ui) {
            if (scrollContent.width() > scrollPane.width()) {
                var margin = Math.round(ui.value / 100 * (scrollPane.width() - scrollContent.width())) * 11.1; // + 'px';
                scrollContent.animate({ 'margin-left': margin }, 'slow');
            }
            else { scrollContent.css('margin-left', 0); }
        }
    });

    //append icon to handle
    var handleHelper = scrollbar.find('.ui-slider-handle')
		.mousedown(function() {
		    scrollbar.width(handleHelper.width());
		})
		.mouseup(function() {
		    scrollbar.width('100%');
		})
		.append('<span class="ui-icon"></span>')
		.wrap('<div class="ui-handle-helper-parent"></div>').parent();

    scrollPane.css('overflow', 'hidden');

    function sizeScrollbar() {
        var remainder = scrollContent.width() - scrollPane.width();
        var proportion = remainder / scrollContent.width();
        var handleSize = 68; // scrollPane.width() - (proportion * scrollPane.width());
        scrollbar.find('.ui-slider-handle').css({
            width: handleSize,
            'margin-left': -handleSize / 2,
            outline: 'none'
        });
        handleHelper.width('').width(scrollbar.width() - handleSize);
    }
    function resetValue() {
        var remainder = scrollPane.width() - scrollContent.width();
        var leftVal = scrollContent.css('margin-left') == 'auto' ? 0 : parseInt(scrollContent.css('margin-left'));
        var percentage = Math.round(leftVal / remainder * 100);
        scrollbar.slider("value", percentage);
    }
    function reflowContent() {
        var showing = scrollContent.width() + parseInt(scrollContent.css('margin-left'));
        var gap = scrollPane.width() - showing;
        if (gap > 0) {
            scrollContent.css('margin-left', parseInt(scrollContent.css('margin-left')) + gap);
        }
    }
    $(window)
		.resize(function() {
		    resetValue();
		    sizeScrollbar();
		    reflowContent();
		});
    setTimeout(sizeScrollbar, 10);
});

function ShowTerms(title, file) {
    var frm = $('#formTerm');
    frm.find('.formContent').load(file, function() {
    frm.find('.formTitle b').html(title);
        $('#frmOverlay').css('display', 'block');
        frm.center().css('display', 'block').fadeIn(1000);
    });

}
jQuery.fn.liScroll = function(settings) {
    settings = jQuery.extend({
        travelocity: 0.07
    }, settings);
    return this.each(function() {
        var $strip = jQuery(this);
        $strip.addClass("newsticker")
        var stripWidth = 0;
        var $mask = $strip.wrap("<div class='mask'></div>");
        var $tickercontainer = $strip.parent().wrap("<div class='tickercontainer'></div>");
        var containerWidth = $strip.parent().parent().width(); //a.k.a. 'mask' width 	
        $strip.find("li").each(function(i) {
            stripWidth += jQuery(this, i).outerWidth(true); // thanks to Michael Haszprunar
        });
        $strip.width(stripWidth);
        var totalTravel = stripWidth + containerWidth;
        var defTiming = totalTravel / settings.travelocity; // thanks to Scott Waye		
        function scrollnews(spazio, tempo) {
            $strip.animate({ left: '-=' + spazio }, tempo, "linear", function() { $strip.css("left", containerWidth); scrollnews(totalTravel, defTiming); });
        }
        scrollnews(totalTravel, defTiming);
        $strip.hover(function() {
            jQuery(this).stop();
        },
				function() {
				    var offset = jQuery(this).offset();
				    var residualSpace = offset.left + stripWidth;
				    var residualTime = residualSpace / settings.travelocity;
				    scrollnews(residualSpace, residualTime);
				});
    });
};