(function() {
	
	Application = {
		
		APP_URL:'',
		BASE_URL:'',

		/*GSearch: function() {
			
			 var searchControl = new google.search.CustomSearchControl();
			 //var brand=document.getElementById("gsc-branding");
  			 var options = new google.search.DrawOptions();
			  //searchControl.addSearcher(new google.search.WebSearch());
			 
  			 options.setSearchFormRoot(document.getElementById("search_box"));
			 searchControl.draw(document.getElementById("gsresult"), options);
			 $('.gsc-branding').hide();
			 $('.gsc-clear-button').hide();
			
		},*/
		
		SetupForm: function() {
			
			//$('input:first').focus();
			var $firstInput = $('.application-form input:first');
			
			$firstInput.parents('tr').addClass('focus');
			$firstInput.focus();
		
			$('.application-form input, .application-form textarea').bind('focus', function(){

				$(this).parents('tr').addClass('focus');
			});

			$('.application-form input, .application-form textarea').bind('blur', function() {
				$(this).parents('tr').removeClass('focus');
			});		
		},
		
		
		
		SetupEditor: function() {
			
			$('#wysiwyg').wysiwyg();
		},
		
		SetupTreeview: function() {
			
			$('#browser').treeview({
				
				collapsed: true,
				persist: "cookie"
			});			
		},
		
		AddEmailAddress: function() {
			
			$('.add-email').bind('click', function() {
				
				var template = '<tr>\
									<td class = "label"></td>\
									<td>\
										<input type = "text" name = "contact_email[]" size = "35" />\
										<a href = "javascript:void(0);" class = "remove-email">remove</a>\
									</td>\
								</tr>';
				var $tr = $(this).parents('tr');
				$tr.after(template);
				$tr.next().find('input[type="text"]').focus();
				
				return false;
			});
		},
		
		RemoveEmailAddress:function() {
			
			$('.remove-email').live('click', function() {
				
				$(this).parents('tr').remove();
			});
		},
		
		ShowAttachFile: function() {
			
			$('.change-file').bind('click', function() {
				
				var self = $(this);
				
				self.nextAll('input[type="file"]').toggle();
				
				return false;
			});
		},
		
		InitGoogleMaps: function() {
			
			
			var url = new String(window.location);
			
			var parts = url.split('/');
			
			parts = parts[parts.length-1].split('#');
						
			var type = parts[1];
			
			if(type !== undefined) {
				
				switch(type) {
					
					//case 'company': Application.ShowOnMap(jQuery.trim($('#company-address').text())); break;
						case 'company': Application.ShowOnMap('Carei,Romania'); break;
					//case 'logistic': Application.ShowOnMap(jQuery.trim($('#logistic-address').text())); break;
						case 'logistic':
							{
								
								var map = new GMap2(document.getElementById("contact-map"));
    								map.setCenter(new GLatLng(46.407473, 24.702645), 13);
    								map.openInfoWindow(map.getCenter(),
                       						document.createTextNode(jQuery.trim($('#logistic-address').text())));

							} break;
				}
			}
			else {
				//Application.ShowOnMap(jQuery.trim($('#company-address').text()));
				Application.ShowOnMap('Carei,Romania');
			}
			
			//Application.ShowOnMap(jQuery.trim($('#company-address').text()));
			
			$('#show-company, #show-logistic').bind('click', function() {
				
				window.location = $(this).attr('href');
				
				window.location.reload();
				
			});
		},
		
		ShowOnMap: function(address) {
			
			google.setOnLoadCallback( function () {
				
				var map = new GMap2(document.getElementById('contact-map'));
				
				//map.setCenter(new GLatLng(37.4419, -122.1419), 13);
				
				map.setUIToDefault();
				
				var geocoder = new GClientGeocoder();
				
				//var address = jQuery.trim($('#company-address').text());
				
				geocoder.getLatLng(
					address,
					function(point) {
						
						if(!point) console.log(address + ' not found :(');
						else map.setCenter(point, 13);
						
						var marker = new GMarker(point);
						
						map.addOverlay(marker);
						marker.openInfoWindowHtml(address);
					}
				);
				
			} );
		},
		
		ProductList: function() {
			
			$('.category-products-ajax').bind('click', function() {
				
				$('#ajax-product-list').load(Application.BASE_URL+'/category/productlist/'+$(this).attr('title'));

				return false;
			})
		},
		
		Slideshow: function() {
			
			$('#slideshow').cycle();
		},
		
		Accordion: function() {
			
			$("#accordion").accordion();

		},
		
		SwapImage: function() {
		//roman	
			$('img#menu1').hover(function() {
				$(this).attr("src",Application.APP_URL+"img/RO/despre_noi_1.png");
					}, function() {
				$(this).attr("src",Application.APP_URL+"img/RO/despre_noi.png");
			});
			
			$('img#menu2').hover(function() {
				$(this).attr("src",Application.APP_URL+"img/RO/produse_1.png");
					}, function() {
				$(this).attr("src",Application.APP_URL+"img/RO/produse.png");
			});
			
			$('img#menu3').hover(function() {
				$(this).attr("src",Application.APP_URL+"img/RO/parteneri_1.png");
					}, function() {
				$(this).attr("src",Application.APP_URL+"img/RO/parteneri.png");
			});
			
			$('img#menu4').hover(function() {
				$(this).attr("src",Application.APP_URL+"img/RO/contact_1.png");
					}, function() {
				$(this).attr("src",Application.APP_URL+"img/RO/contact.png");
			});
			
		//magyar
			$('img#menu5').hover(function() {
				$(this).attr("src",Application.APP_URL+"img/HU/rolunk-1.png");
					}, function() {
				$(this).attr("src",Application.APP_URL+"img/HU/rolunk.png");
			});
			
			$('img#menu6').hover(function() {
				$(this).attr("src",Application.APP_URL+"img/HU/termekek-1.png");
					}, function() {
				$(this).attr("src",Application.APP_URL+"img/HU/termekek.png");
			});
			
			$('img#menu7').hover(function() {
				$(this).attr("src",Application.APP_URL+"img/HU/partnerek-1.png");
					}, function() {
				$(this).attr("src",Application.APP_URL+"img/HU/partnerek.png");
			});
			
			$('img#menu8').hover(function() {
				$(this).attr("src",Application.APP_URL+"img/HU/kapcsolat-1.png");
					}, function() {
				$(this).attr("src",Application.APP_URL+"img/HU/kapcsolat.png");
			});
			
		//angol
			$('img#menu9').hover(function() {
				$(this).attr("src",Application.APP_URL+"img/EN/about_1.png");
					}, function() {
				$(this).attr("src",Application.APP_URL+"img/EN/about.png");
			});
			
			$('img#menu10').hover(function() {
				$(this).attr("src",Application.APP_URL+"img/EN/products_1.png");
					}, function() {
				$(this).attr("src",Application.APP_URL+"img/EN/products.png");
			});
			
			$('img#menu11').hover(function() {
				$(this).attr("src",Application.APP_URL+"img/EN/partners_1.png");
					}, function() {
				$(this).attr("src",Application.APP_URL+"img/EN/partners.png");
			});
			
			$('img#menu12').hover(function() {
				$(this).attr("src",Application.APP_URL+"img/EN/contact_1.png");
					}, function() {
				$(this).attr("src",Application.APP_URL+"img/EN/contact.png");
			});
			
			//nemet
			$('img#menu13').hover(function() {
				$(this).attr("src",Application.APP_URL+"img/DE/uberuns_1.png");
					}, function() {
				$(this).attr("src",Application.APP_URL+"img/DE/uberuns.png");
			});
			
			$('img#menu14').hover(function() {
				$(this).attr("src",Application.APP_URL+"img/DE/produkte_1.png");
					}, function() {
				$(this).attr("src",Application.APP_URL+"img/DE/produkte.png");
			});
			
			$('img#menu15').hover(function() {
				$(this).attr("src",Application.APP_URL+"img/DE/partners_1.png");
					}, function() {
				$(this).attr("src",Application.APP_URL+"img/DE/partners.png");
			});
			
			$('img#menu16').hover(function() {
				$(this).attr("src",Application.APP_URL+"img/DE/kontakt_1.png");
					}, function() {
				$(this).attr("src",Application.APP_URL+"img/DE/kontakt.png");
			});

		}
		
	};
	
}) ();


$('document').ready(function() {
	//Application.SetupForm();
	
	//console.log(Application.APP_URL);
	
	Application.ProductList();
	
	Application.SetupEditor();
	Application.SetupTreeview();
	Application.ShowAttachFile();
	Application.AddEmailAddress();
	Application.RemoveEmailAddress();
	Application.Slideshow();
	Application.SwapImage();
	//Application.GSearch();
	
});
