/*
 * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*jslint devel: true*/
/*global tizen, $*/

/**
* @class Helpers
*/
function Helpers() {
    'use strict';
    return;
}

(function strict() { // strict mode wrapper
    'use strict';

    Helpers.prototype = {
       /**
        * Returns date in format YYYY/MM/DD
        * @param {Date} dateObj
        * @returns {string} formatted
        */
        getShortDate: function Helpers_getShortDate(dateObj) {
            var dd, mm, yyyy;

            try {
                yyyy = dateObj.getFullYear().toString();
                mm = (dateObj.getMonth() + 1).toString(); // January === 0
                dd  = dateObj.getDate().toString();
            } catch (e) {
                console.error('error', e);
            }

            // return in format yyyy/mm/dd
            return yyyy + '/' + (mm[1] ?
                    mm :
                    '0' + mm[0]) + '/' + (dd[1] ? dd : '0' + dd[0]);
        },

       /**
        * Returns date in format DD short_month YYYY
        * @param {Date} dateObj
        * @returns {string} formatted
        */
        toNativeDate: function Helpers_toNativeDate(dateObj) {
            var date = dateObj.toDateString().split(' ');
            return date[2] + ' ' + date[1] + ' ' + date[3];
        },

       /**
        * Returns time in format HH:mm or hh:mm ap
        * @param {Date} dateObj
        * @returns {string} formatted
        */
        toNativeTime: function Helpers_toNativeDate(dateObj) {
            var hours, apHours;
            if (tizen.time.getTimeFormat().indexOf('ap') !== -1) {
                hours = dateObj.getHours();
                apHours = hours % 12 < 10 ? '0' + hours % 12 : hours % 12;

                return apHours.toString().replace('00', '12') + ':' +
                    (dateObj.getMinutes() < 10 ? '0' + dateObj.getMinutes() :
                            dateObj.getMinutes()) +
                            (hours > 11 ? ' PM' : ' AM');
            }
            return dateObj.toTimeString().substring(0, 5);
        },

       /**
        * Seconds to hours converter
        *
        * @param {number} seconds
        * @returns {string}
        */
        secondsToHours: function Helpers_secondsToHours(seconds) {
            var str = '';
            str = ((seconds % 60 < 10) ? '0' : '') + (seconds % 60);
            seconds = parseInt(seconds / 60, 10);
            str = ':' + str;
            str = ((seconds % 60 < 10) ? '0' : '') + (seconds % 60) + str;
            seconds = parseInt(seconds / 60, 10);
            str = ':' + str;
            str = ((seconds % 24 < 10) ? '0' : '') + (seconds % 24) + str;
            seconds = parseInt(seconds / 24, 10);
            return str;
        },

       /**
        * Returns scroll position of the given scroll view.
        * @param {object} element ScrollView element
        * @returns {number} scroll position
        */
        getScrollPosition: function Helpers_getScrollPosition(element) {
            return element.scrollview('getScrollPosition').y;
        },

       /**
        * Scrolls given jQuery ScrollView to given Y position.
        * @param {object} element ScrollView element to be scrolled.
        * @param {number} position Y position to scroll to
        */
        scrollTo: function Helpers_scrollTo(element, position) {
            // don't allow scrolling too much
            var maxPosition =
                element.children().first().outerHeight(true) -
                element.height();
            if (maxPosition < 0) {
                maxPosition = 0;
            }
            if (position > maxPosition) {
                position = maxPosition;
            }
            if (position < 0) {
                position = 0;
            }
            // scroll
            element.scrollview('scrollTo', 0, -position);
        }

    };

}());
