
// page is ready...
$(document).ready(function () {
    PageBind();

    BindLinkSwitcher();

});

function PageBind() {

    // give focus to the appropriate input field
    $(".focus-input").focus();

    // Textbox hint handlers
    BindTextboxHints();
     
    // Bind input focus
    BindInputLabelHighlight();

    // Bind table events
    BindTableEvents();

    // Bind date filter events
    BindFilterEvents();

    // Bind CC Check Boxes
    BindCCBoxes();

    // Bind Price Point selects
    BindPricePointDDs();

    // Bind the Service Cover Design page
    BindCoverServices();

    // Bind the custom checkboxes and radio buttons
    StyleCustomInputs();

}

function BindCoverServices() {
    $('.mockup-choice input').change(function () {
        if ($(this).val() == "No") {
            $(".mockup-upload-container").addClass("hide-swf");
        } else {
            // make sure that Flash is installed; otherwise display modal with link to player...
            if (!FlashDetect.installed) {
                $.openModal({ target: '#flash-info' });
            } else {
                $(".mockup-upload-container").removeClass("hide-swf");
            }   // else
        }   // else
    });
    $('.imagery-choice input').change(function () {
        if ($(this).val() == "No") {
            $(".imagery-yes-container").addClass("hide-swf");
            $(".imagery-no-container").removeClass("hide-swf");
            $(".txt-imagery-yes").removeClass("req-field");
            $(".txt-imagery-no").addClass("req-field");
        } else {
            // make sure that Flash is installed; otherwise display modal with link to player...
            if (!FlashDetect.installed) {
                $.openModal({ target: '#flash-info' });
            } else {
                $(".imagery-no-container").addClass("hide-swf");
                $(".imagery-yes-container").removeClass("hide-swf");
                $(".txt-imagery-no").removeClass("req-field");
                $(".txt-imagery-yes").addClass("req-field");
            }   // else
        }   // else
    });

    // make sure that the correct textarea has the required field class on it...
    if ($('.imagery-choice input:checked').val() == "No") {
        $(".txt-imagery-yes").removeClass("req-field");
        $(".txt-imagery-no").addClass("req-field");
    } else {
        $(".txt-imagery-no").removeClass("req-field");
        $(".txt-imagery-yes").addClass("req-field");
    }   // else

}   // BindCoverServices()

function BindPricePointDDs() {
	
	//Fade all out
	$('.book-tier-free').fadeOut(0);
	$('.book-tier-99').fadeOut(0);
	$('.book-tier-1299').fadeOut(0);

	// check initial value for message
	var price = $('.book-tier-dd').val();
	if (price == 00) {
		$('.book-tier-free').fadeIn(200);
	}
	else if (price < 1.99) {
		$('.book-tier-99').fadeIn(200);
	}
	else if (price > 12.99) {
		$('.book-tier-1299').fadeIn(200);
	}

	$('.book-tier-dd').change(function () {
		//attach the same code to the change event on the dropdown
		
		//Fade all out
		$('.book-tier-free').fadeOut(0);
		$('.book-tier-99').fadeOut(0);
		$('.book-tier-1299').fadeOut(0);

		// check initial value for message
		var price = $('.book-tier-dd').val();
		if (price == 00) {
			$('.book-tier-free').fadeIn(200);
		}
		else if (price < 1.99) {
			$('.book-tier-99').fadeIn(200);
		}
		else if (price > 12.99) {
			$('.book-tier-1299').fadeIn(200);
		}
	});

}   // BindPricePointDDs()

function BindLinkSwitcher() {
    $("#switch-views").click(function () {
        if ($(this).text() === "view sales by partner") {
            $("#author-container").fadeOut("300", function () {
                $("#partner-container").fadeIn("300", function () {
                    $("#switch-views").text("view sales by author");
                    $("#switch-views").parent().find("span").eq(0).text("Sales by Partner");
                });
            });
        } else {
            $("#partner-container").fadeOut("300", function () {
                $("#author-container").fadeIn("300", function () {
                    $("#switch-views").text("view sales by partner");
                    $("#switch-views").parent().find("span").eq(0).text("Sales by Author");
                });
            });
        }   // else
    });
}   // BindLinkSwitcher

function BindTextboxHints() {
    $(".txt-signup").focus(function () {
        if ($(this).val() === "Email Address") { $(this).removeClass("hinted").val(""); }
    });
    $(".txt-signup").blur(function () {
        if ($(this).val() === "") { $(this).addClass("hinted").val("Email Address"); }
    });
}   // BindTextboxHints()

function BindInputLabelHighlight() {
}   // BindInputLabelHighlight()

function BindTableEvents() {
    $(".data-table tbody tr").not("#total-row, .no-hover, .paid-book, .partners").hover(
        function () {
            if ($(this).find(".warning-image").length > 0) {
                $(this).addClass("highlighted-error-row");
            } else {
                $(this).addClass("highlighted-row");
            }   // else
        },
        function () {
            if ($(this).find(".warning-image").length > 0) {
                $(this).removeClass("highlighted-error-row");
            } else {
                $(this).removeClass("highlighted-row");
            }   // else
        }
    );
    $(".data-table tbody tr.paid-book").hover(
        function () {
            $(this).addClass("highlighted-row");
            $(this).next().addClass("highlighted-row");
        },
        function () {
            $(this).removeClass("highlighted-row");
            $(this).next().removeClass("highlighted-row");
        }
    );
    $(".data-table tbody tr.partners").hover(
        function () {
            $(this).addClass("highlighted-row");
            $(this).prev().addClass("highlighted-row");
        },
        function () {
            $(this).removeClass("highlighted-row");
            $(this).prev().removeClass("highlighted-row");
        }
    );
}   // BindTableEvents()

function BindFilterEvents() {
    $('.filter-by').click(function () {
        if ($(".datefilter").css("display") == "block") {
            $(".datefilter").slideUp("300", function () {
            });
            $(".filter-by").removeClass("filter-by-clicked");
            $(".filter-by").text("filter by date");
            $(".datefilter .input-error").css("display", "none");
        } else {
            $(".datefilter").slideDown("300", function () {
            });
            $(".filter-by").addClass("filter-by-clicked");
            $(".filter-by").text("cancel");
        }   // else
        return false;
    });

    $('.cancel-filter').click(function () {
        $(".datefilter").slideUp("300", function () {
            $(".filter-by").removeClass("filter-by-clicked");
        });

        return false;
    });

    $("#from").change(function () {
        $(".txtFilterStart").val($(this).val());
    });

    $("#to").change(function () {
        $(".txtFilterEnd").val($(this).val());
    });

    
}   // BindCCBoxes()

function BindCCBoxes() {
    $('.card-boxes input').change(function () {
        if ($(".card-boxes input:checked").size() > 0) {
            //$(".use-saved-button").css("display", "block");
            $(".card-boxes input:checked").not(this).attr('checked', false);
        } else {
            //$(".use-saved-button").css("display", "none");
        }   // else
    });
}   // BindCCBoxes()


function StyleCustomInputs() {

    $(".checkbox, .checkbox input, .radio, .radio input, .radio-button-list input").each(function (i) {
        if ($(this).is('[type=checkbox],[type=radio]')) {

            var inputElement = $(this);

            // get the associated label using the input's id
            var label = $("label[for='" + inputElement.attr('id') + "']");

            //get type, for classname suffix 
            var inputType = (inputElement.is('[type=checkbox]')) ? 'checkbox' : 'radio';

            // wrap the input + label in a div 
            $('<div class="custom-' + inputType + '"></div>').insertBefore(inputElement).append(inputElement, label);

            // find all inputs in this set using the shared name attribute
            var allInputs = $("input[name='" + inputElement.attr('name') + "']");

            // necessary for browsers that don't support the :hover pseudo class on labels
            label.hover(
			        function () {
			            $(this).addClass('hover');
			            if (inputType == 'checkbox' && inputElement.is(':checked')) {
			                $(this).addClass('checkedHover');
			            }
			        },
			        function () { $(this).removeClass('hover checkedHover'); }
		        );

            //bind custom event, trigger it, bind click,focus,blur events					
            inputElement.bind('updateState', function () {

                if (inputElement.is(':checked')) {
                    if (inputElement.is(':radio')) {
                        allInputs.each(function () {
                            $('label[for=' + $(this).attr('id') + ']').removeClass('checked');
                        });
                    };
                    label.addClass('checked');
                }
                else {
                    label.removeClass('checked checkedHover checkedFocus');
                }

            })
		    .trigger('updateState')
		    .click(function () {
		        $(this).trigger('updateState');
		    })
		    .focus(function () {
		        label.addClass('focus');
		        if (inputType == 'checkbox' && inputElement.is(':checked')) {
		            $(this).addClass('checkedFocus');
		        }
		    })
		    .blur(function () { label.removeClass('focus checkedFocus'); });

            inputElement.animate({ opacity: 0 }, 1000, function () {
                $(this).fadeIn(500);
            });

        }
    });
}

// Validation functions...

function verifyUpdatesReq() {
    var returnValue = true;
    var emailVal = jQuery.trim($(".txt-signup").val());
    var isEmail = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(emailVal);
    if (emailVal == "" || emailVal == "Email Address" || !isEmail) {
        returnValue = false;

        $(".txt-signup").addClass("no-color");
        $("#newsletter-error").stop().fadeIn(500).delay(1000).fadeOut(500, function () {
            $(".txt-signup").removeClass("no-color");
        })

    }   // if
    else {
        $("#newsletter-error").slideUp();
    }   // else
    return returnValue;
}   // verifyUpdatesReq()

