jQuery(function() {

	if (jQuery.browser.safari) {
		jQuery(document.documentElement).attr('class','js webkit');
	}
	else if (jQuery.browser.mozilla) {
		jQuery(document.documentElement).attr('class','js moz');
	}
	else if (jQuery.browser.msie) {
		jQuery(document.documentElement).attr('class','js ie');
	}

	$(".disabled-option").attr('disabled', 'disabled');
	$('.disabled-option').parent().find('.oi-select-text:first').css("background", "#e0e0e0");
	$('.disabled-option').parent().find('.oi-select-text:first').css("color", "#666");

	jQuery('#formSearch fieldset').click(function(e) {
		if (e.target.tagName.toLowerCase()=='fieldset') {
			jQuery('#formSearch').submit();
		}
	});

	jQuery('.search-row select, .search-row input')
		.focus(function() {
			jQuery(this).parents('fieldset').addClass('focus');
		})
		.blur(function() {
			jQuery(this).parents('fieldset').removeClass('focus');
		});
	
	
	jQuery('.search-row input')
		.focus(function() {
			this.value = (this.value=='Search...' ? '' : this.value);
		})
		.blur(function() {
			this.value = (this.value=='' ? 'Search...' : this.value);
		});
		
	// Open external links in new window.
	jQuery("a[href^='http://'][target!=_blank]").live('click',function() {
		var $this = jQuery(this);
		if ($this.attr('href').indexOf('mannheim.dabbledb.com') < 0) {
			window.open($this.attr('href'));
		}
		return false;
	});
	

	jQuery('.row-project-images .thumb-image')
	.click(function() {
		var $this = jQuery(this);
		var img = new Image();
		jQuery(img).load(function() {
			var $container = $this.parent().next('.row-project-image');
			$container.empty();
			$container.append(img).append('<span class=\"legend\">' + $this.attr('title') + '<\/span>');
		});
		img.src = $this.attr('href');
		return false;
	});

	jQuery('.row-project-images .thumb-video')
	.click(function() {
	    
	    /*
	    // Flowplayer (old)
	    
		var $this = jQuery(this);
		var $vid = jQuery("<a/>").attr("href",$this.attr('href')).css({display:'block'}).click(function() { return false; });
		
		var $container = $this.parent().next('.row-project-image');
		$container.empty();
		$container.append($vid).append('<legend>' + $this.attr('title') + '<\/legend>');

		$vid.picletFlowplayer();

		return false;
		*/
		
		
		
		var $this = jQuery(this);
		
		var movie = $this.attr("data-mov");
		
        var $movie = jQuery(movie);
                
		var $container = $this.parent().next('.row-project-image');
		$container.empty();
		$container.append($movie).append('<span class=\"legend\">' + $this.attr('title') + '<\/span>');
		
		
		var _fit = function($object) {

            if ($object.length) {
        		var w = $object.attr('width');
                var h = $object.attr('height');
                if (w && h) {
                    h = Math.ceil(h * 460 / w);
                    w = 460;
                    $object.attr('width',w).attr('height',h);
                }
            }
		}
		
        if ($.browser.msie) {
            setTimeout(function() { // IE needs its time to get this right...
                var $object = $container.find('embed');
                _fit($object);
            },10);
        }
        else {
            var $object = $container.find('object');
            _fit($object);
            $object = $container.find('embed');
            _fit($object);
        }
        
		return false;
	});


    /**
     * Artist video
     */
    
    jQuery('.row-artist-video [data-mov]').click(function() {
        
		var $this = jQuery(this);
		
		var movie = $this.attr("data-mov");
		
        var $movie = jQuery(movie);
                
		var $container = $this.parent();
		$container.empty();
		$container.append($movie);
		
        
		var _fit = function($object) {

            if ($object.length) {
        		var w = $object.attr('width');
                var h = $object.attr('height');
                if (w && h) {
                    h = Math.ceil(h * 460 / w);
                    w = 460;
                    $object.attr('width',w).attr('height',h);
                }
            }
		}
		
        if ($.browser.msie) {
            setTimeout(function() { // IE needs its time to get this right...
                var $object = $container.find('embed');
                _fit($object);
            },10);
        }
        else {
            var $object = $container.find('object');
            _fit($object);
            $object = $container.find('embed');
            _fit($object);
        }

    });

	
	// Set up any newsletter forms
	jQuery('.newsletter').newsletter();

	// Show thumbalizr iframe if it's not empty.
	setTimeout(function() {
		if ($('.row-thumboo').size() > 0) {
			if ($('.row-thumboo iframe').contents().find('img').size() != 0) {
				$('.row-thumboo').slideDown('slow');
			}
		}
	}, 500);

});



 
(function($) {
    $(function() {
        
        /**
         * Search
         */
         
        $('#form-search').each(function() {
            
            var $form = $(this);
                        
            $form.find('select').change(function() {
	    
	    	//exclude the fields under the current field from search. they will be filled with new data according the selected fields above them
		if ($(this).attr("name") == "q_dir" && $(this).val() != "") {
			$('select[name*=q_country]').attr('disabled', 'disabled');
			$('select[name*=q_city]').attr('disabled', 'disabled');
			$('select[name*=q_alpha]').attr('disabled', 'disabled');
			$('select[name*=q_type]').attr('disabled', 'disabled');
		}
	    	else if ($(this).attr("name") == "q_country" && $(this).val() != "") {
			$('select[name*=q_city]').attr('disabled', 'disabled');
			$('select[name*=q_alpha]').attr('disabled', 'disabled');
			$('select[name*=q_type]').attr('disabled', 'disabled');
		}
	    	else if ($(this).attr("name") == "q_city" && $(this).val() != "") {
			$('select[name*=q_alpha]').attr('disabled', 'disabled');
			$('select[name*=q_type]').attr('disabled', 'disabled');
		}
	    	else if ($(this).attr("name") == "q_alpha" && $(this).val() != "") {
			$('select[name*=q_type]').attr('disabled', 'disabled');
		}
               
		$('input[name*=q]').attr('disabled', 'disabled');
 
		$form.submit(); 
            });

	    $form.find('input[name*=q]').change(function() {
			$('select[name*=q_dir]').attr('disabled', 'disabled');
			$('select[name*=q_country]').attr('disabled', 'disabled');
			$('select[name*=q_city]').attr('disabled', 'disabled');
			$('select[name*=q_alpha]').attr('disabled', 'disabled');
			$('select[name*=q_type]').attr('disabled', 'disabled');
	    });
            
            
            $form.submit(function() {
                
                var action = $form.attr('action');
                
                var arr = $form.serializeArray();
                
                var params = [];
                $.each(arr, function(i, param) {
                    
                    if ($.trim(param.value) != '' && $.trim(param.value) != 'Search...') {
                        params.push(param.name + '=' + encodeURIComponent(param.value));
                    }
                    
                });
                
                if (params.length > 0) {
                    window.location.href = action + '?' + params.join('&');
                }
                // else => no search parameters => nothing to search...
                
                return false;
            });
            
        });
         

        /**
         * Google Maps
         */

        $('.gmap').each(function() {
            
            var $obj    = $(this);
            
            var loaded  = false;
            var visible = false;
            
            var map = null;
            var latlng  = null;
            
            $obj.click(function() {
                                
                var $map        = $('#gmap');
                var $wrapper    = $map.parent();
                
                
                if (visible) {
                    $wrapper.hide();
                    visible = false;
                }
                else {
                    $wrapper.show();
                    visible = true;

                    if (!loaded) {
                        
                        loaded = true;
                    
                        var adr     = $obj.attr('data-address');

                        var coder   = new google.maps.Geocoder();
                    
                        coder.geocode({address: adr}, function(results, status) {
                        
                            if (status == google.maps.GeocoderStatus.OK) {
                            

                                latlng = results[0].geometry.location;
                            
                                var mapOptions = {
                                    zoom: 15,
                                    center: latlng,
                                    mapTypeId: google.maps.MapTypeId.ROADMAP
                                }
                                map = new google.maps.Map($map.get(0), mapOptions);
                            
                                var marker = new google.maps.Marker({
                                    map: map,
                                    position: latlng
                                });
                            
                            }
                        });
                    }
                    else {
                        map.setCenter(latlng);
                    }
                }
                
                
                return false;
            });
            
        });
        
    })
})(jQuery);

	
