var cnt = 1;
var strNewHeight;
var strNewWidth;
var thisWidth;
var thisHeight;
var galStatus;
var objPopup;
var setPicHeight;
var FirstImage = true;
var PrevImg;
var GalleryExists = false;
var GalleryExists = false;

$(function() {

    /* SOCIAL BOOKMARK PLUGIN */
    if (ResiBlogUpgrade) {
        $('.cornersBookmark').each(function(i) {
            $(this).bookmark({
                compact: true,
                addEmail: true,
                icons: 'images/bookmarks.png',
                addFavorite: true
            });
        });
        /* SOCIAL BOOKMARK PLUGIN SHOW ON HOVER .bookmarkIt */
        $('.bookmarkIt').hover(function() {
            var strThis = $(this).attr('rel');
            var offset = $('#ShareThisPost' + strThis).offset();
            $('#bookmarkContainer' + strThis).css('left', offset.left).
            css('top', offset.top + $('#ShareThisPost' + strThis).height());
            $('#bookmarkContainer' + strThis).toggle();
        }, function() {
            var offset = $('.ShareThisPost').offset();
            var strThis = $(this).attr('rel');
            $('#bookmarkContainer' + strThis).css('left', offset.left - 4).
            css('top', offset.top + $('.ShareThisPost').height() + 4);
            $('#bookmarkContainer' + strThis).toggle();
        });



        $('.GalleryControl,#FullScreen').hover(function() {
            $(this).addClass('LIHoverClass');
        }, function() {
            $(this).removeClass('LIHoverClass');
        });


        /* GALLERY NAVIGATION CONTROL CLICK TO ZOOM/ZOOMOUT */
        $('.Gallery img,#FullScreen').click(function() {
            thisWidth = (parseInt(arrWidth[cnt]) + parseInt(25));
            thisHeight = (parseInt(arrHeight[cnt]) + parseInt(25));
            strImage = $('#Image' + cnt).attr("src");
            if (objPopup) {
                objPopup.close();
            }
            objPopup = window.open(strImage, 'Image', 'resizable=no,scrollbars=no,width=' + thisWidth + ',height=' + thisHeight);
            objPopup.focus();
        });

        if (GalleryExists) {
            ResizeWidth();
        }

        $('.GalleryControl').click(function() {
            strThisRel = $("#" + this.id).attr('rel');
            cnt = $("#Count").val();
            PrevImg = '#Image' + cnt;

            piccnt = $("#TotalCount").val();

            if (strThisRel != "-1") {
                $("#Count").val(parseInt(cnt) + parseInt(1));
                if (cnt == (parseInt(piccnt) + parseInt(1))) {
                    $("#Count").val(parseInt(1));
                    galStatus = "Image " + parseInt(1) + " of " + (parseInt(piccnt) + parseInt(1));
                    cnt = parseInt(1);
                } else {
                    $("#Count").val((parseInt(cnt) + parseInt(1)));
                    galStatus = "Image " + (parseInt(cnt) + parseInt(1)) + " of " + (parseInt(piccnt) + parseInt(1));
                    cnt = (parseInt(cnt) + parseInt(1));
                }
            } else {
                if (cnt == "1") {
                    $("#Count").val((parseInt(piccnt) + parseInt(1)));
                    galStatus = "Image " + (parseInt(piccnt) + parseInt(1)) + " of " + (parseInt(piccnt) + parseInt(1));
                    cnt = (parseInt(piccnt) + parseInt(1));
                } else {
                    $("#Count").val((parseInt(cnt) - parseInt(1)));
                    galStatus = "Image " + (parseInt(cnt) - parseInt(1)) + " of " + (parseInt(piccnt) + parseInt(1));
                    cnt = (parseInt(cnt) - parseInt(1));
                }
            }

            $("#PhotoGalleryImage").text(galStatus);

            ResizeWidth();

            $(PrevImg).fadeOut(350, function() {
                $('#Image' + cnt).fadeIn(350);
            });

            return false;
        });

    }

    //Configuration Options
    var max_width = MaxWidth; 	//Sets the max width, in pixels, for every image
    var selector = '.ImageResize * img'; 	//Sets the syntax for finding the images.  Defaults to all images.
    //For images inside of a particular element (id="abc") or class (class="abc"),
    //use '.abc img' or '#abc img' respectively.  Don't leave off the img tag!!!
    //End configuration options.  You don't need to change anything below here.

    $(selector).each(function() {
        var width = $(this).width();
        var height = $(this).height();
        if (width > max_width) {
            //Set variables	for manipulation
            var ratio = (height / width);
            var new_width = max_width;
            var new_height = (new_width * ratio);

            //Shrink the image and add link to full-sized image
            $(this).height(new_height).width(new_width);

            $(this).hover(
                function() {
                    $(this).attr("title", "This image has been scaled down.\nClick to view the original image.")
                    $(this).css("cursor", "pointer");
                },
                function() {
                    //$(this).find("span:last").remove();
                }
            );

            $(this).click(function() {
                strImage = $(this).attr("src");
                //alert('Image:' + strImage);
                strTest = window.open(strImage, 'Image', 'resizable=no,scrollbars=no,width=' + (width + 25) + ',height=' + (height + 25));
                //window.location = $(this).attr("src");
            });
        } //ends if statement
    } //ends each function
	);
    

    $("#SearchForm").submit(function() {
        var SearchField = $("input:first")
        if (SearchField.val() == '' || SearchField.attr('title') == SearchField.val()) {
            alert('Please enter a search criteria.');
            return false;
        } else {
            return true;
        }
    });
    // find all the input elements with title attributes

    if (FormSubmit) {
        /* FORM SUBMIT SCROLL TO BOTTOM TO THANK YOU DIV */
        destination = $("#CommentsTable").offset().top;
        $("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination }, 750);
    } else {
        /* REPLACE FORM VALUE WITH TITLE FIELD FOR FORM HINTS ONLY IF FORM IS NOT SUBMITTED */
        $('input[title!=""],textarea[title!=""]').hint();
    }

    /* FORM SUBMIT, ON INVALID CODE ALERT AND SCROLL TO BOTTOM */
    if (InvalidCode) {
        alert('The Security Code you entered does not match.\nPlease try again.')
        destination = $("#CommentsTable").offset().top;
        $("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination }, 750);
    }

});

function ResizeWidth() {
    thisWidth = arrWidth[cnt];
    thisHeight = arrHeight[cnt];
    
    if (thisWidth > MaxWidth) {
        $("#Image" + cnt).css({
            width: MaxWidth,
            height: thisHeight * MaxWidth / thisWidth
        });
        thisWidth = MaxWidth;
        thisHeight = thisHeight * MaxWidth / thisWidth;
    }
    
    if (FirstImage) {
        setPicHeight = $('#Image1').height();
        FirstImage = false;
    } else if ($("#Image" + cnt).height() > setPicHeight) {
        $("#Image" + cnt).css({
            height: setPicHeight,
            width: thisWidth * setPicHeight / $("#Image" + cnt).height()
        });
    } 
}

function DoCustomValidation() {
    var frm = document.getElementById("form");
    if (!(frm.Purchase_Type_Buy.checked) && !(frm.Purchase_Type.checked = false)) {
        alert('Please select a Purchase Type');
        frm.Purchase_Type_Buy.focus();
        return false;
    } else {
        return true;
    }
}

function RefreshImage(valImageId) {
    var objImage = document.images[valImageId];
    if (objImage == undefined) {
        return;
    }
    var now = new Date();
    objImage.src = objImage.src.split('?')[0] + '?x=' + now.toUTCString();
}

function toggleDiv(divId) {
    var div = document.getElementById(divId);
    if (div) {
        div.style.display = (div.style.display == 'none' ? 'block' : 'none');
    }
}

function ValidateSecurityCode() {
    var strFieldName = '#SecurityCode';
    var strCodeValue = $(strFieldName).val();
    var strCodeTitle = $(strFieldName).attr('title');

    if (strCodeValue == strCodeTitle) {
        alert(strCodeTitle);
        $(strFieldName).focus();
        return false;
    } else {
        return true;
    }

}

(function($) {
    $.fn.image = function(src, f) {
        return this.each(function() {
            var i = new Image();
            i.src = src;
            i.onload = f;
            this.appendChild(i);
        });
    }   

    $.fn.hint = function(blurClass) {
        if (!blurClass) blurClass = 'blur';

        return this.each(function() {
            var $input = $(this),
                title = $input.attr('title'),
                $form = $(this.form),
                $win = $(window);

            function remove() {
                if (this.value === title && $input.hasClass(blurClass)) {
                    $input.val('').removeClass(blurClass);
                }
            }

            // only apply logic if the element has the attribute
            if (title) {
                // on blur, set value to title attr if text is blank
                $input.blur(function() {
                    if (this.value === '') {
                        $input.val(title).addClass(blurClass);
                    }
                }).focus(remove).blur(); // now change all inputs to title

                // clear the pre-defined text when form is submitted
                $form.submit(remove);
                $win.unload(remove); // handles Firefox's autocomplete
            }
        });
    };

})(jQuery);

