$(function(){
    global.mixxtbar && hook.initMixxtbar();
    general && hook.initGeneral();
    layout.error && hook.initLayout();
    overlay.init();
    global.mixxtbar || login.init();
    welcomeBox.init();

    $('#header .searchtext').focus(function(){
        if($(this).val() == 'Suche')
            $(this).val('');
    }).blur(function(){
        if($(this).val() == '')
            $(this).val('Suche');
    });

    $(window).bind('resize', function () {
        overlay.init();
    });
});
function custom_game_tracking_transfer_data(number_nid, string_type, user_action, timestamp, number_round, number_tournament) {
	api.callJSON("/api/js/tee-off_tracking", {
		number_nid: number_nid,
		string_type: string_type,
		user_action: user_action,
		timestamp: timestamp,
		number_round: number_round,
		number_tournament: number_tournament
	});
}
items = {
    listVote: function(instanceID, elementID) {
        api.callJSON("/api/js/network/items/api_save_value", {
                elementID: elementID,
                instanceID: instanceID,
                value: 1
            }, function(data) {
                $("#rating_" + elementID + "_" + instanceID).html(data.count);
            }
        );
    },
    gotoRegion: function(me, identifier) {
        var url = "/networks/items/" + identifier + ".byReference:" + identifier + ":" + $(me).html();
        window.location.href = url;
    },
    getGolfList: function(page) {
        var typeID = $("#typeID").val();
        api.replaceFileWithParameters('/networks/items/getGolfList', { currentPage: page, typeID: typeID }, '#golfList', false, true);
    }
}
teeoff = {
    load: function(){
        overlay.toggle();
        $('#global').addClass('globalBig').show().children().load('/networks/tee-off #inner');

        return false;
    },

    unload: function(){
        overlay.toggle();
        $('#global').hide().removeClass('globalBig').children().html('');
    }
}
login = {
    init: function(){
        this.form = $('#userlogin');
        this.idfield = $('#login_id', this.form);
        $('#login_login', this.form).hover(function(){
                $(this).addClass('hover');
        }, function(){
            $(this).removeClass('hover');
        }).focus(function(){
            $(this).addClass('focus');
        }).blur(function(){
            $(this).removeClass('focus');
        });
    },
    idfield: null,
    form: null,
    show: function() {
        login.form.show();
        login.idfield.focus();
        if(!login.visible)
            window.setTimeout(function(){$('body').one('click', login.close)}, 10);
        login.visible = true;
        return false;
    },
    close: function(e) {
        if($(e.target).parents('#userlogin').length == 0){
            login.form.hide();
            login.visible = false;
        }
        else {
            window.setTimeout(function(){$('body').one('click', login.close)}, 10);
        }
    }
}

/* welcome Box switcher */
welcomeBox = {
    current: 1,
    init: function(){
        $('#welcomeBox .switcher li').live('mouseover', function(){
                $(this).addClass('hover');
        }).live('mouseout', function(){
                $(this).removeClass('hover');
        });
    },
    show: function(id, elem){
        if($(elem).hasClass('active')) return;
        $('#welcomeBox').children('.switch'+welcomeBox.current).fadeOut().siblings('.switch'+id).fadeIn(
            function(){
                !$.support.opacity && $(this).removeAttr('style');
            });
        welcomeBox.current = id;

        $(elem).siblings('.active').removeClass('active');
        $(elem).addClass('active');
    }
}
/* end welcome Box switcher */

/* global hook + overlay */
overlay = {
    init: function() {
        options = {
            isAnimated: false,
            color: '#FFFFFF',
            opacity: 0.85
        };
        layout.overlay.init(options);
    }
}
hook = {
    initMixxtbar: function() {
        hook.addressBookOpened     = false;
        hook.switchSettings        = global.mixxtbar.settings.switchSettings;
        hook.switchMessageCenter   = global.mixxtbar.messages.switchMessageCenter;
        hook.loadNewMsg            = global.mixxtbar.messages.loadNewMsg;
        
        hook.contentMarkerOpen     = global.mixxtbar.feedback.contentMarkerOpen;
        hook.contentMarkerClose    = global.mixxtbar.feedback.contentMarkerClose;
        
        hook.openAddressBook       = global.mixxtbar.messages.openAddressBook;
        hook.closeAddressBook      = global.mixxtbar.messages.closeAddressBook;
        
        global.mixxtbar.messages.loadNewMsg = function(a,b,c) {
            layout.overlay.show();
            return hook.loadNewMsg(a,b,c);
        }
        global.mixxtbar.messages.switchMessageCenter = function(a,b,c) {
            layout.overlay.toggle();
            return hook.switchMessageCenter(a,b,c);
        }
        global.mixxtbar.settings.switchSettings = function(a,b,c) {
            layout.overlay.toggle();
            return hook.switchSettings(a,b,c);
        }
        global.mixxtbar.feedback.contentMarkerOpen = function(a) {
            layout.overlay.show();
            return hook.contentMarkerOpen(a);
        }
        global.mixxtbar.feedback.contentMarkerClose = function() {
            layout.overlay.hide();
            return hook.contentMarkerClose();
        }
        global.mixxtbar.messages.openAddressBook = function(a) {
            if(!hook.addressBookOpened) {
                layout.overlay.show();
                hook.addressBookOpened = true;
                return hook.openAddressBook(a);
            }
        }
        global.mixxtbar.messages.closeAddressBook = function() {
            if(hook.addressBookOpened) {
                layout.overlay.hide();
                hook.addressBookOpened = false;
                return hook.closeAddressBook();
            }
        }
        
    },
    initGeneral: function() {
        general.checkMixxtID = function() {
            var id2check = $('#choose_id').val();
            if(id2check == "") {
                return;
            }
            $('#check_domain_message_ctr').empty();
            $('#check_domain_message_ctr').append('<img src="/images/ajax_indicator.gif" width="16" height="16" />');
            api_call_json("/api/js/checks/check_mixxtid", { mixxtID: id2check }, function (data) {
                        $('#check_domain_message_ctr').empty();
                        if (!data.error) {
                            $('#check_domain_message_ctr').removeClass('fault_message');
                            $('#check_domain_message_ctr').addClass('available_message');
                            $('#check_domain_message_ctr').append(data.text + " <img src=\"/static/css/golf4all/images/icons/gruenerpfeil.jpg\" width=\"16\" height=\"16\" alt=\"" + data.text + "\" />");
                        } else {
                            $('#check_domain_message_ctr').removeClass('available_message');
                            $('#check_domain_message_ctr').addClass('fault_message');
                            $('#check_domain_message_ctr').append(data.text + " <img src=\"/static/css/golf4all/images/icons/roteskreuz.jpg\" width=\"16\" height=\"16\" alt=\"" + data.text + "\" />");
                        }
                    $('#checkmessage').fadeIn('slow');
                }
            );
        }
    },
    initLayout: function() {
        layout.error.image         = '/static/css/golf4all/images/icons/icon_warn.gif';
        hook.openAlert             = layout.openAlert;

        layout.openAlert = function(a,b,c,d,e) {
            var options = {
                type: 'alert',
                text: a,
                isAnimated: true,
                isModal: true,
                overlay: {
                    isAnimated: false,
                    opacity: 0.85,
                    color: '#FFFFFF'
                }
            };
            layout.popup.show(options);
        }
    }
}
/* end global hook + overlay */
/* social bookmark */
bookmark = {
    toggle: function() {
        var b = $('#socialBookmark');
        if(b.hasClass('hide')) {
            bookmark.show();
        } else {
            bookmark.hide();
        }
    },
    hide: function() {
        $('#socialBookmark').addClass('hide');
    },
    show: function() {
        $('#socialBookmark').removeClass('hide');
    },
    click: function(type) {
        var active_url 		    = encodeURIComponent(location.href);
        var active_description  = encodeURIComponent(document.title);
        var url			        = "#";

        switch(type) {
            case 'delicious':
                url = "http://del.icio.us/post?v=2&url="+active_url+"&notes=&tags=&title="+active_description;
                break;
            case 'digg':
                url = "http://digg.com/submit?phase=2&url="+active_url+"&bodytext=&tags=&title="+active_description;
                break;
            case 'facebook':
                url = "http://www.facebook.com/share.php?u="+active_url+"&t="+active_description;
                break;
            case 'google':
                url = "http://www.google.com/bookmarks/mark?op=add&hl=de&bkmk="+active_url+"&annotation=&labels=&title="+active_description;
                break;
            case 'linkarena':
                url = "'http://linkarena.com/bookmarks/addlink/?url="+active_url+"&title="+active_description+"&desc=&tags=";
                break;
            case 'wong':
                url = "http://www.mister-wong.de/index.php?action=addurl&bm_url="+active_url+"&bm_notice=&bm_description="+active_description+"&bm_tags=";
                break;
            case 'reddit':
                url = "http://reddit.com/submit?url="+active_url+"&title="+active_description;
                break;
            case 'stumbleupon':
                url = "http://www.stumbleupon.com/submit?url="+active_url+"&title="+active_description;
                break;
            case 'technorati':
                url = "http://technorati.com/faves/seoportal?add="+active_url+"&tag=";
                break;
            case 'twitter':
                url = "http://twitter.com/home?status="+active_description+": "+active_url;
                break;
            case 'yigg':
                url = "http://yigg.de/neu?exturl="+active_url;
                break;
            case 'webnews':
                url = "http://www.webnews.de/einstellen?url="+active_url+"&title="+active_description;
                break;
            default:
                url = "#";
                alert('Für den Dienst '+type+' existiert leider keine URL zum verlinken.');
                return;
                break;
        }

        window.open(url);
    }
}
/* Golf4All Suche */
g4asearch = {
    start: function() {
        var form = $("#searchform");
        var name = form.find('input[name=search]');
        var nameDef = 'Golfplatz/-clubname';
        var zip = form.find('input[name=GeoSearch]');
        var zipDef = 'Ort / PLZ / Land';
        var radius = form.find('select[name=distance]');
        
        if((name.val().trim() == nameDef || name.val() == '') && (zip.val().trim() == zipDef || zip.val() == '' 
        	|| zip.val().trim().length < 2)) {
            form.find('div.searchError').removeClass('hide');
            window.setTimeout("$('div.searchError').addClass('hide');", 6000);
            return false;
        }
        if(name.val().trim() == nameDef)
        	name.val('');
        if(zip.val().trim() == zipDef)
        	zip.val('');
        
        return true;
    },
    setPage: function(page) {
        $("#searchPage").val(page);
        $("#searchSubmitButton").click();
    },
    updateAction: function(elem){
        var value = "/networks/search/" + elem.value;
        $(elem).parents('form').attr('action', value);
    }
}

/* Showcases */
showcase = {
    init: function() {
        $('body').append('<div id="showcase" style="display:none;"><div class="inner"><div class="close"><a href="javascript:void(0);" onclick="showcase.close();"><img src="/static/css/golf4all/images/icons/close.png" alt="schließen" title="schließen" width="22" height="22" /></a></div><iframe src="/images/ajax_loader.gif" class="frame" frameborder="0"></iframe></div></div>');
        showcase.elm = $('#showcase').hide();
    },
    open: function(url) {
        layout.overlay.show();
        $('#showcase .frame').attr('src', url);
        showcase.elm.show();
    },
    close: function() {
        layout.overlay.hide();
        $('#showcase .frame').attr('src', '/images/ajax_loader.gif');
        showcase.elm.hide();
    }
}

