var mplsMagLrgImgWidth;
$(function(){
    $('div#mplsMagImgHover').show();
    var mplsMagLrgImgWidth = $('div#mplsMagImgHover img').width();
    $('div#mplsMagImgHover').hide();
    
    if ($("#timeleftunix").val()) {
        //dealTimer($("#timeleftunix").val() + '000');
        dealTimer($("#timeleftunix").val() * 1000);
    }

    $("#buy_as_gift a").click(function(){
        var d = new Date(),
            sec = 60;
        d.setTime(d.getTime() + (sec * 1000));
        document.cookie = 'giftpopup=true; expires=' + d.toGMTString() + '; path=/';
    });

    //$(".share-links a.twitter").shortenUrl(function(data){
    /*$("a#mainurl").shortenUrl(function(data){
        var href = ($(".share-links a.twitter").attr('href')),
            pattern = new RegExp("http[s]?://(?:www\.)?twitter.com/.*?(http://.*$)"),
            first,
            extr = href.match(pattern),
            new_href;

        for ( var r in data ) {
            first = data[r]; break;
        }

        new_href = href.replace(extr[1], (first.shortUrl));
        $(".share-links a.twitter").attr('href', new_href);
    });*/

    /*$('iframe.twitter-share-button').load(function(){
        $(this).load(function(){
            alert($(this).contents().find('body').text);
        });
    });*/

    $(".share-links a.facebook").click(function(){
        if (FB.Connect.get_status().result == 1) {
            FB.Connect.showShareDialog($(".share-links a.facebook input").val(), function(){});
            return false;
        } else {
            return true;
        }
    });

    $("#emailthisform").submit(function(){
        var to = $("#emailthisto").val(),
            from = $("#emailthisfrom").val(),
            code = $("#emailthiscode").val(),
            deal = $("#emailthisdeal").val(),
            valid = true;

    cchildren().css('cursor', 'pointer');children().css('cursor', 'pointer');hildren().css('cursor', 'pointer');    $("#emailthisform label .err").html('');
        $("#emailthiserr").html('');
        $("#emailthisgood").html('');

        if ((to+'').length < 1) {
            $("label[for=emailthisto] .err").html('Required');
            valid = false;
        }

        if ((from+'').length < 1) {
            $("label[for=emailthisfrom] .err").html('Required');
            valid = false;
        }

        if ((code+'').length < 1) {
            $("label[for=emailthiscode] .err").html('Required');
            valid = false;
        }

        if (!valid) {
            return false;
        }

        $("#emailthisimg").attr('src', '').hide();
        $.post("/emailthis", {"to":to,"from":from,"code":code,"deal":deal}, function(data){
            $("#emailthisimg").attr('src', '/images/captcha.png').show();
            $("#emailthiscode").val('');
            if (data.stat) {
                $("#emailthisto").val(''),
                $("#emailthisfrom").val(''),
                $("#emailthisdialog").dialog('close');
                $("#dialog").html(data.msg).dialog({
                    modal:true,
                    resizable:false,
                    draggable:false,
                    buttons:{
                        'Close':function(){
                            $(this).dialog('close');
                        }
                    },
                    close:function(){
                        $(this).html('').dialog('destroy');
                    }
                });

            } else {

                $("#emailthiserr").html(data.msg);
            }
        },"json");

        return false;
    });

    $("#emailthisdialog").dialog({
        title: "Email This",
        width: 350,
        draggable:false,
        resizable: false,
        autoOpen: false,
        modal: true,
        buttons: {
            "Send": function(){
                $("#emailthisform").submit();
            },
            "Cancel": function(){
                $(this).dialog('close');
            }
        }
    });
    
    $('img#mplsMagImg').mousemove(
        function(e)
        {
            //make the browser load the image
            var cacheImage = document.createElement('img');
            cacheImage.src = $('div#mplsMagImgHover img').attr('src'); 
            
            var x = e.pageX-mplsMagLrgImgWidth-5;
            var y = e.pageY+5;
            
            $('div#mplsMagImgHover').css({
                'left': x,
                'top': y
            });
            $('div#mplsMagImgHover').show();
        }
    );

    $('img#mplsMagImg').mouseout(
        function(e)
        {
            $('div#mplsMagImgHover').hide();
        }
    );
});

function emailThis()
{
    $("#emailthisdialog").dialog('open');
}

function prependZero(val)
{
    val = val + '';
    return val.length == 1 ? 0 + val : val;
}

function dealTimer(val)
{
    var sUnix = $('#nowunix').val();
    var cUnix = Math.round(new Date().getTime() / 1000);
    var offset = sUnix - cUnix;
    var end = new Date();
    end.setTime(val);

    function doIt()
    {
        var now = new Date();
        var timeLeft = Math.floor((end.getTime() / 1000) - ((now.getTime() / 1000) + offset));
        var timeLeftMinutes = Math.floor(timeLeft / 60);
        var timeLeftHours = Math.floor(timeLeftMinutes / 60);
        var timeLeftDays = Math.floor(timeLeftHours / 24);
        var days = prependZero(timeLeftDays);
        var hours = prependZero(timeLeftHours - timeLeftDays * 24);
        var minutes = prependZero(timeLeftMinutes - timeLeftHours * 60);
        var seconds = prependZero(timeLeft - (timeLeftMinutes * 60));
        var str = days+':'+hours+':'+minutes+':<span class="seconds">'+seconds+'</span>';

        if (timeLeft >= 0) {
            $("#timeleft").html(str);
            setTimeout(function(){
                doIt();
            }, 500);
        } else {
            window.location = window.location;
        }
    }

    doIt();
}

