Array.prototype.find = function(predicate){
	for(var i = 0; i < this.length; i++){
		if(predicate(this[i])){
			return this[i];
		}
	}
	return null;
}

Array.prototype.where = function(predicate){
	var arr = [];
	for(var i = 0; i < this.length; i++){
		if(predicate(this[i])){
			arr.push(this[i]);
		}
	}
	return arr;
}

function getLen(resp){
	if(resp.ads){
		return resp.ads.length;
	}else{
		return resp.data.length;
	}
}


// audio controller
function AudioController(){
}

AudioController.prototype = {
	audio: null,
	isPlaying: false,
	init: function(){
		if(this.audio == null){
			this.audio = new Audio();
			/*return;
			var that = this;
			this.audio.addEventListener('ended', function() {
				alert(that.isPlaying);
				if(that.isPlaying){
					this.currentTime = 0;
					this.play();
				}
			}, false);*/
		}
	},
	start: function(src){
		this.init();
		//this.isPlaying = true;
		this.audio.src = src;
		this.audio.play();
	},
	stop: function(){
		this.init();
		//this.isPlaying = false;
		//alert(this.isPlaying);
		this.audio.pause();
	}
};

var hScrolls = [];
function initScrolls(){
	for(var i = 0;i<hScrolls.length;i++){
		hScrolls[i].destroy();
	}
	hScrolls = [];
	
	$(".scroller_wrapper").each(function(index, element) {
		var scrlr = new iScroll(element, { hScrollbar: false, vScrollbar: false, bounce: true, vScroll: false, hScroll: true , momentum: true });  
		hScrolls.push(scrlr);
		setTimeout(function(){ scrlr.refresh(); }, 100);
	});
}
function refreshScrolls(){
	for(var i=0;i<hScrolls.length;i++){
		hScrolls[i].refresh();
	}
}

function inflate(){	
	$("[inflate]").each(function(index, element) {
		var el = $(element);
		var template = $('#' + el.attr("inflate"));
		el.html(template.html());
		if(template.attr("class")){
			el.addClass(template.attr("class"));
		}
	});
}
	
function setupNavbars(){
	$('[data-role="navbar"]').each(function(index, element) {
		var nb = $(element);
		var n = + nb.attr("index");
		if(isNaN(n)){
			return;
		}
		
		nb.children().children().children('a').each(function(i, e) {
			var nbitem = $(e);
			if(i == n){
				nbitem.addClass("ui-btn-active");//ui-state-persist 
				nbitem.addClass("ui-state-persist");
				nbitem.attr("transition", "slideup");
				return;
			}
			
			nbitem.attr("transition", "slide");
			if(i<n){
				nbitem.attr("data-direction", "reverse");
			}
		});
	});
}

function jsonEncode(arr) {
    var parts = [];
    var is_list = (Object.prototype.toString.apply(arr) === '[object Array]');

    for(var key in arr) {
    	var value = arr[key];
		if(Object.prototype.toString.call(value) === '[object Function]'){
			continue;
		}
        if(typeof value == "object") {
            if(is_list) parts.push(jsonEncode(value));
            //else parts[key] = jsonEncode(value);
			else parts.push('"' + key + '":' + jsonEncode(value));
        } else {
            var str = "";
            if(!is_list) str = '"' + key + '":';

            if(typeof value == "number") str += value;
            else if(value === false) str += 'false';
            else if(value === true) str += 'true';
			else if(value === null) str += 'null';
            else {
				//str += value;
				//str = '"' + str.split('"').join('\\"') + '"';
				str += '"' + value.split('"').join('\\"') + '"';
			}

            parts.push(str);
        }
    }
    var json = parts.join(",");
    while(json.indexOf("\r") != -1){
		json = json.replace("\r","\\r");
	}
	while(json.indexOf("\n") != -1){
		json = json.replace("\n","\\n");
	}
	while(json.indexOf("\t") != -1){
		json = json.replace("\t","\\t");
	}
    if(is_list) return '[' + json + ']';
    return '{' + json + '}';
}
