$(window).load(function() {
    // find all containers with the imageAdjust class and center images horizonally and vertically
    $(".imageAdjust").each(function(i) {
        var heightDiff = $(this).find("img").height() - $(this).height();
        var widthDiff = $(this).find("img").width() - $(this).width();

        // set image to be positioned centered/middle of container
        $(this).find("img").css("marginTop", -heightDiff / 2).css("marginLeft", -widthDiff / 2);

    });
});


$(document).ready(function() {
    var focusClass = "watermarked-focus"
    var defaultValues = new Array();

    $(".field-watermarked").each(function(i) {
        var $this = $(this);

        defaultValues[i] = $this.val();

        $this.bind("focus", function() {
            $this.addClass(focusClass);
            if ($this.val() == defaultValues[i]) {
                $this.val("");
            }
        });

        $this.bind("blur", function() {
            if (!($this.val())) {
                $this.removeClass(focusClass);
                $this.val(defaultValues[i]);
            }
            else if ($this.val() == defaultValues[i]) {
                $this.removeClass(focusClass);
            }
        });
    });
});
$(document).ready(function() {


    var defaultValues = new Array();

    $(".watermarked").each(function(i) {
        var $this = $(this);

        defaultValues[i] = $this.attr("style");

        if ($(this).val().length > 0) {
            $this.attr("style", "");
        }
        $this.bind("focus", function() {
            if (this.id == "login_txtPassword") {
                //alert("hello password");
                $(this).removeAttr("style");
            }
            else if (this.id == "login_txtUsername") {
                //alert("hello username");
                $(this).removeAttr("style");
            }

        });

        $this.bind("blur", function() {
            if (!($this.val())) {

                $this.attr("style", defaultValues[i]);

            }
        });
    });



    //$(document).ready(function() {

    //    var focusClass = "watermarked-focus"
    //    var defaultValues = new Array();

    //    $(".watermarked").each(function(i) {
    //        var $this = $(this);

    //        defaultValues[i] = $this.val();

    //        $this.bind("focus", function() {
    //            $this.addClass(focusClass);
    //            if ($this.val() == defaultValues[i]) {
    //                $this.val("");
    //            }
    //        });

    //        $this.bind("blur", function() {
    //            if (!($this.val())) {
    //                $this.removeClass(focusClass);
    //                $this.val(defaultValues[i]);
    //            }
    //            else if ($this.val() == defaultValues[i]) {
    //                $this.removeClass(focusClass);
    //            }
    //        });
    //    });


    // attach Google page tracker function to all outbound links
    var account = 'UA-11998698-2';

    $("a[href^='http']").each(function(i) {
        var $this = $(this);

        $this.bind("click", function() {
            var link = $this.attr('href');


            _gaq.push(function() {
                var tracker = _gat._getTracker(account);
                tracker._trackPageview("/downloads/" + link);
            });

            //pageTracker._trackPageview("/outgoing/"+$this.attr('href'));
        });
    });


    // attach Google page tracker function to all downloads

    $("a[href$='pdf'],[href$='doc'],[href$='xls'],[href$='jpg'],[href$='gif'],[href$='ppt']").each(function(i) {
        var $this = $(this);
        $this.bind("click", function() {
            var link = $this.attr('href');

            _gaq.push(function() {
                var tracker = _gat._getTracker(account);
                tracker._trackPageview("/downloads/" + link);
            });

            // pageTracker._trackPageview("/downloads/"+$this.attr('href'));
        });       
    });


    $(".form-holder input").focus(function() {
        $(this).addClass("has-focus");
    });

    $(".form-holder textarea").focus(function() {
        $(this).addClass("has-focus");
    });

    $(".form-holder input").blur(function() {
        $(this).removeClass("has-focus");
    });

    $(".form-holder textarea").blur(function() {
        $(this).removeClass("has-focus");
    });


    /* show attendee 1 */
    $("#attendee-yes").click(function(event) {
        $("#ctl00_body_newBillingAddress").slideDown(750);
    });

    /* hide attendee 1 */
    $("#attendee-no").click(function(event) {
        $("#ctl00_body_newBillingAddress").slideUp(750);
    });

//    /* show attendee 2 */
//    $("#attendee-no-2").click(function(event) {
//        $("#register-other-2").slideDown(750);
//    });

//    /* hide attendee 2 */
//    $("#attendee-yes-2").click(function(event) {
//        $("#register-other-2").slideUp(750);
//    });

    /* show non member login */
    $("#event-non-member-login-show").click(function(event) {
        $("#non-member-login").slideDown(750);
    });

    /* show non member login */
    $(".change-password").click(function(event) {
        $(".hidden-password").slideDown(750);
    });



    $(".checkMe").click(function() {
        $('#chk_pref_5941').is(':checked') ? $("#hidden-form-3").slideDown(750) : $("#hidden-form-3").slideUp(750);
        if (!$('#chk_pref_5941').is(':checked')) {
            $("INPUT[class='checkBox chkKIP']").attr('checked', $('#chk_pref_5941').is(':checked'));
            $("#checkAll").attr('checked', $('#chk_pref_5941').is(':checked'));
        }
    });

    // check all check boxes
    $('#checkAll').click(function() {
        $("INPUT[class='checkBox chkKIP']").attr('checked', $('#checkAll').is(':checked'));
        if (!$('#checkAll').is(':checked')) {
            $('#chk_pref_5941').attr('checked', $('#checkAll').is(':checked'));
            $("#hidden-form-3").slideUp(750);
        }
    });
    //if ($('#hidden-form-button-3').is(':checked')) { $("#hidden-form-3").slideDown(750); } else { $("#hidden-form-3").slideUp(750); }


    /* accordion open each seperatly */
    function initMenu() {

        //$('.list-items ul').hide();
        $('.list-items ul ul').show();
        $('.list-items ul:first').show();
        $('.list-items a').click(
        function() {
			if ($(this).attr("class").indexOf("icon") == -1) {
				$(this).next().slideToggle('slow');
				if ($(this).attr("style")) {
					$(this).attr("style", "");
				}
				else {
					$(this).attr("style", "background-position:right 0px;");
				}
			}
        })
    ;
        $('.list-items').prev().find('a.show').click(
        function() {
        $(this).parent().next().slideToggle('slow');
            if ($(this).attr("style")) {
                $(this).attr("style", "");
            }
            else {
                $(this).attr("style", "background-position:right 0px;");
            }
        })
    ;

        $('a.list-items-show').click(
        function() {
            $(this).parent().next().slideToggle('slow');
            if ($(this).attr("style")) {
                $(this).attr("style", "");
            }
            else {
                $(this).attr("style", "background-position:right 0px;");
            }
        })
    ;
    }

    initMenu();



});


/*
* Popup window script 
*  
* Example markup 
* <a href="mypage.html" onclick="popUpWin(this.href,'console',660,520);return false;" title="Open popup window">Link text</a>
*/
var newWindow = null;
function closeWin() {
    if (newWindow != null) {
        if (!newWindow.closed)
            newWindow.close();
    }
}

function popUpWin(url, type, strWidth, strHeight) {
    closeWin();
    if (type == "fullScreen") {
        strWidth = screen.availWidth - 10;
        strHeight = screen.availHeight - 160;
    }
    var tools = "";
    if (type == "standard" || type == "fullScreen") tools = "resizable,toolbar=yes,location=yes,scrollbars=yes,menubar=yes,width=" + strWidth + ",height=" + strHeight + ",top=0,left=0";
    if (type == "console") tools = "resizable,toolbar=no,location=no,scrollbars=yes,width=" + strWidth + ",height=" + strHeight + ",left=0,top=0";
    newWindow = window.open(url, 'newWin', tools);
    newWindow.focus();
}


function ShowSection(parentElement, selectedElement) {
    alert(parentElement + " " + selectedElement.value);

    // hide all child div's inside section_area div 
    $("#" + parentElement + " > div").hide();
    // show the element	
    $("#" + selectedElement.value).show();

}







/****************** EMAIL DECRYPTION *********************/
function decrypt_string(crypted_string) {

    var numbers = crypted_string.split(' ');
    n = numbers[0]; decryption_key = numbers[1];
    numbers[0] = ""; numbers[1] = "";
    crypted_string = numbers.join(" ").substr(2);


    var decrypted_string = '';
    var crypted_characters = crypted_string.split(' ');
    var i;

    for (i = 0; i < crypted_characters.length; i++) {
        var current_character = crypted_characters[i];
        var decrypted_character = exponentialModulo(current_character, n, decryption_key);
        decrypted_string += String.fromCharCode(decrypted_character);
    }

    return decrypted_string;
}


// Finds base^exponent % y for large values of (base^exponent)
function exponentialModulo(base, exponent, y) {
    if (y % 2 == 0) {
        answer = 1;
        for (var i = 1; i <= y / 2; i++) {
            temp = (base * base) % exponent;
            answer = (temp * answer) % exponent;
        }
    } else {
        answer = base;
        for (var i = 1; i <= y / 2; i++) {
            temp = (base * base) % exponent;
            answer = (temp * answer) % exponent;
        }
    }
    return answer;
}

function sendEmail(dat) {
    document.location.href = 'mailto:' + do_decrypt(dat);
}


function do_decrypt(dat) {
    return decrypt_string(dat, 0, 0, '');
}



function isNumberKey(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode == 13 || charCode == 46 || (charCode >= 96 && charCode <= 105) || (charCode >= 37 && charCode <= 40)) {
        return true;
    } else
        if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

    return true;
}


function OpenBugWindow() {
    var oWinBug;
    var url = "http://www.webling.com.au/SubmitBug/?email=ifsa.support@webling.com.au&url=" + document.location.href;
    oWinBug = window.open(url, "BugWindow", "top=10,left=10,scrollbars=yes, width=550, height=390;");
}

function bookmark()
{
 bookmarkurl=window.location;
 bookmarktitle=document.title;
 if (document.all)
 window.external.AddFavorite(bookmarkurl,bookmarktitle)
 else if (window.sidebar) // firefox
 window.sidebar.addPanel(bookmarktitle, bookmarkurl, "");
}
