﻿//Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function (from, to) {
    var rest = this.slice((to || from) + 1 || this.length);
    this.length = from < 0 ? this.length + from : from;
    return this.push.apply(this, rest);
};

String.format = function () {
    if (arguments.length == 0) return null;
    var str = arguments[0];
    for (var i = 1; i < arguments.length; i++) {
        var re = new RegExp('\\{' + (i - 1) + '\\}', 'gm');
        str = str.replace(re, arguments[i]);
    }
    return str;
};

var getCumulativeOffset = function (obj) {
    var left, top;
    left = top = 0;
    if (obj.offsetParent) {
        do {
            left += obj.offsetLeft;
            top += obj.offsetTop;
        } while (obj = obj.offsetParent);
    }
    return {
        x: left,
        y: top
    };
};

var baseurl = String.format("{0}//{1}", window.location.protocol, window.location.host);

var pagedata = []; //for the cache

var aff = {
    _ga: function (action, id, value) {
        if (typeof id === 'undefined') {
            id = '';
        }

        if (typeof value === 'undefined') {
            value = 0;
        }

        _gaq.push(['_trackEvent', 'Ipekyol', action, id, value]);

        //_gaq.push(['_trackEvent', 'category', 'action', 'opt_label', opt_value]);
    },
    _log: function (log) {
        if (window.console) {
            console.log(log);
        }
    },
    _isFunction: function (funcTest) {
        return typeof funcTest === 'function';
    },
    /** 
    * Get query string value
    * @param string qs: querystring key
    */
    _getQueryString: function (qs) {
        hu = window.location.search.substring(1);
        gy = hu.split("&");
        for (i = 0; i < gy.length; i++) {
            ft = gy[i].split("=");
            if (ft[0] == qs) {
                return ft[1];
            }
        }
    },
    /**
    * base ajax send request
    */
    _sendRequest: function (opt) {
        $.ajax({
            url: String.format('{0}/service/service.ashx?command={1}&{2}', baseurl, opt.command, new Date().getTime()),
            data: opt.data,
            contentType: "application/json; charset=utf-8",
            dataType: 'json',
            success: function (result) {
                if (aff._isFunction(opt.success)) {
                    opt.success(result);
                }
            },
            error: function (result) {
                if (aff._isFunction(opt.error)) {
                    opt.error(result);
                }
            }
        });
    },
    /**
    * cache page data (will not ajax request)
    */
    _savePageData: function (data) {
        var hasAdded = $.grep(pagedata, function (item) {
            return item.FriendlyUrl === data.FriendlyUrl;
        });
        if (hasAdded && hasAdded.length > 0) {
            return;
        }
        pagedata.push(data);
    },
    _getPageData: function (url) {
        var pageItem = $.grep(pagedata, function (item) {
            return item.FriendlyUrl === url;
        });

        if (pageItem && pageItem.length > 0) {
            return pageItem;
        } else
            return null;
    },
    _toVeritcalCenterElement: '',
    _toVerticalCenter: function (element) {
        if (aff._toVeritcalCenterElement === '') {
            aff._toVeritcalCenterElement = $(element);
        }
        var window_height = document.documentElement.clientHeight;
        var content_height = aff._toVeritcalCenterElement.height();
        var toppadding = ((window_height - content_height) / 2)
        if (toppadding < 0) {
            toppadding = 0;
            $('html').css({ overflow: 'auto' });
        } else {
            $('html').css({ overflow: 'hidden' });
        }

        toppadding += "px";
        aff._toVeritcalCenterElement.css("margin-top", toppadding)
        // $(window).bind("resize", aff._toVerticalCenter);
    }
};


