var BrowseAll = {
	types: {
		/*prints: {
			url_gallry: '/prints/all/',
			form_id: 'browse_prints',
			sc_type: 'print'
		}, 
		websites: {
			url_gallry: '/websites/all/',
			form_id: 'browse_websites',
			sc_type: 'website'
		},*/
		logos: {
			url_gallry: '/logo-design/',
			url_gallry_portfolio: '/users/portfolio/logos/',
			form_id: 'browse_brands',
			sc_type: 'logo'
		},
		domains: {
			url_gallry: '/domain-names/all/',
			url_gallry_portfolio: '/users/portfolio/domains/',
			form_id: 'browse_domains',
			sc_type: 'domain'
		},
		users: {
			url_gallry: '/users/all/',
			form_id: 'browse_brands',
			sc_type: null
		}
	},
	
	current_type : null,
	
	starter: function(type) {
		this.current_type = type;
		if (this._check()) {
			this.variations.setType(type);
			this.layouts.setType(type);
		} else {
			this.current_type = null;
		}
	},
	
	reportWins: {},
	reportThis: function(item_id, feeadback_object_id, type) {
		//TODO change - to allow use from comments/all ...
		if (type) {
			switch(type) {
			case 'brand':
				type = 'logos';
				break;
			case 'domain':
				type = 'domains';
				break;
			case 'profile':
				type = 'users';
				break;
			}
		}
		
		if (!this._check(type) || item_id < 1) return;
		type = type ? type : this.current_type;
		var code = type + '_' + item_id;
		if (!this.reportWins[code]) {
			this.reportWins[code] = new ReportThis({
				item_id: item_id,
				type: type,
				feeadback_object_id: feeadback_object_id ? feeadback_object_id : null
			});
		}
		this.reportWins[code].show();
	},

/*** make offer [start] ***/
	makeOffer: function(item_id) {
		if (item_id < 1 || !BasePage.isLogged()) return;
		if (!this.offerWin) {
			this.offerWin = ITcraft.ut.getDialog({
				id: 'new_message_win',
				width: 320,
				height: 520
			},{
				//header: 'Make an offer<div class="t1">Info</div>',
				ajax_content: {
					parameters: {
						type: 'offer',
						item_id: item_id,
						subtype: this.current_type
					}
				}
			});
		}
		this.offerWin.show();
	},
/*** make offer [end] ***/
	sendMessageToDesigner: function(item_id, user_name){
		if (user_name) {
			if (!BasePage.isLogged()) return;
			var params = {
				type: 'send_message_to_designer_direct',
				designer_username: user_name
			};
		} else {
			if (!this._check() || item_id < 1 || !BasePage.isLogged()) return;
			var params = {
				type: 'send_message_to_designer',
				item_id: item_id,
				subtype: this.current_type
			};
		}

		if (!this.designerMesWin) {
			this.designerMesWin = ITcraft.ut.getDialog({
				id: 'send_mess_win',
				width: 450,
				height: 320
			},{
				ajax_content: {
					parameters: params
				}
			});
		}
		this.designerMesWin.show();
	},
	
	//spring item
	rateIt: function(item_id, el, type, need_counter){
		if (!this._check(type) || !el || item_id < 1 || !BasePage.isLogged()) return;

		ITcraft.request('/ajax/rate.php', {
			parameters:{
				cmd: 'rateIt',
				type: type ? type : this.current_type,
				item_id: item_id,
				need_counter: need_counter ? 1 : 0
			},
			onSuccess: function(json, res) {
				if(!json.is_error){
					var link = $(this.el);
					if (link) {
						link.className += ' not_active';
						link.onclick = '';
						/*if (/ico-link2/.test(link.className)) {
							link.update(json.new_counter ? '<span class="counter">'+(json.new_counter < 10 ? '' : '')+json.new_counter+'</span>' : 'Sprung');
						}*/
						if (/ico-link/.test(link.className)) {
							link.update(json.new_counter ? '<span class="counter">'+json.new_counter+'</span>' : 'Sprung');
						}
					}
				}
			},
			scope: {
				el: el
			}
		});
	},
	//add item to favorites
	addToFav: function(item_id, el, type, need_counter){
		if (!this._check(type) || !el || item_id < 1 || !BasePage.isLogged()) return;
		var ctype = type ? type : this.current_type;
		ITcraft.request('/ajax/favorites.php', {
			parameters:{
				cmd: 'add',
				type: ctype,
				item_id: item_id,
				need_counter: need_counter ? 1 : 0
			},
			onSuccess: function(json, res) {
				if(!json.is_error){
					var link = $(this.el);
					if (link) {
						link.className += ' not_active';
						link.onclick = '';
						/*if (this.ct == 'users' || /ico-link2/.test(link.className)) {
							link.update(json.new_counter ? ' <span class="counter">'+(json.new_counter < 10 ? '' : '')+json.new_counter+'</span>' : (this.ct == 'users' ? 'Following' : 'Favorited'));
						}*/
						if (this.ct == 'users' || /ico-link/.test(link.className)) {
							link.update(json.new_counter ? '<span class="counter">' + json.new_counter + '</span>' : (this.ct == 'users' ? 'Following' : 'Favorited'));
						}
					}
				}
			},
			scope: {
				el: el,
				ct: ctype
			}
		});
	},
	
	removeFromFav: function(item_id, el, type){
		if (!this._check(type) || !el || item_id < 1 || !BasePage.isLogged()) return;
		var ctype = type ? type : this.current_type;
		ITcraft.request('/ajax/favorites.php', {
			parameters:{
				cmd: 'delete',
				type: ctype,
				item_id: item_id
			},
			onSuccess: function(json, res) {
				var link = $(this.el);
				if (link) {
					link.className += ' not_active';
					link.onclick = '';
				}
			},
			scope: {
				el: el,
				ct: ctype
			}
		});
	},

	//show variation image
	variations: {
		current_type: null,
		winConfigns: {
			logos: {
				width: 650,
				height: 425
			},
			prints: {
				width: 830,
				height: 745
			}
		},
		img_element_id: 'main_logo_image',
		img_element: null,
//		hidden_thumb: null,
		hidden_el: null,
		hidden_main_path: null,
		setType: function(type) {
			if (!type || !this.winConfigns[type]) return;
			this.current_type = type;
		},
		show: function (item_id, picture_id){
			if (!this.current_type || !item_id || !picture_id) return;
			if(this.win) {
				this.close();
				this.win = null;
			}
			if (this.win) {
				this.close();
			}
			
			this.win = ITcraft.ut.getDialog(this.winConfigns[this.current_type],{
				header: 'Variantion Image',
				ajax_content: {
					parameters: {
						type: 'variation_image',
						subtype: this.current_type,
						picture_id: picture_id,
						item_id: item_id
					}
				}
			});
			this.win.show();
		},
		close: function() {
			if (this.win) {
				this.win.destroy();
			}
		},
		changeSRC: function(path, c_el) {
			if (!path || !c_el) return;
			if (!this.img_element) {
				this.img_element = $(this.img_element_id);
				if (!this.img_element) return;
			}
			this.img_element.src = path;
			if (!this.current_active) {
				var col = c_el.parentNode.getElementsByClassName('selected_ico');
				if (col.length) {
					this.current_active = col[0];
				}
			}

			if (this.current_active) {
				this.current_active.className = 'brand-ico';
			}
			c_el.className = 'brand-ico selected_ico';
			this.current_active = c_el;
		}
	},
	
	//add to shopping cart
	addToSC: function(item_id, already_in_sc, type){
		if (already_in_sc) {
			ITcraft.showAlert('This item is already in your shopping cart!', 'Shopping Cart');
			return;
		}
		if (!this._check(type) || item_id < 1 || !BasePage.isLogged()) return;
		var ctype = type ? type : this.current_type;
		//document.location = '/shoppingcart/add/' + this.types[ctype].sc_type + '/' +item_id; 

		var form = new Element('form', {
			action: '/shoppingcart/add/' + this.types[ctype].sc_type + '/' +item_id,
			method: 'post'
		});
		var from_inp = new Element('input', {
			name: 'from',
			value: document.location.pathname + document.location.search
		});
		form.appendChild(from_inp);
		document.body.appendChild(form);
		form.submit();
	},
	
	//layouts dialog
	layouts: {
		current_type: null,
		setType: function(type) {
			this.current_type = type;
		},
		show: function (item_id, layout_id){
			if (!this.current_type || !item_id || !layout_id) return;
			if(this.win) {
				this.close();
				this.win = null;
			}
			this.win = ITcraft.ut.getDialog({
				width: 830,
				height: 725
				//close: 'destroy',
				//auto: false
			},{
				header: 'Website Layouts',
				ajax_content: {
					parameters: {
						type: 'layouts',
						subtype: this.current_type,
						layout_id: layout_id,
						item_id: item_id
					}
				}
			});
			this.win.show();
		},
		close: function(){
			if (this.win) {
				this.win.destroy();
			}
		},
		nextImage: function(){
			this.checkEls();
			if (this.image_counter > this.current_image) {
				this.current_image++;
				this.image_el.src = this.images[this.current_image].path;
				this.size_field.textContent = this.images[this.current_image].size;
			}
			if (this.image_counter == this.current_image) {
				this.pager_next_el.className='inactive_next';
			}
			if (this.current_image > 0){
				this.pager_prev_el.className='prev';
			}
		},
		prevImage: function(){
			this.checkEls();
			if (this.current_image > 0) {
				this.current_image--;
				this.image_el.src = this.images[this.current_image].path;
				this.size_field.textContent = this.images[this.current_image].size;
			}
			if (0 == this.current_image) {
				this.pager_prev_el.className='inactive_prev';
			}
			if (this.image_counter > this.current_image){
				this.pager_next_el.className='next';
			}
		},
		checkEls: function(){
			if (!this.pager_next_el) {
				this.pager_next_el = $('website_lay_next_btn');
				this.pager_prev_el = $('website_lay_prev_btn');
				this.size_field = $('size_field_w');
				this.image_el = $('website_lay_img');
				this.image_counter--;
			}
		},
		
		setLayouts: function(list){
			if (list && list.length) {
				this.images = list;
				this.image_counter = list.length;
			}
		},
		setCurrentImage: function(num){
			this.current_image = num;
			this.images = [];
			this.image_counter = 0;
			this.pager_next_el = null;
		}
	},

	_check: function(type) {
		var c_type = type ? type : this.current_type;
		if (!c_type || !this.types[c_type]) {
			return false;
		}
		return true
	},
	
	showVerifiedDomainMessage: function() {
		ITcraft.showAlert('This domain has been purchased from or transferred to Brandstack and will be automatically transferred to you upon being purchased.','Verified Domain',false,false,480,170);
	},
	
	//used on logo details
	onFilesDetails: function() {
		ITcraft.showAlert('These are the files you will receive when you purchase this brand. Use them to create print, web or identity designs.',
			'Help',
			false,
			false,
			480,
			220
		);
	},
	
	initCarousel: function(containner_id) {
		if (!containner_id) {
			containner_id = 'horizontal_carousel';
		}
		var cont = document.getElementById(containner_id);
		if (!cont) return;
		var imgs = cont.getElementsByTagName('IMG');
		var total_imgs = imgs.length;
		var CCreator = {
			totalImages: total_imgs,
			loaded: 0,
			cont: cont,
			create: function() {
				this.crousel = new UI.Carousel(this.cont);
			},
			onLoad: function() {
				this.loaded++;
				if (this.loaded >= this.totalImages) {
					this.create();
				}
			}
		};
		
		if (total_imgs < 1) {
			CCreator.create();
		} else {
			var onLoadListener = CCreator.onLoad.bind(CCreator);
			for(var i=0; i<total_imgs; i++) {
				if (imgs[i].complete) {
					onLoadListener();
				} else {
					imgs[i].onload = onLoadListener;
				}
				
				
			}
		}
	},
/***** search fucntionality [start] *****/
	startTagsSearch: function(btn) {
		var form = btn && btn.form ? btn.form : null;
		if (!form) return;
		var s_string = form['search_string'] ? form['search_string'].value : null;
		if (s_string) {
			var w = s_string.split(',');
			if (w.length > 1) {
				form.submit();
			} else {
				document.location = form.action + s_string;
			}
		}
		/*if (s_string) {
			document.location = document.location.pathname + s_string;
		}*/
	},
	
	//used on logo-names page - to switch between search modes
	onSwitchSearchMode: function(el)
	{
		var cont = $('advanced_search_container');
		if (!el || !cont) {
			return false;
		}
		var new_mode = 'advanced';
		if (new_mode == el.getAttribute('currentMode')) {
			new_mode = 'simple';
		}
		cont.style.display = new_mode == 'advanced' ? '' : 'none';
		el.innerHTML = new_mode == 'advanced' ? 'Switch to Simple Search' : 'Switch to Advanced Search';
		
		el.setAttribute('currentMode', new_mode);
		ITcraft.setCookie('showAdvSearch', new_mode == 'advanced' ? '1' : '0')
		/*var cmd = $('search_cmd_input');
		if (cmd) {
			cmd.value = new_mode == 'advanced' ? 'asearch' : 'ssearch';
		}*/
	},
	onSearchFormSubmit: function(f)
	{
		if (!f || !f.elements['keyword']) return;
		//TODO cahnge this!!
		var k = f.elements['keyword'];
		if (k.value == 'enter keyword and begin search today') {
			k.value = '';
		};
		var el;
		for(var i=0; i<f.elements.length; i++) {
			el = f.elements[i];
			if (el.type=='text' && (el.value == '' || el.value == '0')) {
				el.disabled = true;
			}
		}
		return true;
	},
	searchSelectColor: function(inp)
	{
		if (!this.color_box) {
			this.csW = $('colorSelector_wrapper');
			if (!this.csW) return;
			
			this.csW.setStyle({
				position: 'absolute',
				zIndex: 100,
				top: '-1px',
				right: '-1px',
				background: '#FFF',
				border: '1px solid #cecece',
				padding: '2px',
				textAlign: 'right'
			});
			var onShow = function() {this.csW.style.display = '';}.bind(this);
			var onHide = function() {this.csW.style.display = 'none';}.bind(this);

			this.csW.onmouseout = onHide;
			this.csW.onmouseover = onShow;
			inp.onmouseout = onHide;

			this.color_box = new ITcraft.colorBoxWidget({
				container: 'sfColorBox_container',
				reset_link: 'cs_reset_link',
				object_name: 'BrowseAll.color_box',
				selected_color_input_id: 'sf_color_input'
			});
		}
		this.csW.style.display = '';
	},
	/*** advanced search dialog [start] ***/
	showAdvancedSearch: function(for_username){
		if (!this._check()) return;
		if(!this.advanceSearchWin){
			this.advanceSearchWin = ITcraft.ut.getDialog({
				id: 'advanced_win',
				width: 550,
				height: 350
			},{
				ajax_content: {
					parameters: {
						type: 'advanced_search',
						subtype: this.current_type,
						for_username: for_username
					}
				},
				onContentUpdatedListener: function() {
					if (this.form) {
						//remove ajax hendler from HTML form
						//and allow submit it in normal way not via AJAX
						//also add handler that change the form action URL depend on the sort field value
						this.form.form.onsubmit = function(){
							var mode_field = $('advnced_search_mode');
							if (mode_field && mode_field.value) {
								this.form.form.action += mode_field.value +'/';
							}
							return true;
						}.bind(this);
					}
					if ($('color_box_container')) {
						this.color_box = new ITcraft.colorBoxWidget({
							container: 'color_box_container',
							object_name: 'BrowseAll.advanceSearchWin.color_box',
							selected_color_input_id: 'selected_color_input_id'
						});
					}
				}
			});
		}
		this.advanceSearchWin.show();
	},
	/*** advanced search dialog [end] ***/
	/*** simple search [start] ***/
	search: function(){
		$(this.types[this.current_type].form_id).submit();
	},
	resetSearch: function(for_username) {
		if (!this._check()) return;
		if (for_username) {
			if (this.types[this.current_type]['url_gallry_portfolio']) {
				document.location.href = this.types[this.current_type]['url_gallry_portfolio'] +for_username;
				return;
			}
			return;
		}
		document.location.href = this.types[this.current_type]['url_gallry'];
	}
	/*** simple search [end] ***/
/***** search fucntionality [start] *****/
};

//comments all & my comments pages
var BrowseComments = {
		deleteComment: function(type, object_id, id){
		if (id < 1 || !type || !object_id) return;
		
		ITcraft.showConfirm('Are you sure want to delete this comment?', null,
			function(){
				ITcraft.request('/ajax/feedbacks.php',{
					parameters: {
						cmd: 'delete_feedback',
						object_id: object_id,
						type: type,
						feedback_id: id
					},
					onSuccess: function(json) {
						//reload page
						history.go();
					},
					scope: this
				});
			},
			this
		);
	}
};