if (typeof(bg) == 'undefined') bg = {};
if (!bg.guns) bg.guns = {};

bg.guns.init = function() {
	
	bg.guns.site = new bg.guns._site();
	
//	setTimeout('bg.guns.site.showCompaniesList()',500);
}

bg.guns._site = function() {
		
	this.active = {};
	this.active.tab = 'promoProducts';
	this.prev = {};
	this.settings = {};
	
	this.settings.companiesJsonUrl = 'http://'+document.location.host+'/products/?action=getManufacturers&plugins=disable';
	this.settings.companiesElCode = '<a href="http://guns.bg/products/?manufacturerId=|id|&ref=home"><span><img src="|image|" /></span><strong>|title|</strong></a>';

//	this.settings.companiesJsonUrl = 'companies.json';
//	this.settings.companiesElCode = '<a href="http://guns.bg/products/?manufacturerId=|id|&ref=home"><span>|image|</span><strong>|title|</strong></a>';

	this.bindEvents();
	
}

bg.guns._site.prototype.bindEvents = function() {

	$('#companiesIndex').find('img').each(function(index, element) {
	
		$(element).bind('load', function() {
			bg.edesign.fitImage(this.parentNode);
		})
			
	});
	
}

bg.guns._site.prototype.setActiveTab = function(tabId) {

		this.active.tab = tabId;

}


bg.guns._site.prototype.loadCompaniesList = function() {
	
	$.ajax({
   url: bg.guns.site.settings.companiesJsonUrl,
   dataType: 'json',
	 success: function(data) {
		 
     bg.guns.site.companies = data;
		 $('#companiesPopupStatus').slideUp();
		 
		 bg.guns.site.showCompaniesList();
		 
   },
	 error: function() {
		alert('Error \n'+this.url) 
	 }
 });	
	
	
}

bg.guns._site.prototype.showCompaniesList = function() {
	
//	bg.guns.site.companies = new Array();
	
	
	$('#fogOfWar').css('height', document.body.offsetHeight+'px');
	$('#fogOfWar').fadeIn();
	$('#companiesPopup').fadeIn();
	
	if (!this.prev.companiesListHldrH) {
		this.prev.companiesListHldrH = $('#companiesPopup ul').css('height');
	}
	$('#companiesPopup ul').css('height','0');
	
	if (!this.companies) {
		this.loadCompaniesList();
		return;
	}
	
	$('#companiesPopup ul').animate({height:this.prev.companiesListHldrH}, 200)
	
	document.getElementById('companiesFilterField').value = '';
	document.getElementById('companiesFilterField').focus();

	if (!this.companiesList) {
		document.getElementById('companiesFilterField').onkeyup = function() { bg.guns.site.setCompaniesFilter(this.value); };
		this.companiesList = new bg.edesign.list({ listObj: this.companies, holder: '#companiesListHolder', listTag: 'li', elCode: this.settings.companiesElCode, elCallback: bg.edesign.initFitImage});
	}
	
	this.positionCompanies();
	
}


bg.guns._site.prototype.hideCompaniesList = function() {

	$('#fogOfWar').fadeOut();
	$('#companiesPopup').fadeOut();
	
	delete this.active.filterString;
	delete this.prev.filterString;
	delete this.companiesFiltered;

}



bg.guns._site.prototype.positionCompanies = function() {
	
//	bg.edesign.log('bg.guns._site.prototype.positionCompanies');
		
	var currX = 0; var currY = 0; var currRow = 0; var currCol = 0;	var maxRow = 3;
	var elW = 154; var elH = 125;
	
	$('#companiesListHolder li').css('display','none');
//	$('#companiesListHolder').css('display','none');
		
	if (this.companiesFiltered) {
		var arrToUse = this.companiesFiltered;
	} else {
		var arrToUse = this.companies;
	}
	
	for (var i in arrToUse) {
				
		arrToUse[i].DOMelement.style.left = currX+'px';
		arrToUse[i].DOMelement.style.top = currY+'px';
		arrToUse[i].DOMelement.style.display = 'block';
	
		currY += elH;
		
		if ( currY >= ( maxRow * elH ) ) {
			currX += elW;
			currY = 0;
		} 
		
	}
	
//	$('#companiesListHolder').fadeIn();

	
	$('.cmpList').css('width', (currX + elW)+'px' )
	
}



bg.guns._site.prototype.setCompaniesFilter = function(filterString) {
	
	clearTimeout(bg.guns.site.active.filterTimeout);
	bg.guns.site.active.filterTimeout = setTimeout(function() {bg.guns.site.doCompaniesFilter(filterString)},250);
	
}

bg.guns._site.prototype.doCompaniesFilter = function(filterString) {
			
	if (this.active.filterString) {
		this.prev.filterString = this.active.filterString;
	}
	
	this.active.filterString = filterString;
	
	if ( this.prev.filterString && (this.active.filterString == this.prev.filterString)) {
		return;
	}
	
	if ( this.prev.filterString && (this.active.filterString.indexOf( this.prev.filterString ) == 0) ) {
		this.companiesFiltered = bg.edesign.filterArr('title',filterString,this.companiesFiltered);
	} else {
		this.companiesFiltered = bg.edesign.filterArr('title',filterString,this.companies);
	}
	
	this.positionCompanies();
	
}



bg.guns._site.prototype.switchTab = function(tabId) {
	
//	console.log('bg.guns._site.prototype.switchTab >>', tabId, this.active, this.prev);
	
	if (this.active.tab) {
		this.prev.tab = this.active.tab;
	}	

	if (this.prev.tab) {
		$('#'+this.prev.tab).hide();
	//	$('#'+this.prev.tab).slideUp();
	}
	
	
	var selectedTab = this.active.tab+'Tab';
		
	$( '#' + selectedTab ).addClass( 'tab' ).addClass( 'pngfix' ).removeClass( 'here2' );
	selectedTab = tabId + 'Tab';
	$( '#' + selectedTab ).addClass( 'tab' ).addClass( 'pngfix' ).addClass( 'here2' );
	

	
	this.active.tab = tabId;
//	$('#'+this.active.tab).show();
	$('#'+this.active.tab).fadeIn( 'fast', function() {	bg.edesign.scrollpanes['prodsTabs'].init( $('#'+bg.guns.site.active.tab).get(0) );} );
	
}

