﻿/// <reference path="jquery-1.4.1-vsdoc.js" />

$(document).ready(function () {

    /// ####################################################################################
    ///Add slider  #########################################################
    /// ####################################################################################
    $('#imgslider').cycle({
        fx: 'scrollLeft',
        speed: 1200,
        timeout: 6000,
       pagerEvent: 'mouseover',
        pagerAnchorBuilder: function (idx, slide) {
            // return selector string for existing anchor 
            return '#thumbs li:eq(' + idx + ') a';
        }
    });

    $('#thumbs').cycle({
        fx: 'scrollRight',
        speed: 1200,
        timeout: 6000
    }); 

    /// ####################################################################################
    ///Add rollover  #########################################################
    /// ####################################################################################
 //   $('#thumbs img').tooltip({
      //  position: "bottom center",
      //  effect: 'slide'
   // });


    $('#thumbs img').mouseover(function () {

        $(this).fadeTo('200', 0.5)
    });

    $('#thumbs img').mouseleave(function () {

        $(this).fadeTo('200', 1)
    });


   // $('#thumbs img').click(function () {
   //     var pid = $(this).attr('prodid')
    //   if (pid == '2') {
     //    window.location = 'projects.aspx?projectid=2&via=tactilewonder'
      //  } else {
      //      window.location = 'collections.aspx?producttype=wallpapers&productid=' + pid
       // }
 //  });



    /// ####################################################################################
    ///Add twitter #########################################################
    /// ####################################################################################


    var url = "http://twitter.com/status/user_timeline/tactilewonder.json?count=3&callback=?";
    $.getJSON(url, function (data) {
        // $.each(data, function (i, item) {
        //$("img#profile").attr("src", item.user["profile_image_url"]); 
        $("#tweetme").html('"' + data[0].text.linkify().atify() + '"<div class="created_at">' + relative_time(data[0].created_at) + ' via ' + data[0].source + '</div>');
        // });
    });


    /// ####################################################################################
    /// Wire Up Add to mailing list button  #########################################################
    /// ####################################################################################

    $('#submit').click(function () {
        var email = document.getElementById('email')
        if (email.validity.valid) {
            $('#email').removeClass('errorfield')
            var emailaddress = $('#email').val();
            $('#loader').show();

            PageMethods.AddtoList(emailaddress, MailSucceeded)
        } else {
            $('#email').addClass('errorfield')
        }
    })


})

function MailSucceeded(result) {
    $('#loader').hide();
    // replace field with a message
    $('#emailfields').html('<span class="msg">You\'ve been added to our mailing list!</span>')
}

function AjaxFailed(result) {
    alert('error');
}




function relative_time(time_value) {
    var values = time_value.split(" ");
    time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
    var parsed_date = Date.parse(time_value);
    var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
    var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
    delta = delta + (relative_to.getTimezoneOffset() * 60);

    var r = '';
    if (delta < 60) {
        r = 'a minute ago';
    } else if (delta < 120) {
        r = 'couple of minutes ago';
    } else if (delta < (45 * 60)) {
        r = (parseInt(delta / 60)).toString() + ' minutes ago';
    } else if (delta < (90 * 60)) {
        r = 'an hour ago';
    } else if (delta < (24 * 60 * 60)) {
        r = '' + (parseInt(delta / 3600)).toString() + ' hours ago';
    } else if (delta < (48 * 60 * 60)) {
        r = '1 day ago';
    } else {
        r = (parseInt(delta / 86400)).toString() + ' days ago';
    }

    return r;
}


String.prototype.linkify = function () {
    return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function (m) {
        return m.link(m);
    });
};

String.prototype.atify = function () {
    return this.replace(/@[\w]+/g, function (m) {
        return "<a href='http://www.twitter.com/" + m.replace('@', '') + "'>" + m + "</a>";
    });
};
function PageMethod(fn, paramArray, successFn, errorFn) {
    var pagePath = 'Default.aspx'// window.location.pathname;
    //Create list of parameters in the form:  
    //{"paramName1":"paramValue1","paramName2":"paramValue2"}  
    var paramList = '';
    if (paramArray.length > 0) {
        for (var i = 0; i < paramArray.length; i += 2) {
            if (paramList.length > 0) paramList += ',';
            paramList += '"' + paramArray[i] + '":"' + paramArray[i + 1] + '"';
        }
    }
    paramList = '{' + paramList + '}';
    //Call the page method  
    $.ajax({
        type: "POST",
        url: pagePath + "/" + fn,
        contentType: "application/json; charset=utf-8",
        data: paramList,
        dataType: "json",
        success: successFn,
        error: errorFn
    })
;
}  


