App = 
{
    isIE: false,
	isIE6: false,
	isIE7: false,
	base_url: '',
	public_path: '',
	searchInputHint: undefined,
	searchInputHint2: undefined,
		
	init: function ()
	{
	    if ($.browser.msie) {
	        this.isIE = true;
	    }
	    
		if ($.browser.msie && (parseInt($.browser.version) == 6)) {
			this.isIE6 = true;
		}
		
		if ($.browser.msie && (parseInt($.browser.version) == 7)) {
			this.isIE7 = true;
		}
		
    	$(document).ready(function () {
    	    $('.hideme').hide();
    	    
    		try {
                if (App.isIE6) {
                    initIE6();
                }
        		initMainNav();
        		cssjsmenu('main-menu');
        		TMO.resizeContent();
        		document.execCommand('BackgroundImageCache', false, true);
    		} catch (e) {
    		}
    		
            if (App.isIE6) {
                App.hover_IE6();
            }
            
            App.initTMobileSearch();
            App.initSearchForm();
            App.initSideMenu();
            App.initModelSelector();
            App.initThumbnails();
            App.initPlayButtons();
            
            App.Slideshow.init($('div.slideshow'));
            App.Player.init();
    	});
	},
	
	initTMobileSearch: function () 
	{
        var form = $('form:[id="suche"]');
        if ($(form).length == 0) {
            return;
        }
        
        if (App.searchInputHint2 == undefined) {
            App.searchInputHint2 = $(form).find('input:[name="qt"]').val();
        }
        
        $(form).find('input:[name="qt"]').focus(function () {
            if ($(this).val() == App.searchInputHint2) {
                $(this).val('');
            }
        }).blur(function () {
            if ($(this).val().length == 0) {
                $(this).val(App.searchInputHint2);
            }
        });
	},
	
	initSearchForm: function ()
	{
	    var form = $('form:[id="search"]');
	    if ($(form).length == 0) {
	        return;
	    }
	    
	    if (App.searchInputHint == undefined) {
	        App.searchInputHint = $(form).find('input:[name="search"]').val();
	    }
	    
	    $(form).find('input:[name="search"]').focus(function () {
	        if ($(this).val() == App.searchInputHint) {
    	        $(this).val('');
    	    }
	    }).blur(function () {
	        if ($(this).val().length == 0) {
    	        $(this).val(App.searchInputHint);
    	    }
	    });
	},
	
	initModelSelector: function ()
	{
	    var box = $('div:[id="model_infos"]');
	    var button = $('div:[id="model_selector"]').find('a:[id="button"]');
	    
        $(button).click(function () {
            return App.slideModelInfoBox();
        });
        
        if ($(box).is(':visible')) {
            $(button).addClass('up');
        } else {
            $(button).removeClass('up');
        }
        
        
        $('form:[id="handyselection"]').find('select').change(function () {
            $('form:[id="handyselection"]').submit();
        });
	},
	
	initThumbnails: function ()
	{
	    $('img:[class="thumbnail"]').each(function () {
	        $(this).mouseover(function () {
	            var parent = $(this).parent();
	            $(parent).find('img:[class*="thumbnail-big"]').show();	            
	        });
	        
            $(this).mouseout(function () {
                var parent = $(this).parent();
                $(parent).find('img:[class*="thumbnail-big"]').hide();              
            });
	    });	    
	},
	
	initPlayButtons: function ()
	{
	    $('a:[class*="play-btn"]').show();
	},
	
	initSideMenu: function ()
	{
	    var sidemenu = $('div:[id="side-menu"]').find('ul:[class="navigation"]');
	    var first = $(sidemenu).find('li:first');
	    var last = $(sidemenu).find('li:last');
	    
	    $(first).find('a').html($(first).find('a').html() + '<span></span>');
	    $(first).addClass('first');
	    
	    if ($(last).find('a').hasClass('lvl1')) {
	        $(last).find('a').html($(last).find('a').html() + '<span></span>');
	        $(last).addClass('last');
	    }
	},
	
	submitForm: function (form)
	{
	    $(form).submit();
	},
	
	switchImage: function (imageId, src)
	{
	    var image = $('img:[id="' + imageId + '"]');
	    $(image).attr('src', src);
	},
	
	slideModelInfoBox: function ()
	{
	    var box = $('div:[id="model_infos"]');
	    var button = $('div:[id="model_selector"]').find('a:[id="button"]');
	    var visible = 1;
	    
	    if ($(box).is(':visible')) {
	        $(box).slideUp();
	        $(button).removeClass('up');
	        visible = 0;
	    } else {
	        $(box).slideDown();
	        $(button).addClass('up');
	        visible = 1;
	    }
	    
        $.ajax({
            url: (App.base_url + 'handyinfobox/' + visible),
            async: true,
            dataType: 'json',
            error: function () {
            },
            success: function (json) {
            }
        });
        
	    return false;
	},

	// IE6 functions
	hover_IE6: function () 
	{
        $('ul#main-menu').children('li').each(function () {
            $(this).mouseover(function () {
                $(this).addClass('hover');
            });
            $(this).mouseout(function () {
                $(this).removeClass('hover');
            });
        });
        
        $('ul#main-menu').children('li').each(function () {
            $(this).find('li').each(function () {
                $(this).mouseover(function () {
                    $(this).addClass('hover');
                });
                $(this).mouseout(function () {
                    $(this).removeClass('hover');
                });
            });
        });

        
		$('ul#client_tabs').find('li').each(function () {
			$(this).mouseover(function () {
				if ($(this).hasClass('hover')) {
					$(this).addClass('current');
				} else {
					$(this).addClass('hover');
				}
			});
			$(this).mouseout(function () {
				if ($(this).hasClass('current')) {
					$(this).removeClass('current');
				} else {
					$(this).removeClass('hover');
				}
			});
		});
	}
};

