﻿Ext.ns('MS')


MS.getRandomInt = function(min, max){
	return Math.floor(Math.random() * (max - min + 1)) + min;
}


Array.prototype.shuffle = function() {
	for(var j, x, i = this.length; i; j = parseInt(Math.random() * i), x = this[--i], this[i] = this[j], this[j] = x);
	return this;
}


/*
 * Tab onclick handler
 */
MS.tabOnclick = function(e, t, o) {
	var el = Ext.get(t);
	if(el.hasClass('active')) return;
	
	Ext.each(el.parent('.tabs').child('tr').query('td'), function(item) {
		if(!Ext.isEmpty(item.id)) {
			Ext.get(item).setVisibilityMode(Ext.Element.DISPLAY).removeClass('active');
		}
	});
	el.addClass('active');

	Ext.each(el.parent('div').query('.content'), function(item) {
		var el = Ext.get(item);
		el.setVisibilityMode(Ext.Element.DISPLAY);
		if(el.isVisible())
			el.hide();
	});
	Ext.get(el.id + '-content').show();
	
}

/*
 * Apply tabs
 */
MS.applyTabs = function() {
	var tabs = Ext.query('.tabs');
	if(!Ext.isEmpty(tabs)) {
		Ext.each(tabs, function(item) {
			Ext.each(Ext.get(item).query('.hascontent'), function(item2) {
				Ext.get(item2).on('click', MS.tabOnclick);
			});
		});
	}
}


MS.textFieldOnFocus = function(e, t, o) {
	if(t.value == t.getAttribute('text'))
		t.value = '';
}


MS.textFieldOnBlur = function(e, t, o) {
	if(t.value == '')
		t.value = t.getAttribute('text');
}

/*
 * Apply text-click fields
 */
MS.applyTextClickFields = function() {
	try {
		var items = Ext.query('input[type=text]');
		//console.log(items);
		Ext.each(items, function(item) {
			if(item.hasAttribute('text')) {
				//console.log(item);
				var el = Ext.get(item);
				el.on('focus', MS.textFieldOnFocus);
				el.on('blur', MS.textFieldOnBlur);
				item.value = item.getAttribute('text');
			}
		});
	}
	catch(e){
		//alert(e);
		// нету поиска на странице
	}
}

/*
 * Preload (hover) imgs
 * imgs - array
 */
MS.preloadImgs = function(imgs) {
	try{
		var pre;
		for(var i = 0; i < imgs.length; i++){
			pre = new Image();
			pre.src = imgs[i];
		}
	}
	catch(e) {
	}
}


MS.applyUploadFileInput = function() {
	try {
		var hide = Ext.query('.upload-file-other');
		var show = Ext.query('.upload-file-webkit');
		if(hide && hide.length == show.length) {
			Ext.each(hide, function(item, index) {
				Ext.get(item).hide();
				Ext.get(show[index]).show();
			});
		}
	}
	catch(e) {
		//alert(e);
	}
}


MS.shotsRandomShadow = function() {
	var shotsItems = Ext.query('.shots .shot');
	if(!Ext.isEmpty(shotsItems) && shotsItems.length) {
		var c = 0;
		do {
			Ext.get(shotsItems[MS.getRandomInt(0, shotsItems.length - 1)]).addClass('w-shadow');
		} while(++c < 3);
	}
}

MS.shadowOver = function(t, e) {
	Ext.get(this).addClass('w-shadow');
}

MS.shadowOut = function(t, e) {
	Ext.get(this).removeClass('w-shadow');
}	

MS.shotsMouseOverShadow = function() {
	var shotsItems = Ext.query('.shots .shot');
	if(!Ext.isEmpty(shotsItems) && shotsItems.length) {
		Ext.each(shotsItems, function(item) {
			Ext.get(item).hover(MS.shadowOver, MS.shadowOut);
		});
	}
}


MS.applyShotsImages = function() {
	var shotsItems = Ext.query('.shots .shot a');
	try {
		if(!Ext.isEmpty(shots) && shots.length && !Ext.isEmpty(shotsItems) && shotsItems.length) {
			shots.shuffle();
			Ext.each(shots, function(item, index) {
				shotsItems[index].setAttribute('href', item.href);
				Ext.get(shotsItems[index]).child('img', true).setAttribute('src', item.src);
			});
		}
	}
	catch(e){
	}
}


MS.applyMoreInfo = function() {
	var items = Ext.query('.more-info');
	if(!Ext.isEmpty(items) && items.length) {
		Ext.each(items, function(item) {
			var el = Ext.get(item);
			try {
				el.child('.show-btn').setVisibilityMode(Ext.Element.DISPLAY).on('click', MS.moreInfoShow);
				el.child('.hide-btn').setVisibilityMode(Ext.Element.DISPLAY).on('click', MS.moreInfoHide);
			}
			catch(e) {
			}
		});
	}
}

MS.moreInfoShow = function(e, t, o) {
	e.stopEvent();
	t = Ext.get(t);
	try {
		t.next('.hidden-content').slideIn('t', {
			callback: function() {
				t.hide();
				t.parent().child('.hide-btn').show();
			}
			,scope: this
			,useDisplay: true
		});
	}
	catch(e) {
	}
}

MS.moreInfoHide = function(e, t, o) {
	e.stopEvent();
	t = Ext.get(t);
	try {
		t.next('.hidden-content').slideOut('t', {
			callback: function() {
				t.hide();
				t.parent().child('.show-btn').show();
			}
			,scope: this
			,useDisplay: true
		});
	}
	catch(e) {
	}
}

MS.applyLegend = function () {
    var show = Ext.get('show-legend'), hide = Ext.get('hide-legend'), le = Ext.get('legend');

    if (!Ext.isEmpty(show) && !Ext.isEmpty(hide) && !Ext.isEmpty(le)) {
        show.on('click', function (e) {
            e.stopEvent();
            show.hide();
            hide.show();
            le.stopFx().slideIn('t', {
                useDisplay: true
				, easing: 'elasticOut'
            });
        }, this);
        hide.on('click', function (e) {
            e.stopEvent();
            show.show();
            hide.hide();
            le.stopFx().slideOut('t', {
                useDisplay: true
                , duration: .3
            });
        }, this);
    }
}

MS.applyThemeUndReporterSelector = function () {
    var theme = Ext.get('themes-selector'), reporter = Ext.get('reporters-selector');

    if (!Ext.isEmpty(theme)) {
        theme.on('click', MS.winShowHandler, this);
        reporter.on('click', MS.winShowHandler, this);

        Ext.get('themes-win-hide').on('click', MS.winHideBtnHandler);
        Ext.get('reporters-win-hide').on('click', MS.winHideBtnHandler);
    }
}

MS.winShowHandler = function (e, t) {
    e.stopEvent();
    //[by vas]console.log(t);
    var el = Ext.get(t);
    if (el.id == 'themes-selector')
        var win = Ext.get('themes-win');
    else if (el.id == 'reporters-selector')
        var win = Ext.get('reporters-win');
    else {
        var win = Ext.get('more-win');
        try {
            var body = el.parent('.column').child('.more-win-body');
            //win.child('.body').dom.innerHTML = '';
            if (!Ext.isEmpty(body)) {
                win.child('.body').dom.innerHTML = body.dom.innerHTML;
            }
        }
        catch (e) {
            //console.log(e);
        }
         try {
        win.show({
            duration: .1
			, callback: function () {
			    MS.shadow.show(win);
			}
			, scope: this
        }).setX(el.getX() - 4).setY(el.getY() - 10);
        }
        catch (e) { }

        return;
    }

    win.show({
        duration: .1
		, callback: function () {
		    MS.shadow.show(win);
		}
    }).setX(el.getRight() - win.getWidth() + 20).setTop(el.getTop() - 11);
}

MS.winHideBtnHandler = function (e, t) {
    e.stopEvent();
    try {
        var el = Ext.get(t);
        MS.shadow.hide();
        el.parent('.white-r').hide({
            duration: .1
        });
    }
    catch (e) {
        //console.log(e);
    }
}

MS.applyMore = function () {
    var more = Ext.query('a.more');
    if (Ext.isEmpty(more)) return;

    try {
        MS.shadow = new Ext.Shadow({
            offset: 3
			, mode: 'sides'
        });
        Ext.each(more, function (item) {
            Ext.get(item).on('click', MS.winShowHandler, this);
            Ext.get('more-win-hide').on('click', MS.winHideBtnHandler);
        });
    }
    catch (e) {
        //[by vas]console.log(e);
    }
}



Ext.onReady(function() {
	MS.applyTabs();
	MS.applyTextClickFields();
	if(Ext.isWebKit)
		MS.applyUploadFileInput();

	// Раскомментировать чтобы апплилась рандомная тень
	//MS.shotsRandomShadow();
	
	MS.shotsMouseOverShadow();
	
	MS.applyShotsImages();
	
	MS.applyMoreInfo();

	MS.applyLegend();

	MS.applyThemeUndReporterSelector();

	MS.applyMore();
});


