var flip = 1;
var buddy = {
    init: function(){
        buddy.setToggleKeyCode();
        if (user != '') {
            buddy.setupStatusBar();
            buddy.setupNotifications();
            buddy.highlight_trigger();
			
        }
        //$("#update").live("click", buddy.update);
        $("#buddyImg").live("click", buddy.showBuddy);
	$(".rotator_h").live("click", function(){alert('clicked!')});
    },
    setupNotifications: function(){
        $('#Notification').jnotifyInizialize({
            oneAtTime: false,
            appendType: 'append'
        }).css({
            'position': 'fixed',
            'right': '0px',
            'bottom': '2px',
            'width': '250px',
            'z-index': '9999'
        });
    },
    setupStatusBar: function(){
        $('#StatusBar').jnotifyInizialize({
            oneAtTime: true
        }).css({
            'display': 'block',
            'position': 'fixed',
            'right': '0px',
            'top': '0px',
            'textAlign': 'center',
            'z-index': '9999'
        });
        $('#StatusBar').jnotifyAddMessage({
            text: user + ' - <a href="/logout">logout</a>  ',
            permanent: true,
            type: 'message',
            showIcon: false
        });
    },
    setupHighlight: function(){
        $('.buddy_highlight').prev().each(function(i){
            $e = $(this);
            epos = $e.position();
            _t = epos.top;
            _l = epos.left;
            _w = $e.width();
            _h = $e.height();
            $(this).next().css({
                left: _l,
                top: _t,
                width: _w,
                height: _h
            });
        })
    },
    setToggleKeyCode: function(){
        $(window).keypress(function(event){
            if (event.keyCode == '96' || event.charCode == '96') {
                buddy.toggle();
            }
        });
    },
    toggle: function(){
        //
        $(".buddyImg").toggle(flip++ % 2 == 0);
        if (flip % 2 == 1) {
            buddy.highlight_trigger();
        }
        else 
            if (flip % 2 == 0) {
                //alert("turn off highlight");
                $(".buddy_highlight").stop();
                $(".buddy_highlight").css({
                    opacity: '0'
                })
            };
            },
    highlight_trigger: function(){
        if (flip == 1) {
            buddy.setupHighlight();
        }
        $(".buddy_highlight").css({
            opacity: '1'
        }).stop().delay(2500).animate({
            opacity: '0'
        }, 3500);
    },
    update: function(){
        var mode = 1;
        var html = $("#html", $("#buddyForm")).val();
        var code = $("#code", $("#buddyForm")).val();
        var comment = $("#comment", $("#buddyForm")).val();
        var data = {
            code: code,
            html: html,
            mode: mode,
            comment: comment
        };
        //alert("Data:\ncode: " + data.code + "\nhtml: " + data.html + "\ncomment: " + data.comment )
        $.post("/buddy", data, function(responseText){
            buddyArray = responseText.split("|");
            span = "#buddy_" + buddyArray[0];
            newhtml = buddyArray[1];
            $(span).html(newhtml);
            /*.effect("highlight", {
             color: '#0073ea'
             }, 3000);*/
            $('#Notification').jnotifyAddMessage({
                text: 'updated: ' + buddyArray[0],
                permanent: false,
                type: 'message'
            });
            buddy.setupHighlight();
            buddy.highlight_trigger();
            
        });
    },
    
    showBuddy: function(){
        var code = $(this).attr("code");
        data = {
            code: code
        };
		w_height = $(window).height() ;
		w_width =  $(window).width();
        $.get('/buddy', data, function(responseHTML){
            $("#buddy").html(responseHTML);
            $("#buddy").dialog({
                modal: true,
                width: (85/100) * w_width,
				height:(85/100) * w_height,
                title: "Edit section: " + code,
                minHeight:500,
				minWidth: 500,
                closeOnEscape: true,
                resizable: true,
                autoOpen: true,
                buttons: {
                    Cancel: function(){
                        $(this).dialog('destroy');
                        $(this).html('');
                    },
                    'Update': function(){
                        buddy.update();
                        $(this).dialog('destroy');
                        $(this).html('');
                    }
                    
                },
                close: function(){
                    $(this).dialog('destroy')
                }
            });
        });
    },
    
    showRotatorEdit: function(){
	var data = {};
	var w_height = $(window).height();
	var w_width = $(window).width();
	$.get('/rotator_edit', data, function(HTML){
		$('#rotator_dialog').html(HTML);
		$('#rotator_dialog').dialog({
			modal: true,
			width: (85/100) * w_width,
			height: (85/100) * w_height,
			title: 'Edit Rotator Images',
			minHeight: 500,
			minWidth: 500,
			closeOnEscape: true,
			resizable: true,
			autoOpen: true,
			buttons:{
				Cancel: function(){
					$(this).dialog('destroy');
					$(this).html('');
				},
				'Update': function(){
					buddy.updateRotator();
					$(this).dialog('destroy');
					$(this).html('');
				}
			},
			close: function(){$(this).dialog('destroy')}
		});	
	});
    }

}
//$(document).ready(function(){
    //buddy.init();
//})

//$(".buddyImg").next().toggleClass('buddyEdit');
/*bhtml = $(".buddyImg").next().html();

 if (bhtml.substring(0, 4) == 'Edit') {

 $(".buddyImg").next().toggleClass('hideNew');

 //alert(bhtml.substring(0,4));

 }*/


