﻿//FancyBox
$(document).ready(function () {

    $("div#eyeFocus").fancybox({
		'transitionIn'	:	'fade',
		'transitionOut'	:	'fade',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	false,
        'href'          :   '/CurrentSearch.html',
        'type'          :   'inline',
        'overlayShow'   :   'true',
        'titleShow'     :   'false'
	});
});


//Links for case columns on misc subpages
$(document).ready(function () {
    $("div.column").mousedown(
        function (event) {
            window.location = $(this).find('a').attr('href')
        }
    );
});

//Print function
$(document).ready(function () {
    $("#lnkPrint").click(function () { window.print(); })
});

//UserControls UX
$(document).ready(
    function (event) {
        $('input.required').focus(function () {
            if (this.value == this.defaultValue) {
                this.value = '';
            }
            if (this.value != this.defaultValue) {
                this.select();
            }
        });

        $('input.required').blur(function () {
            if ($.trim(this.value) == '') {
                this.value = (this.defaultValue ? this.defaultValue : '');
            }
        });

        $('input.required2').focus(function () {
            if (this.value == this.defaultValue) {
                this.value = '';
            }
            if (this.value != this.defaultValue) {
                this.select();
            }
        });

        $('input.required2').blur(function () {
            if ($.trim(this.value) == '') {
                this.value = (this.defaultValue ? this.defaultValue : '');
            }
        });

    });

//Do the login in the top via mouseclick
$(document).ready(function () {
    $("#btnExtLogOn").click(function () {
        if (!$("#frmExtLogOn").valid())
            return;
        $.ajax({
            type: "POST",
            url: '/AddStatistic',
            data: $("#iptUserName").attr("name") + "=" + $("#iptUserName").val() + "&" + $("#iptPassword").attr("name") + "=" + $("#iptPassword").val(),
            success: function (result) {
                $("#frmExtLogOn").submit();
            },
            error: function (req, status, error) {
                alert("Sorry! We could not receive your feedback at this time.");
            }
        });
    });
    $("#frmExtLogOn").validate();
});

//Do the login in the top via enter
$(document).ready(function () {
    $('#frmExtLogOn').keypress(function(event) {
      if (event.keyCode == '13') {
         if (!$("#frmExtLogOn").valid())
                return;
            $.ajax({
                type: "POST",
                url: '/AddStatistic',
                data: $("#iptUserName").attr("name") + "=" + $("#iptUserName").val() + "&" + $("#iptPassword").attr("name") + "=" + $("#iptPassword").val(),
                success: function (result) {
                    $("#frmExtLogOn").submit();
                },
                error: function (req, status, error) {
                    alert("Sorry! We could not receive your feedback at this time.");
                }
            });
        }
        $("#frmExtLogOn").validate();  
    });
});

//Index page block mouseover
$(document).ready(function () {    
    $("div.block").mouseenter(
        function (event) {
                $(this).animate({ backgroundColor: '#F2F2F2', color: '#ffffff' }, 300)                
            }
        );        
    $("div.block").mouseleave(
        function (event) {
                $(this).animate({ backgroundColor: '#E8E8E8', color: '#ffffff' }, 300)                
            }
        );
});

//Index page block mousedown
$(document).ready(function () {
    $("div.block").mousedown(
        function (event) {
            window.location = $(this).find('a').attr('href')
        }
    );
});

//Initiate (and play) video clips
    $(document).ready(function () {

        //Initiate video on page
        var hbc = {
            player: {
                id: "ytPlayer",
                init: function (videoID) {
                    var params = { allowScriptAccess: "always", wmode: "transparent", allowfullscreen: "true" };
                    var atts = { id: this.id };
                    swfobject.embedSWF("http://www.youtube.com/v/" + videoID + "&enablejsapi=1&rel=0&fs=1&cc_load_policy=1",
								"divPlayer", "448", "277", "8", null, null, params, atts);
                                
                },
                loadVideo: function (videoID, startSeconds) {                    
                    document.getElementById("ytPlayer").loadVideoById(videoID, parseInt(startSeconds), "default");
                }
            }
        };

        //Get url
        var url = window.location.pathname;
        
        //Declare startvideo depending on url
        if (url == "/agency-search/adstrument") {
            //Adstrument Video            
            var videoID = "ABKRNEZX3QQ";
        }
        else if (url == "/agency-search/process") {            
            //Search Process
            var videoID = "lGiuXiEYuNY";
        }

        //Initiate videoplayer
        hbc.player.init(videoID);
        loadVideo = function (startSeconds, newVideoID) {
            hbc.player.loadVideo(newVideoID, parseInt(startSeconds));
        };
    });

//Viewpoint Subscription
    $(document).ready(function () {
        $("#btnSubscribe").click(function () {

            if ($("#btnSubscribe").attr("class") == "button") {
                if ($("#frmSubscription").valid()) {
                    if ($("#iptUserName1").val() == "Full Name (optional)") {                        
                        var FullName = "Subscriber";
                    }
                    else {
                        var FullName = $("#iptUserName1").val();
                    }

                    $.ajax({
                        type: "POST",
                        url: '/market-insights/AddSubscription',
                        data: $("#iptUserName1").attr("name") + "=" + FullName + "&" + $("#iptEmail1").attr("name") + "=" + $("#iptEmail1").val(),
                        success: function (result) {
                            $("#divConfirmedMessage").html("<p class='red'>" + $("#iptEmail1").val() + " has successfully signed up.</p>");
                            hiddenInputElements("divConfirmedMessage", true)
                            $("#divInfoText").css("display", "none")
                        },
                        error: function (req, status, error) {
                            alert(req.responseText);
                            alert("Your subscription could not be completed.");
                        }
                    });
                }
            } else {
                hiddenError();
                $("#divUserName1").css("display", "");
                $("#btnSubscribe").attr("class", "button");
                $("#btnUnsubscribe").attr("class", "notbutton");
                $("#divInfoText").css("display", "block");
            }
        });
        $("#btnUnsubscribe").click(function () {
            if ($("#btnUnsubscribe").attr("class") == "button") {                
                $("#iptUserName1").val("stub");
                if ($("#frmSubscription").valid()) {
                    $.ajax({
                        type: "POST",
                        url: '/market-insights/DeleteSubscription',
                        data: $("#iptEmail1").attr("name") + "=" + $("#iptEmail1").val(),
                        success: function (result) {
                            if (result.emaiIsExists) {
                                $("#divConfirmedMessage").html("E-mail has been removed from the mailing list");
                                hiddenInputElements("divConfirmedMessage", true);
                            }
                            else {
                                $("#divEmail1").append("<label for='iptEmail1' class='error1'>e-mail does not exist.</label>");
                                hiddenInputElements("divConfirmedMessage", false);
                            }
                        },
                        error: function (req, status, error) {
                            alert("The email you have entered could not be found in the database.");
                        }
                    });
                }
            } else {
                hiddenError();
                $("#divUserName1").css("display", "none");
                $("#btnSubscribe").attr("class", "notbutton");
                $("#btnUnsubscribe").attr("class", "button");
                $("#divInfoText").css("display", "none");
            }
        });
        hiddenError = function () {
            $("#frmSubscription div > label").css("display", "none");
        };
        hiddenInputElements = function (id, flag) {
            $("#" + id).css("display", "");
            if (flag) {
                $("#divUserName1").css("display", "none");
                $("#divEmail1").css("display", "none");
                $("#btnSubscribe").css("display", "none");
                $("#btnUnsubscribe").css("display", "none");
            }
        }

        $("#frmSubscription").validate();
    });


//Browser War
$(document).ready(function () {
    if ( $.browser.msie ) {
        if ($.browser.version == "6.0") {
            alert("Please upgrade your browser. This website will not work in IE 6.0!");
        }
    }
});
