﻿//image-stretch
$(document).ready(function(){
    $("#page-wrapper").addClass("js");
    
    $(".main-content-wrapper > img.js-stretch").stretch();
});

jQuery.fn.stretch = function() {
    return this.each(function() {
        var _img = $(this);
        _img.siblings("div.main-content").show();
        _img.height(_img.siblings("div.main-content").height());
    });
};

//send to a friend
$(document).ready(function(){
    $("#page-wrapper").addClass("js");
    
    var _sendModal = $("#send-modal", "body");
    var _sendFriend = $("#send-to-a-friend", "body");
    
    _sendModal.fadeTo(0,0);
    _sendFriend.fadeTo(0,0);
    
    $("li.send-to-a-friend > a", "#bottom-bar").click(function()
    {
        resizeModal();
        _sendModal.show().fadeTo("slow", 0.9);
        _sendFriend.show().fadeTo("slow", 1);
        
        //bind the non mousewheel plugin on click
        //$("body").css("overflow", "hidden");
        $(window).scroll(function(event){
            event.preventDefault();
        });
        $("#send-modal, #send-to-a-friend").mousewheel(function(event){
            event.preventDefault();
        }); 
        
        //reset form elements
        $("#result").empty().removeAttr("class");
        $("div.form-element input:text, div.form-element textarea", "#form-elements").each(function(index, item)
        {
            if($(item).attr("id") == "verification")
            {
                var defaultText = "Please enter the verification code here.";
                $(item).val(defaultText);
                $(item).click(function(){
                    if($(item).val() == defaultText)
                    {
                        $(item).val("");
                    }
                });
                $(item).blur(function(){
                    if($(item).val().length == 0)
                    {
                        $(item).val(defaultText);
                    }
                });
            }
            else
            {
               $(item).val("");
            }
        });
        
        $("#form-elements").show();
        return false;        
    });
    
    $("#close > img", "#send-to-a-friend").click(function()
    {
        //unbind the non mousewheel plugin on close
        //$("body").css("overflow", "visible");
    
        _sendModal.fadeTo("slow", 0);
        _sendFriend.fadeTo("slow", 0, function(){_sendModal.hide();_sendFriend.hide();});
        return false;
    });
    
    $(window).resize(function(){resizeModal();});
});    

function resizeModal()
{
    $("#send-modal").width($(window).width());
    $("#send-modal").height($(window).height());
    $("#send-modal").css({"top": $(window).scrollTop() + "px", "left":  $(window).scrollLeft() + "px"});
    //$("#send-to-a-friend").scrollTop($(window).scrollTop());
    
    var sendPosTop = ($(window).scrollTop() + ($(window).height() / 2)) - ($("#send-to-a-friend").height() / 2);
    var sendPosLeft = ($(window).scrollLeft() + ($(window).width() /2)) - ($("#send-to-a-friend").width() /2);
    
    $("#send-to-a-friend").css({"top": sendPosTop + "px", "left": sendPosLeft + "px"});
}

/*The below code is for Mouse Wheel Extension 3.0.2*/

/* Copyright (c) 2009 Brandon Aaron (http://brandonaaron.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
 * Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
 *
 * Version: 3.0.2
 * 
 * Requires: 1.2.2+
 */
(function(c){var a=["DOMMouseScroll","mousewheel"];c.event.special.mousewheel={setup:function(){if(this.addEventListener){for(var d=a.length;d;){this.addEventListener(a[--d],b,false)}}else{this.onmousewheel=b}},teardown:function(){if(this.removeEventListener){for(var d=a.length;d;){this.removeEventListener(a[--d],b,false)}}else{this.onmousewheel=null}}};c.fn.extend({mousewheel:function(d){return d?this.bind("mousewheel",d):this.trigger("mousewheel")},unmousewheel:function(d){return this.unbind("mousewheel",d)}});function b(f){var d=[].slice.call(arguments,1),g=0,e=true;f=c.event.fix(f||window.event);f.type="mousewheel";if(f.wheelDelta){g=f.wheelDelta/120}if(f.detail){g=-f.detail/3}d.unshift(f,g);return c.event.handle.apply(this,d)}})(jQuery);